正在加载图片...
私有派生 (1)私有派生类对基类成员的访问 基类的公有成员相当于派生类的私有成员,派生类的成员数 可以访问它。但基类的私有成员派生类的成员函数不可以访问它。 例41 #include <iostream. h> void maino class baset 定义基类 int x; i derived obj; public obj. setxy(10, 20) void setx( int n)ix-n; void showxoi cout <<x<< endl obj. showy; class derived: private base定义私有派生类 int public void setxy(int n, int m i setx(o);y=m;) void showy cout<<x<y<endl;}∥非法不能访问x,改为 show01. 私有派生 (1) 私有派生类对基类成员的访问 基类的公有成员相当于派生类的私有成员,派生类的成员数 可以访问它。但基类的私有成员派生类的成员函数不可以访问它。 例4.1 #include <iostream.h> class base{ //定义基类 int x; public: void setx(int n) { x=n; } void showx() { cout << x << endl; } }; class derived : private base { //定义私有派生类 int y; public: void setxy(int n, int m) { setx(n); y=m; } void showxy() { cout << x << y << endl; } // 非法 不能访问 x ,改为 showx() }; void main() { derived obj; obj.setxy(10,20); obj.showxy(); } 5
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有