1 //下面是private继承举例 2 #include<iostream.h> 3 class A{ 4 private: 5 int a1; 6 protected: 7 int a2; 8 public: 9 int a3; 10 A(int x1,int x2,int x3){ 11 a1=x1; 12 a2=x2; 13 a3=x3; 14 } 15 ~A(){} 16 }; 17 18 class B:private A{ 19 private: 20 int b1; 21 protected: 22 int b2; 23 public: 24 int b3; 25 B(int x1,int x2,int x3,int y1,int y2,int y3):A(x1,x2,x3){ 26 b1=y1; 27 b2=y2; 28 b3=y3; 29 } 30 ~B(){} 31 32 void Print()const{ 33 //cout<<"a1="<<a1<<endl;//非法 34 cout<<"a2="<<a2<<endl; 35 cout<<"a3="<<a3<<endl; 36 cout<<"b1="<<b1<<endl; 37 cout<<"b2="<<b2<<endl; 38 cout<<"b3="<<b3<<endl; 39 } 40 }; 41 42 void Show(A &myA,B &myB){ 43 //cout<<"myA.a1="<<myA.a1<<endl;//非法 44 //cout<<"myA.a2="<<myA.a2<<endl;//非法 45 cout<<"myA.a3="<<myA.a3<<endl;//非法 46 47 //cout<<"myB.a1="<<myB.a1<<endl;//非法 48 //cout<<"myB.a2="<<myB.a2<<endl;//非法 49 //cout<<"myB.a3="<<myB.a3<<endl;//非法 50 //cout<<"myB.b1="<<myB.b1<<endl;//非法 51 //cout<<"myB.b2="<<myB.b2<<endl;//非法 52 cout<<"myB.b3="<<myB.b3<<endl; 53 } 54 55 int main(){ 56 return 0; 57 }
原文地址:https://www.cnblogs.com/Tobi/p/9249834.html
时间: 2024-11-02 17:00:52