How To Use Multiple Inheritance In C++

  1. #include<iostream.h>  
  2.    
  3. using namespace std;  
  4.   
  5. // base class  
  6. class Shape   
  7. {  
  8.    public:  
  9.       void setWidth(int w)  
  10.       {  
  11.          width = w;  
  12.       }  
  13.       void setHeight(int h)  
  14.       {  
  15.          height = h;  
  16.       }  
  17.    protected:  
  18.       int width;  
  19.       int height;  
  20. };  
  21.   
  22.   
  23. class PaintCost   
  24. {  
  25.    public:  
  26.       int getCost(int area)  
  27.       {  
  28.          return area * 70;  
  29.       }  
  30. };  
  31.   
  32. // Derived class  
  33. class Rectangle: public Shape, public PaintCost  
  34. {  
  35.    public:  
  36.       int getArea()  
  37.       {   
  38.          return (width * height);   
  39.       }  
  40. };  
  41.   
  42. int main(void)  
  43. {  
  44.    Rectangle a;  
  45.    int area;  
  46.    
  47.    a.setWidth(5);  
  48.    a.setHeight(7);  
  49.   
  50.    area = a.getArea();  
  51.      
  52.   
  53.    cout << "Total area: " << a.getArea() << endl;  
  54.   
  55.    cout << "Total paint cost: $" << a.getCost(area) << endl;  
  56.   
  57.    return 0;  
  58. }  
Output

Total area: 35
Total paint cost: $2450