1. 程式人生 > >C++學習(19)

C++學習(19)

com bubuko In turn http 成員 pub nta int

 1 //使派生類C只有公共基類A的一個成員變量a
 2 #include<iostream.h>
 3 
 4 class A{
 5     protected:
 6         int a;
 7     public:
 8         A(int aa){
 9             a=aa;
10         }
11         ~A(){}
12 };
13 
14 class B1:virtual public A{
15     protected:
16         int b1;
17     public:
18         B1(int
aa,int bb1):A(aa){ 19 b1=bb1; 20 } 21 ~B1(){} 22 }; 23 24 class B2:virtual public A{ 25 protected: 26 int b2; 27 public: 28 B2(int aa,int bb2):A(aa){ 29 b2=bb2; 30 } 31 ~B2(){} 32 }; 33 34 35 class C:public B1,public B2{
36 protected: 37 int c; 38 public: 39 // C(int aa1,int aa2,int aa3,int bb1,int bb2,int cc):A(aa1),B1(aa2,bb1),B2(aa3,bb2){ 40 // c=cc; 41 // } 42 43 C(int aa,int bb1,int bb2,int cc):A(aa),B1(aa,bb1),B2(aa,bb2){ 44 c=cc; 45 } 46 ~C(){}
47 void printA()const{ 48 cout<<"a="<<a<<endl; 49 } 50 }; 51 int main(){ 52 C myC(1,2,3,4); 53 myC.printA(); 54 return 0; 55 }

技術分享圖片

C++學習(19)