C++学习(4)
今天主要学了多层继承的构造函数和析构函数,多继承的二义性问题以及虚基类
1)多层继承:对于一个空心管: Point<-----Circle<-----Tube(Tube 中还有InCircle 内圆成员)
多层继承构造函数和析构函数
1 //多层继承的构造函数和析构函数 2 #include <iostream> 3 using namespace std; 4 class Point{ 5 private: 6 int X,Y; 7 public: 8 Point(int X,int Y) 9 { 10 this->X=X; 11 this->Y=Y; 12 cout<<"Point("<<X<<","<<Y<<") constructing..."<<endl; 13 } 14 ~Point() 15 { 16 cout<<"Point("<<X<<","<<Y<<") destructing..."<<endl; 17 } 18 }; 19 20 class Circle: protected Point 21 { 22 protected: 23 double radius; 24 public: 25 Circle(double R=0,int X=0,int Y=0):Point(X,Y) 26 { 27 radius=R; 28 cout<<"Circle constructing,radius:"<<R<<endl; 29 } 30 ~Circle() 31 { 32 cout<<"Circle destructing, dadius:"<<radius<<endl; 33 } 34 }; 35 36 class tube:protected Circle 37 { 38 private: 39 double height; 40 Circle InCircle; 41 public: 42 tube(double H,double R1,double R2=0,int X=0,int Y=0):InCircle(R2,X,Y),Circle(R1,X,Y) 43 { 44 height=H; 45 cout<<"tube constructing, height:"<<H<<endl; 46 } 47 ~tube() 48 { 49 cout<<"tube destructing, height:"<<height<<endl; 50 } 51 }; 52 53 int main() 54 { 55 tube TU(100,20,5); 56 return 0; 57 58 }
2)多继承:是指派生类具有两个或者两个以上的直接基类。
如果多个基类中拥有同名的成员,那么派生类在继承各个基类的成员后,当我们调用该派生类成员时,由于该成员标识不唯一,出现二义性。
多继承 二义性
1 //多继承 2 //car小汽车,Wagon小货车,StationWagon客货两用车 3 #include <iostream> 4 using namespace std; 5 class Car 6 { 7 private: 8 int power; 9 int seat; 10 public: 11 Car(int power,int seat) 12 { 13 this->power=power; 14 this->seat=seat; 15 } 16 void show() 17 { 18 cout<<"car power:"<<power<<" seat:"<<seat<<endl; 19 } 20 }; 21 22 class Wagon 23 { 24 private: 25 int power; 26 int load; 27 public: 28 Wagon(int power,int load) 29 { 30 this->power=power; 31 this->load=load; 32 } 33 void show() 34 { 35 cout<<"wagon power:"<<power<<" load:"<<load<<endl; 36 } 37 }; 38 39 class StationWagon: public Car,public Wagon 40 { 41 public: 42 StationWagon(int power,int seat,int load):Wagon(power,load),Car(power,seat) 43 { 44 } 45 void showSW() 46 { 47 cout<<"StationWagon:"<<endl; 48 Car::show(); 49 Wagon::show(); 50 } 51 }; 52 53 int main() 54 { 55 StationWagon SW(105,3,8); 56 // Sw.show(); //出现二义性,程序不知道是哪个类的show() 57 SW.showSW(); 58 return 0; 59 }
3)虚基类:为了解决从不同途径继承而来的同名的数据成员在内存中有不同的拷贝造成数据不一致的问题讲共同基类设置为虚基类。
虚基类
1 //为了解决从不同途径继承而来的同名的数据成员在内存中有不同的拷贝造成数据不一致的问题 2 //讲共同基类设置为虚基类。 3 //虚基类的构造函数 4 #include <iostream> 5 using namespace std; 6 class Automobile 7 { 8 private: 9 int power; 10 public: 11 Automobile(int power) 12 { 13 this->power=power; 14 cout<<"Automobile constructing..."<<endl; 15 } 16 void show() 17 { 18 cout<<" power:"<<power; 19 } 20 }; 21 22 class Car: virtual public Automobile 23 { 24 private: 25 int seat; 26 public: 27 Car(int power,int seat): Automobile(power) 28 { 29 this->seat=seat; 30 cout<<"Car constructing..."<<endl; 31 } 32 void show() 33 { 34 cout<<"Car:"; 35 Automobile::show(); 36 cout<<" seat:"<<seat<<endl; 37 } 38 }; 39 40 class Wagon: virtual public Automobile 41 { 42 private: 43 int load; 44 public: 45 Wagon(int power,int load):Automobile(power) 46 { 47 this->load=load; 48 cout<<"Wagon constructing..."<<endl; 49 } 50 void show() 51 { 52 cout<<"wagon:"; 53 Automobile::show(); 54 cout<<" load:"<<load<<endl; 55 } 56 }; 57 58 class StationWagon: public Car,public Wagon 59 { 60 public: 61 StationWagon(int CPower,int WPower,int seat,int load):Automobile(CPower),Wagon(WPower,load),Car(CPower,seat) 62 { 63 cout<<"StationWagon constructing..."<<endl; 64 } 65 void show() 66 { 67 cout<<"StationWagon:"<<endl; 68 Car::show(); //输出的是car power:105 seat:3 69 Wagon::show(); //输出的是wagon powe:105 load:8 因为公共虚基类自对象只能初始化一次 70 } 71 }; 72 73 int main() 74 { 75 StationWagon SW(105,108,3,8); 76 SW.show(); 77 return 0; 78 }