喜刷刷4——shape类—4(完成RsubC类)

本博客详细介绍了如何在已有的Shape类基础上,通过继承和封装,实现一个用于比较矩形和圆面积的RsubC类。该类能够根据布尔类型参数决定是计算矩形加圆的总面积还是矩形减去圆的面积。通过实例化RsubC类并调用其area()成员函数,可以得出具体的面积结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小聪送给小亮和小华的形状他们都很喜欢,小亮和小华非要比一下他们两个的形状,来看看小聪更爱谁,请完成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;

}

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

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;
}
class RsubC:public Shape
{
public:
 RsubC(int c,double w,double h,double r,bool s);
 double area();
private:
 Rectangle rectangle;
 Circle circle;
 bool sign;
};
RsubC::RsubC(int c,double w,double h,double r,bool s):Shape(c),rectangle(c,w,h),circle(c,r),sign(s){}
double RsubC::area()
{
if(sign==0)
    return rectangle.area()+circle.area();
else
    return rectangle.area()-circle.area();
}
int main()
{
RsubC rc=RsubC(1,2,3,1,1);
cout<<"RsubC area:"<<rc.area()<<endl;
return 0;

}

结果输出 小聪测试的RsubC的面积即 RsubC area:2.86

学习总结:

这几个shape类的题层层嵌套,循序渐进,很有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值