2023 4 23
#include<iostream> #include<string> using namespace std; class shape{ public: virtual void setvalues(float a,float b)=0; virtual float area()=0; }; class rectangle:public shape{ private: float length,width; public: void setvalues(float a,float b){ length=a; width=b; } float area(){ return length*width; } }; class trangle:public shape{ private: float heigth,length; public: void setvalues(float a,float b){ length=a; heigth=b; } float area(){ return (length*heigth)/2; } }; class zheng:public shape{ private: float length; public: void setvalues(float a,float b=0.0){ length=a; } float area(){ return length*length; } }; class rong:public shape{ private: float length; public: void setvalues(float a,float b=0.0){ length=a; } float area(){ return length*length*3.14; } }; int main(){ shape *p; rectangle rec1; trangle tra1; zheng z1; rong r1; p=&rec1; float a1,a2,b1,b2,c1,d1; cout<<"请输入长方形数据:"; cin>>a1>>a2; p->setvalues(a1,a2); cout<<"S:"<<p->area(); cout<<endl; p=&tra1; cout<<"请输入三角形数据:"; cin>>b1>>b2; p->setvalues(b1,b2); cout<<"S:"<<p->area(); cout<<endl; p=&z1; cout<<"请输入正方形数据:"; cin>>c1; p->setvalues(c1,0); cout<<"S:"<<p->area(); cout<<endl; p=&r1; cout<<"请输入圆形数据:"; cin>>d1; p->setvalues(d1,0); cout<<"S:"<<p->area(); cout<<endl; return 0; }