Professional Documents
Culture Documents
Inheritance
• One of the most important concepts in object-oriented programming is
that of inheritance.
• Inheritance allows us to define a class in terms of another class, which
makes it easier to create and maintain an application.
• This also provides an opportunity to reuse the code functionality and fast
implementation.
• When creating a class, instead of writing completely new data members
and member functions, the programmer can designate that the new class
should inherit the members of an existing class.
• The idea of inheritance implements the is a relationship.
example,
• mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well
and so on.
• Inheritance is a mechanism for building class types from existing class types
Reusability
• Reusability--building new components by utilising existing
components- important aspect of OO paradigm.
• It is always good/ “productive” if we are able to reuse
something that is already exists rather than creating the same
all over again.
• This is achieve by creating new classes, reusing the properties
of existing classes.
• This mechanism of deriving a new class from existing/old
class is called “inheritance”.
Contd.
• The old class is known as
– base class
– super class
– parent class
A base class is a previously defined class that is used to define new classes
• The new class is known as
– sub class
– derived class
– child class.
A derived class inherits all the data and function members of a base class (in
addition to its explicitly declared members.)
members of ABC
};
B
Multiple Inheritance
• A derived class with several
base classes, is called multiple
Inheritance.
class A : public B, public C C B
{
members of A
};
A
Hierarchical Inheritance
• The properties of one class may be
inherited by more than one class is
called hierarchical Inheritance.
class A : public B
{ B
members of A
};
class C : public B
{ A C
members of B
};
Multilevel Inheritance
• The mechanism of deriving a class from another
derived class is known as multilevel Inheritance.
class B : public A
A
{
members of B
}; B
class C : public B
{
members of C
C
};
Hybrid Inheritance
• The hybrid Inheritance is a combination of all
types of Inheritance.
B
C
Multipath Inheritance
A
i
B C
I,j I,k
D
i,j,k,
sum
Example
class Shape // Derived class
{ class Rectangle: public Shape
public: {
void setWidth(int w) public:
{ int getArea()
width = w; {
} return (width * height);
void setHeight(int h) }
{ };
height = h; int main(void)
} {
protected: Rectangle Rect;
int width; Rect.setWidth(5);
int height; Rect.setHeight(7);
}; // Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
Access Control and Inheritance
• A derived class can access all the non-private members of
its base class.
• Thus base-class members that should not be accessible to
the member functions of derived classes should be declared
private in the base class.
private - - -
protected private protected protected
public private protected public