码图:152 实现RoundTable类(C++)

题目描述:

定义一个Table类和Circle类,
Table类有高度high属性,Circle类有半径 radius属性,类型都为float。
Circle类有GetArea()方法,返回圆的面积。
Table类有GetHigh方法,返回Table的高度。

实现一个RoundTable类,它由Table类和Circle类共同派生出,并拥有color属性
同时实现 char* GetColor方法,返回color的值。

本题中圆周率请取为3.14

最终RoundTable类的使用方法如下所示,在你的代码中除了实现以上三个类,还需加入如下main函数:

int main(){
    float radius,high;
	char color[20];
	cin>>radius>>high>>color;
	
	RoundTable RT(radius,high,color);
	cout<<"Area:"<<RT.GetArea()<<endl;
	cout<<"High:"<<RT.GetHigh()<<endl;
	cout<<"Color:"<<RT.GetColor()<<endl;
	return 0;
}

实现:

#include<iostream>

using namespace std;

class Table {
private:
	float high;
public:
	Table() { high = 0; }
	Table(float h) { high = h; }
	float GetHigh()
	{
		return high;
	}

};
class Circle
{
private:
	float radius;
public:
	Circle() { radius = 0; }
	Circle(float r) { radius = r; }
	float GetArea() {
		return radius * radius * 3.14;
	}
};
class RoundTable :public Table, public Circle
{private:
	char color[20];
public:
	RoundTable(float r, float h, char* col) :Circle(r),Table(h)
	{
		strcpy(color, col);
	}
	char* GetColor()
	{
		return color;
	}

};

int main() {
	float radius, high;
	char color[20];
	cin >> radius >> high >> color;

	RoundTable RT(radius, high, color);
	cout << "Area:" << RT.GetArea() << endl;
	cout << "High:" << RT.GetHigh() << endl;
	cout << "Color:" << RT.GetColor() << endl;
	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值