您的位置:首页 > 编程语言 > C语言/C++

c++ 学习日记 2017.8.1

2017-08-01 16:51 134 查看
#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.setHeight(4);
Rect.setWidth(5);

area = Rect.getArea();

cout << "Total area:" << area << endl;
cout << "Total paint cost:" << Rect.getCost(area) << endl;

return 0;
}

2017.8.1
学了类的继承,用教程上的例子写了下。
感觉继承后可以在一个类中直接用其他类的功能,就像直接在一个平台上调用所有的功能一样,很方便,表述也很清晰。

程序来自c++教程 http://www.runoob.com/cplusplus/cpp-inheritance.html 感谢提供
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: