訪問控制 : public、private、protected以及friend
https://blog.csdn.net/Tostick/article/details/80685482
public 所有均可訪問
private 類自己的成員函式訪問,不能被類物件訪問
protected 類自己以及子類訪問,不能被類物件訪問
friend 友元,別人是你的朋友,他可以訪問我的東西。(但不是我可以訪問他的東西)
友元關係不能被繼承。
友元關係是單向的,不具有交換性。若類B是類A的友元,類A不一定是類B的友元,要看在類中是否有相應的宣告。
友元關係不具有傳遞性。若類B是類A的友元,類C是B的友元,類C不一定是類A的友元,同樣要看類中是否有相應的申明
友元函式並不是類的成員函式,因此在類外定義的時候不能加上class::function name
例子1
#include <iostream> using namespace std; class A { private: int i; public: A() {i = 0; cout <<"A::A"<<endl; } ~A() {i = 0; cout <<"A::~A"<<i<<endl; } void set(int i) {this->i = i;} void g(A *q) { cout << q->i << endl; } }; int main () { A a; A b; b.set(100); a.g(&b); // 使用物件a的成員函式訪問物件b中的私有變數i,則不能通過物件直接訪問 return 0; }
結果
備註:
private,protected限制編譯時刻檢查,而不是執行檢查。一直編譯成二進位制,則不再受到此限制。
struct 預設許可權是public, class預設許可權是private
C++中,只有成員變數很簡單數量少才可能使用struct;一般使用class
例子2-protected
#include <iostream> using namespace std; class A { public: int a = 1; private: int b = 2; protected: int c = 3; }; class B: public A { public: void func() { std::cout << A::a << std::endl; // 可通過A類訪問公有變數 // std::cout << A::b << std::endl; // 不能在B類中通過A類訪問私有變數 std::cout << A::c << std::endl; // 可通過A類訪問保護變數 } }; int main() { B b; A a; cout << b.a << a.a << endl; // 可通過A類變數訪問共有變數 // cout << b.c << a.c << endl; // 不可通A過類變數訪問保護變數 return 0; }
注:
類中省略public 等時,預設是private屬性
繼承基類, 在基類前省略public 等時, 預設是protected屬性
例子3-友元函式,外部呼叫可訪問類的變數和方法
#include <iostream> #include <math.h> using namespace std; class Point { public: Point(double xx, double yy) { x=xx; y=yy; } //預設建構函式 void Getxy(); //公有成員函式 friend double Distance(Point &a, Point &b); //友元函式 private: double x, y; int key = 101; public: int kk = 102; }; void Point::Getxy() { cout<<key<<endl; // 函式定義中訪問私有變數 } double Distance(Point &a, Point &b) // 友元定義,注意函式名前未加類宣告符 { double dx = a.x - b.x; double dy = a.y - b.y; return sqrt(dx*dx+dy*dy); } int main() { Point p1(3.0, 4.0), p2(6.0, 8.0); p1.Getxy(); // cout << p2.key << endl; 不能訪問私有變數 // cout << Point::key << endl; 不能訪問非靜態變數 cout << p2.kk << endl; double d = Distance(p1, p2); // 友元訪問 cout<<"Distance is: "<<d<<endl; return 0; }
結果:
例子4-友元類,外部呼叫可訪問類的變數和方法
當一個類作為另一個類的友元時,它的所有成員函式都是另一個類的友元函式,都可以訪問另一個類的私有或者公有成員
友元作為引數與使用類指標作為引數的區別在於,友元可以訪問類的私有成員
#include <iostream> #include <cstring> using namespace std ; //宣告教師類 class Techer ; //學生類 class Student { private: string name ; int age ; char sex ; int score ; public : Student(string name , int age , char sex , int score); void stu_print(Techer &tech); }; //教師類 class Techer { private: string name ; int age ; char sex ; int score ; public : Techer(string name , int age , char sex , int score); //宣告一個友元類 friend Student ; }; //Student類的建構函式的實現 Student::Student(string name , int age , char sex , int score) { this->name = name ; this->age = age ; this->sex = sex ; this->score = score ; } //Techer類的建構函式的實現 Techer::Techer(string name , int age , char sex , int score) { this->name = name ; this->age = age ; this->sex = sex ; this->score = score ; } //列印Student類中的私有成員和Techer的私有成員 void Student::stu_print(Techer &tech) { //用this指標訪問本類的成員 cout << this->name << endl ; cout << this->age << endl ; cout << this->sex << endl ; cout << this->score << endl ; //訪問Techer類的成員 cout << tech.name << endl ; cout << tech.age << endl ; cout << tech.sex << endl ; cout << tech.score << endl ; } int main(void) { Student stu1("YYX",24,'N',86); Techer t1("hou",40,'N',99); stu1.stu_print(t1); return 0 ; }
結果
備註:
運算子過載經常使用friend
friend寫在類宣告中,一旦類實現完成,則不能再新增friend
friend的授權在編譯期
---------------------
作者:gocgocgo
來源:CSDN
原文:https://blog.csdn.net/Tostick/article/details/80685482
版權宣告:本文為博主原創文章,轉載請附上博文連結!