it
easier
application.
to
create
This
and
also
maintain
provides
an
an
class
by default.
#include <iostream>
// Base class
class Shape
{
public:
void setWidth(int w)
Rectangle Rect;
{
Rect.setWidth(5);
width = w;
Rect.setHeight(7);
}
void setHeight(int h)
height = h;
<< endl;
}
protected:
return 0;
int width;
int height;
};
}
When the above code is compiled and
executed, it produces the following result:
// Derived class
class Rectangle: public Shape
Total area: 35
{
public:
int getArea()
{
Inheritance:
A derived class can access all the non-
int main(void)
{
We
hardly
following way:
Access
Same
class
public
protected private
yes
yes
yes
Derived
classes
Outside
classes
yes
yes
from
a public base
yes
no
no
of
the
base class.
Protected
deriving
class
no
from
Inheritance: When
a protected base
class.
class.
Private Inheritance: When deriving
Type of Inheritance:
When deriving a class from a base class, the
base class may be inherited through public,
protected orprivate inheritance. The type of
inheritance is specified by the accessspecifier as explained above.
from
a private base
Multiple Inheritances:
{
height = h;
protected:
syntax:
int width;
int height;
};
access
is
one
of public,
class PaintCost
public:
int getCost(int area)
#include <iostream>
{
return area * 70;
};
class Shape
// Derived class
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
public:
int getArea()
{
};
// Print the total cost of painting
cout << "Total paint cost: $" <<
int main(void)
{
Rectangle Rect;
return 0;
int area;
}
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();