例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 }
123
时间: 2024-10-13 12:28:16