c++——繼承


#include <iostream>
using namespace std;

class Shape 
{
public:
	void setWidth(int w) 
	{
		width = w;
	}
	void setHeight(int h) 
	{
		height = h;
	}
protected:
	int width;
	int height;
};

class PaintCost 
{
public:
	int getCost(int area) 
	{
		return area * 70;
	}
};

//派生類
class Rectangle :public Shape,public PaintCost 
{
public:
	int getArea() 
	{
		return (width*height);
	}
};

int main() 
{
	Rectangle rect;
	int area;

	rect.setWidth(10);
	rect.setHeight(20);
	area = rect.getArea();

	cout << "total area:" << rect.getArea() << endl;

	cout << "total paint cost:$" << rect.getCost(area) << endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章