C++ 程序:执行继承

原文:https://www.studytonight.com/cpp-programs/program-to-perform-inheritance-in-cpp

下面是用 C++ 执行继承的程序。

#include <iostream.h>
#include<stdio.h>
#include<conio.h>

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(void) 
{
    Rectangle Rect;
    int area;
    Rect.setWidth(5);
    Rect.setHeight(7);

    area = Rect.getArea();
    cout << "Total area: " << Rect.getArea() << endl;

    cout << "Total paint cost: $" << Rect.getCost(area) << endl;
    getch();
    return 0;
}

总面积:35 油漆总成本:2450 美元