帮忙看看这道C++的题怎么写吧,谢谢啦,最好有讲解

定义一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者均有GetArea成员函数用于计算对象的面积,GetPerim成员函数计算对象的周长;在... 定义一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者均有GetArea成员函数用于计算对象的面积,GetPerim成员函数计算对象的周长;在主程序中对它们进行测试。

刚刚忘了打就提交了= =
展开
 我来答
muqsh
2008-10-21 · TA获得超过129个赞
知道小有建树答主
回答量:233
采纳率:0%
帮助的人:235万
展开全部
#include <iostream>
using namespace std;

class Shape //定义一个Shape抽象类
{
public:
virtual double GetArea()=0;
virtual double GetPerim()=0;
};

class Rectangle : public Shape //派生Rectangle
{
public:
Rectangle(double h, double w) : height(h), width(w){}
double GetArea(){ return height * width; } //计算面积
double GetPerim(){ return 2*(height+width); } //计算周长
private:
double height;
double width;
};
class Circle : public Shape //派生Circle类
{
public:
Circle(double r):radius(r){}
double GetArea(){ return 3.14 * radius * radius; } //计算面积
double GetPerim(){ return 2 * 3.14 * radius; } //计算周长
private:
double radius;
};

void main()
{
Rectangle r(3,4);
cout<<"area of rectangle:"<<r.GetArea()<<endl;
cout<<"perim of rectangle:"<<r.GetPerim()<<endl;
Circle c(3);
cout<<"area of circle:"<<c.GetArea()<<endl;
cout<<"perim of circle:"<<c.GetPerim()<<endl;
}
guojingcha
2008-10-21 · TA获得超过1.1万个赞
知道大有可为答主
回答量:1.3万
采纳率:42%
帮助的人:9826万
展开全部
题目在哪里?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
medky
2008-10-21 · TA获得超过413个赞
知道小有建树答主
回答量:331
采纳率:0%
帮助的人:297万
展开全部
//做c的,不太会c++

#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
double Area; //数据类型没怎么讲究,你自己看着办
double Perim; //处理起来有点麻烦
Shape(){};
~Shape(){};
double GetArea() { return Area;} //基类的函数
double GetPerim() { return Perim;}
};

class RectAngle:public Shape
{
private:
int x,y; //矩形的长宽
public:
RectAngle(){};
RectAngle(int a,int b){x=a;y=b;};
~RectAngle(){};
double GetArea()
{
Area=(double)x*y; //带参数的构造函数
return Shape::GetArea();
}
double GetPerim()
{
Perim=(double)(2*x+2*y);
return Shape::GetPerim(); //自己的处理后再调用基类的处理
}
};

class Circle:public Shape
{
private:
int Ra;
public:
Circle(){Shape::Shape();};
Circle(int r){Ra=r;};
~Circle(){};

double GetArea()
{
Area=(double)(PI*Ra*Ra);
return Shape::GetArea();
}
double GetPerim()
{
Perim=(double)(2*PI*Ra);
return Shape::GetPerim();
}
};

void main()
{
RectAngle rect(3,4);
Circle cir(5);

cout<<rect.GetArea()<<"\t"<<rect.GetPerim()<<endl;
cout<<cir.GetArea()<<"\t"<<cir.GetPerim()<<endl;

cout<<RectAngle(4,7).GetArea()<<endl; //如果你只想调用RectAngle类的公式,就可以这样写,做一个匿名

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式