1 #include <iostream> 2 using namespace std; 3 class point 4 { 5 int x; 6 int y; 7 public : 8 point () 9 { 10 x=y=0; 11 } 12 point (int a,int b) 13 { 14 x=a; 15 y=b; 16 } 17 ~point (){} 18 void show () 19 { 20 cout <<"("<<x<<","<<y<<")"<<endl; 21 } 22 }; 23 class Shape 24 { 25 private: 26 double longth; 27 double width; 28 public : 29 Shape () 30 { 31 longth=width=0; 32 } 33 Shape (double l,double w) 34 { 35 longth=l; 36 width=w; 37 cout<<"构造函数,创建Shape"<<endl; 38 } 39 ~Shape() 40 { 41 cout <<"析构函数,释放Shape"<<endl; 42 } 43 double area() 44 { 45 double s; 46 s=longth*width; 47 return s; 48 } 49 void show () 50 { 51 cout <<"面积="<<area()<<endl; 52 } 53 }; 54 class Rectangle:public Shape 55 { 56 public : 57 Rectangle():Shape(0,0){} 58 Rectangle(double a,double b):Shape(a,b) 59 { 60 cout<<"创建Rectangle"<<endl; 61 } 62 void show () 63 { 64 cout <<"面积="<<area()<<endl; 65 } 66 ~Rectangle() 67 { 68 cout <<"析构函数,释放Rectangle"<<endl; 69 } 70 }; 71 class Square:public Rectangle 72 { 73 public : 74 Square(double a):Rectangle(a,a) 75 { 76 cout<<"构造函数,创建Square"<<endl; 77 } 78 ~Square() 79 { 80 cout <<"析构函数,释放Square"<<endl; 81 } 82 }; 83 class Circle:public Shape 84 { 85 private : 86 double r; 87 point p; 88 public : 89 Circle(double c,point n):Shape(c,c) 90 { 91 r=c; 92 p=n; 93 cout<<"构造函数,创建Circle"<<endl; 94 } 95 ~Circle() 96 { 97 cout <<"析构函数,释放Circle"<<endl; 98 } 99 double area() 100 { 101 double s; 102 s=3.14*r*r; 103 return s; 104 } 105 void show () 106 { 107 cout <<"面积="<<area()<<endl; 108 } 109 }; 110 int main() 111 { 112 Rectangle r(2,4); 113 r.show(); 114 point o(1,1); 115 Circle c (4.0,o); 116 c.show (); 117 Square s(4); 118 s.show(); 119 return 0; 120 }