1 #include<iostream> 2 using namespace std; 3 const double PI=3.14; 4 5 class Shape 6 { 7 public: 8 Shape (float a,float b=0.0){this->a=a;this->b=b;} 9 protected: 10 float a,b; 11 }; 12 13 class Rectangle:public Shape 14 { 15 public: 16 Rectangle(float l,float w):Shape(l,w){} 17 float getArea(){return a*b;} 18 }; 19 20 class Circle:public Shape 21 { 22 public: 23 Circle(float r):Shape(r){} 24 float getArea(){return a*a*PI;} 25 }; 26 27 class Square:public Rectangle 28 { 29 public: 30 Square(float l):Rectangle(l,l){} 31 float getArea(){return a*a;} 32 }; 33 34 35 int main() 36 { 37 Rectangle R(3,4); 38 Circle C(5); 39 Square S(5); 40 cout<<"The Rectangle area is "<<R.getArea()<<endl; 41 cout<<"The Circle area is "<<C.getArea()<<endl; 42 cout<<"The Square area is "<<S.getArea()<<endl; 43 return 0; 44 }
时间: 2024-10-10 06:51:34