Shape系列-4

2020年1月17日 1796点热度 0人点赞 0条评论

Shape系列-4

时间: 1ms        内存:128M

描述:

小聪送给小亮和小华的形状他们都很喜欢,小亮和小华非要比一下他们两个的形状,来看看小聪更爱谁,请完成RsubC类。RsubC类中包括Rectangle类和Circle类的数据成员,新增布尔类型的数据成员sign(sign等于0时,新面积等于Rectangle+Circle,sign等于1时,新面积等于Rectangle-Circle,新定义了求面积的成员函数area()。但是小聪没有为RsubC类写构造函数和成员函数,请帮助小聪完成RsubC类。

小强写的文件头和Shape类

#include<iostream>
#define PI 3.14
using namespace std;
class Shape
{
public:
 Shape();
 Shape(int c);
 int getcolor();
 double area();
protected:
 int color;
};
Shape::Shape()
{
 color=0;
}
Shape::Shape(int c)
{
 color=c;
}
int Shape::getcolor()
{
 return color;
}
double Shape::area()
{
 return 10000;
}

小聪写的Rectangle类
class Rectangle:public Shape
{
public:
 Rectangle(int c,double w,double h);
 double getwidth();
 double getheight();
 double area();
 double price();
protected:
 double height;
 double width;
};
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
 width=w;
 height=h;
}
double Rectangle::getwidth()
{
 return width;
}
double Rectangle::getheight()
{
 return height;
}
double Rectangle::area()
{
 return height*width;
}
double Rectangle::price()
{
 return height*width*color;
}

小聪写的Circle类

class Circle:public Shape
{
public:
 Circle(int c,double r);
 double getradius();
 double area();
 double price();
protected:
 double radius;
};

Circle::Circle(int c,double r):Shape(c)
{
 radius=r;
}
double Circle::getradius()
{
 return radius;
}

double Circle::area()
{
 return PI*radius*radius;
}
double Circle::price()
{
 return PI*radius*radius*color;
}

小聪的测试函数:

int main()

{

RsubC rc=RsubC(1,2,3,1,1);
cout<<"RsubC area:"<<rc.area()<<endl;
return 0;

}

提示:不用提交全部程序,只提交补充部分。

输入:

输出:

输出小聪测试的RsubC的面积。

示例输入:

示例输出:

RsubC area:2.86

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论