See the Tutorial List

Program to perform Inheritance in C++

Following is the program to perform Inheritance.

#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;
}

Total area: 35 Total paint cost: $2450