C++继承

C++继承,第1张

C++继承 C++继承

// 假设有一个基类 Shape,Rectangle 是它的派生类

#include 
using namespace std;

class Shape
{
public:
	void setLength(double len) { length = len; }
	void setWidth(double wid) { width = wid; }
	void setHeight(double hei) { height = hei; }

	// 成员
	double length;
	double width;
	double height;
};

class painCost
{
public:
	int getCost(double area)
	{
		cost = area * 10;
		return cost;
	}
	double cost;
};


class Rectangle:public Shape
{
public:
	int getVolume()
	{
		return length*width*height;
	}
	double getArea()
	{
		double Area = (length*height + length*width + width*height) * 2;
		return Area;
	}
};


class PainRectangle : public painCost, public Rectangle
{
public:
	double budget = 200;
	bool isInBudget()
	{
		if (cost > budget)
			return true;
		else
			return false;
	}
};

int main()
{
	Rectangle rec;
	rec.setLength(4);
	rec.setWidth(2);
	rec.setHeight(1);

	cout << "矩形的体积为:" << rec.getVolume() << endl;

	PainRectangle Prec;
	Prec.setLength(3);
	Prec.setWidth(2);
	Prec.setHeight(1);
	cout << "长方体的体积为:" << Prec.getVolume() << endl;
	cout << "长方体的表面积为:" << Prec.getArea() << endl;
	cout << "长方体表面涂漆需要花费:" << Prec.getCost(Prec.getArea()) << endl;

	if (Prec.isInBudget())
		cout << "花费为:" << Prec.cost << "t预算为:" << Prec.budget << "t超出预算" << endl;
	else
		cout << "花费为:" << Prec.cost << "t预算为:" << Prec.budget << "t没有超出预算" << endl;

	getchar();
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5115159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存