Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Sunday 8 September 2013

Multiple Inheritances:



A C++ class can inherit members from more than one class and here is the extended syntax:
class derived-class: access baseA, access baseB....

Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example:
#include <iostream>

using namespace std;

// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Base class PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};

// Derived class
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();

// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;
}

When the above code is compiled and executed, it produces the following result:
Total area: 35
Total paint cost: $2450

No comments:

Post a Comment