正在加载图片...
#include<iostream. h> class m friend class N;/为M的友元,可以直接使用M中的私有成员 private int I, 3 void show(void)cout<<j=<<i<<It'<<j=<<j<<It'. 1 public M(int a=0, int b=Oi=a; j=b: 1 class n: public M/N为M的派生类 public: N(int a=0,int b=0): M(a, b 1 void Print(void) show(: cout<<1+=<<i+j<<endl; J void main/od)直接引用类M的私有成员函数和私有成员 Nn1(10,20) Mm1(100,200); ∥m1. showo;∥私有成员函数,在类外不可调用 1.Print(18 #include<iostream.h> class M { friend class N; //N为M的友元,可以直接使用M中的私有成员 private: int i , j; void show(void){cout<<"i="<<i<<'\t'<<"j="<<j<<'\t';} public: M(int a=0,int b=0){ i=a; j=b;} }; class N :public M{ //N为M的派生类 public: N(int a=0,int b=0):M(a,b){ } void Print(void){ show(); cout<<"i+j="<<i+j<<endl; } }; void main(void) { N n1(10,20); M m1(100,200); // m1.show(); //私有成员函数,在类外不可调用 n1.Print(); } 直接引用类M的私有成员函数和私有成员
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有