多继承
多继承:派生类继承多个基类(继承多父类特性)
语法:class <派生类名>: <继承方式1><基类名1> , <继承方式2><基类名2> , ...{
<派生类类体>;
}
1 #include <iostream> 2 3 using namespace std; 4 5 class info1 { 6 public: 7 void setter1(int a) { 8 num1 = a; 9 } 10 11 protected: 12 int num1; 13 14 private: 15 int age1; 16 }; 17 18 class info2 { 19 public: 20 void setter2(int a) { 21 num2 = a; 22 } 23 24 protected: 25 int num2; 26 27 private: 28 int age2; 29 30 }; 31 32 class info : public info1, public info2 { 33 public: 34 void fun(void) { 35 int a; 36 a = num1 + num2; 37 cout << a << endl; 38 } 39 40 protected: 41 int num3; 42 43 private: 44 int age3; 45 }; 46 47 48 int main(void) 49 { 50 info text; 51 52 /* text.num1; 由于多继承方式为public,所以派生类不能直接访问protected与private 53 text.age2; 54 */ 55 56 57 text.setter1(33); 58 text.setter2(44); 59 text.fun(); 60 61 return 0; 62 }
笔记