#include<iostream> #include<math.h> using namespace std; class shape { public: virtual double Area()=0; virtual double Setshaape()=0; }; class Tri:public shape{ private: double a1,b1,c1; public: double Area(); double Setshaape(); Tri(){}; }; double Tri::Area(){ if((a1+b1)>c1){ double p=(a1+b1+c1)/2.0; return sqrt(p*(p-a1)*(p-b1)*(p-c1)); } else{ cout<<"这不是一个三角形"<<endl; } } double Tri::Setshaape(){ cin>>a1>>b1>>c1; } class R:public shape{ private : double len,win; public: R(){} double Area(); double Setshaape(); }; double R::Setshaape(){ cin>>len>>win; } double R:: Area(){ return len*win; } class C:public shape{ private : double r; public: double Area(); double Setshaape(); C(){} }; double C::Setshaape(){ cin>>r; } double C::Area(){ double p; p= r*r*3.1415; return p; } class Total:public Tri,public R,public C{ public: double show (Tri t,R r,C c){ return double( t.Area()+r.Area()+c.Area()); } }; int main() { cout<<"请输入三角形三边"<<endl; Tri T; T.Setshaape(); cout<<T.Area()<<endl; cout<<"请输入矩形边长"<<endl; R r; r.Setshaape(); cout<<r.Area()<<endl; cout<<"请输入圆的半径"<<endl; C c; c.Setshaape(); cout<<c.Area()<<endl; Total T1; cout<<T1.show(T,r,c)<<endl; return 0; }
定义一个抽象类shape,包括名字,编号,性别,派生三个类,圆,矩形,三角形
猜你喜欢
转载自blog.csdn.net/liuzmcscs/article/details/80053717
今日推荐
周排行