帮忙看看这道C++的题怎么写吧,谢谢啦,最好有讲解
定义一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者均有GetArea成员函数用于计算对象的面积,GetPerim成员函数计算对象的周长;在...
定义一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者均有GetArea成员函数用于计算对象的面积,GetPerim成员函数计算对象的周长;在主程序中对它们进行测试。
刚刚忘了打就提交了= = 展开
刚刚忘了打就提交了= = 展开
3个回答
展开全部
#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;
}
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;
}
展开全部
题目在哪里?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//做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类的公式,就可以这样写,做一个匿名
}
#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类的公式,就可以这样写,做一个匿名
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询