#include <iostream>
#define pi 3.14
using namespace std;
class Shape{
public:
virtual float getArea(){return 0;}
virtual ~Shape(){}
};
class Rectangle:public Shape{
private:
int lenth,width;
public:
Rectangle(int len,int wid):lenth(len),width(wid){}
float getArea(){return width*lenth;}
};
class Circle:public Shape{
private:
int r;
public:
Circle(int r1):r(r1){}
float getArea(){return pi*r*r;}
};
int main()
{
int a,b,r;
cout<<"Input a,b:";
cin>>a>>b;
cout<<"Input r:";
cin>>r;
Shape *ps;
ps=new Rectangle(a,b);
Shape *pr;
pr=new Circle(r);
cout<<"Rectangle Area:"<<ps->getArea()<<","<<"Circle Area:"<<pr->getArea()<<endl;
delete ps;
delete pr;
return 0;
}
标签:return,继承,float,int,Shape,public,getArea From: https://www.cnblogs.com/yuanxinglan/p/17316493.html