Example of a C ++ program (static, friend)

xiaoxiao2021-03-06  64

#include using namespace std;

Class myclass {public: myclass () {mchar = 'a';}; static char * str; static void print (void); Friend Void PrintPrivate (MyClass & myclass); private: char mchar;};

Char * myclass :: str = "this is a static var";

Void myclass :: print (void) {cout << str << endl;}

Void PrintPrivate (MyClass & myclass) {cout << myclass.mchar << endl;

Void main () {myclass :: print (); myclass myclass; printprivate (myclass);

}

Char * myclass :: str = "this is a static var"; when initializing the STR, because the type is written, for example, you declare: static char * STR; then you initialize this when you initialize: Char * myclass :: str = "this is a static var"; Friend: There is a meticone, not part of the class, but can access private members in the class.

转载请注明原文地址:https://www.9cbs.com/read-84704.html

New Post(0)