栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++继承

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号