例6.1
使用默认内联函数实现单一继承。
1 #include<iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 private: 8 int x, y; 9 public: 10 Point(int a, int b) 11 { 12 x = a; 13 y = b; 14 cout << "Point..." << endl; 15 } 16 void Showxy() 17 { 18 cout << "x=" << x << "y=" << y << endl; 19 } 20 ~Point() 21 { 22 cout << "Delete Point" << endl; 23 } 24 }; 25 26 class Rectangle :public Point 27 { 28 private: 29 int H, W; 30 public: 31 Rectangle(int a, int b, int h, int w) :Point(a, b)//构造函数初始化列表 32 { 33 H = h; 34 W = w; 35 cout << "Rectangle..." << endl; 36 } 37 void Show() 38 { 39 cout << "H=" << H << ",W" << W << endl; 40 } 41 ~Rectangle() 42 { 43 cout << "Delete Rectangle" << endl; 44 } 45 }; 46 47 void main() 48 { 49 Rectangle r1(3, 4, 5, 6); 50 r1.Showxy(); 51 r1.Show(); 52 53 system("pause"); 54 }
例6.2
演示使用protected成员。
1 #include <iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 protected: 8 int x, y;//声明保护数据成员 9 public: 10 Point(int a, int b) 11 { 12 x = a; 13 y = b; 14 } 15 void Show() 16 { 17 cout << "x=" << x << ",y=" << y << endl;//基类的Show()函数 18 } 19 }; 20 21 class Rectangle :public Point 22 { 23 private: 24 int H, W; 25 public: 26 Rectangle(int, int, int, int);//构造函数原型 27 void Show() 28 { 29 cout << "x=" << x << ",y=" << y << ",H=" << H << ",W=" << W << endl; 30 } 31 }; 32 33 Rectangle::Rectangle(int a, int b, int h, int w) :Point(a, b)//定义构造函数 34 { 35 H = h; 36 W = w; 37 } 38 39 void main() 40 { 41 Point a(3, 4); 42 Rectangle r1(3, 4, 5, 6); 43 a.Show();//基类对象调用基类Show()函数 44 r1.Show();//派生类对象调用派生类Show()函数 45 46 system("pause"); 47 }
例6.3
使用Point和Rectangle类演示赋值兼容规则的例子。
1 #include <iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 protected: 8 int x, y;//声明保护数据成员 9 public: 10 Point(int a, int b) 11 { 12 x = a; 13 y = b; 14 } 15 void Show() 16 { 17 cout << "x=" << x << ",y=" << y << endl;//基类的Show()函数 18 } 19 }; 20 21 class Rectangle :public Point 22 { 23 private: 24 int H, W; 25 public: 26 Rectangle(int, int, int, int);//构造函数原型 27 void Show() 28 { 29 cout << "x=" << x << ",y=" << y << ",H=" << H << ",W=" << W << endl; 30 } 31 }; 32 33 Rectangle::Rectangle(int a, int b, int h, int w) :Point(a, b)//定义构造函数 34 { 35 H = h; 36 W = w; 37 } 38 39 void main()//演示公有继承的赋值兼容性规则 40 { 41 Point a(1, 2);//基类对象a 42 Rectangle b(3, 4, 5, 6);//派生类对象b 43 a.Show(); 44 b.Show(); 45 46 Point& ra = b;//派生类对象初始化基类的引用 47 ra.Show();//实际调用的是基类的Show函数 48 49 Point *p = &b;//派生类对象的地址赋给指向基类的指针 50 p->Show();//实际调用的是基类的Show函数 51 52 Rectangle *pb = &b;//派生类指针pb 53 pb->Show();//调用派生类的Show函数 54 55 a = b;//派生类对象的属性值更新基类对象的属性值 56 a.Show(); 57 58 system("pause"); 59 }
例6.5
演示多重继承的例子。
1 #include <iostream> 2 3 using namespace std; 4 5 class A 6 { 7 private: 8 int a; 9 public: 10 void setA(int x) 11 { 12 a = x; 13 } 14 void showA() 15 { 16 cout << "a=" << a << endl; 17 } 18 }; 19 20 class B 21 { 22 private: 23 int b; 24 public: 25 void setB(int x) 26 { 27 b = x; 28 } 29 void showB() 30 { 31 cout << "b=" << b << endl; 32 } 33 }; 34 35 class C :public A, public B 36 { 37 private: 38 int c; 39 public: 40 void setC(int x, int y) 41 { 42 c = x; 43 setB(y); 44 } 45 void showC() 46 { 47 showB(); 48 cout << "c=" << c << endl; 49 } 50 }; 51 52 void main() 53 { 54 C obj; 55 56 obj.setA(53); 57 obj.showA();//输出a=53 58 obj.setC(55, 58); 59 obj.showC();//输出b=58 c=55 60 61 system("pause"); 62 }
例6.6
访问具有二义性的例子。
1 #include <iostream> 2 3 using namespace std; 4 5 class A 6 { 7 public: 8 void func() 9 { 10 cout << "a.func" << endl; 11 } 12 }; 13 14 class B 15 { 16 public: 17 void func() 18 { 19 cout << "b.func" << endl; 20 } 21 void gunc() 22 { 23 cout << "b.gunc" << endl; 24 } 25 }; 26 27 class C :public A, public B 28 { 29 public: 30 void gunc() 31 { 32 cout << "c.gunc" << endl; 33 } 34 void hun1() 35 { 36 A::func(); 37 } 38 void hun2() 39 { 40 B::func(); 41 } 42 }; 43 44 void main() 45 { 46 C obj; 47 48 obj.A::func();//输出a.func 49 obj.B::func();//输出b.func 50 obj.B::gunc();//输出b.func 51 obj.C::gunc();//输出c.gunc 52 53 obj.gunc();//输出c.gunc 54 obj.hun1();//输出a.func 55 obj.hun2();//输出b.func 56 57 system("pause"); 58 }
123