c++ 学习日记 2017.8.1

来源:互联网 发布:淘宝主图制作技巧 编辑:程序博客网 时间:2024/06/05 09:39

#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

感谢提供

原创粉丝点击