类的继承5

#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;
}

 

posted @   不会JAVA的小袁  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示