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 感谢提供
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 感谢提供
相关文章推荐
- C++学习日记7——模板篇的标准模板库
- C++学习日记
- c++学习日记 12-3
- C++学习日记
- C++学习日记2015.12.25
- c++学习日记 加virtual和不加virtual的区别
- 【学习日记3】c++开始&nbsp;我要做…
- C++学习日记13——递归、内联、函数重载、变量的作用域和存储类型
- 【学习日记2】c++开始&nbsp;我要做…
- C++学习日记
- C++学习日记--分支结构(if语句)
- C++学习日记(容器)
- C++学习日记
- 编程语言学习:C++学习日记0 热身
- 【学习日记1】c++开始&nbsp;我要做…
- 我的C++学习日记——变量和基本类型(一)
- C++学习日记/4类排序算法
- C++学习日记
- C++学习日记
- C++学习日记