#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<<"This is not a triangle"<<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()); } }; intmain() { cout<<"Please enter the three sides of the triangle"<<endl; Tri T; T.Setshaape (); cout<<T.Area()<<endl; cout<<"Please enter the side length of the rectangle"<<endl; R r; r.Setshaape (); cout<<r.Area()<<endl; cout<<"Please enter the radius of the circle"<<endl; C c; c.Setshaape (); cout<<c.Area()<<endl; Total T1; cout<<T1.show(T,r,c)<<endl; return 0; }
Define an abstract class shape, including name, number, gender, derived three classes, circle, rectangle, triangle
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324813419&siteId=291194637
Ranking