正在加载图片...
()外部函数(main)对私有派生类继承来的成员的访问 私有派生时,基类的所有成员在派生类中都成为私有成员,外部函 数不能访问。 例42 void maino #include <iostream.h> i derived obj; class baset ∥定义基类 obj. setx((10);∥出错 Int x; 0bety(20);∥合法 public obj. showxO;∥出错 void setx(int n)x=n;) obj. showy;∥合法 void showxoi cout <<x<< end; class derived: private base{/定义私有派生类 nt y; public void sety(int n)i void showy {cout≤<y<end;}(2) 外部函数(main)对私有派生类继承来的成员的访问 私有派生时,基类的所有成员在派生类中都成为私有成员,外部函 数不能访问。 例4.2 #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 sety(int n) { y=n; } void showy() { cout << y << endl; } }; void main() { derived obj; obj.setx(10); //出错 obj.sety(20); //合法 obj.showx(); //出错 obj.showy(); //合法 } 6
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有