Inheritance
Inheritance
class A A
{
Member data and methods
} B
class B: public A
{
Member data and methods
}
Multilevel Inheritance
The classes can also be derived from the classes that are
already derived. This type of inheritance is called
multilevel inheritance.
class A A
{
Member data and methods
}
class B: public A B
{
Member data and methods
}
class C: public B C
{
Member data and methods
}
Multiple inheritance
If a class is derived from more than one base class, it
is known as multiple inheritance.
class A
{
A B
Member data and methods
}
class B C
{
Member data and methods
}
class C: public A, public B
{
Member data and methods
}
Hierarchical Inheritance
If a number of classes are derived from a single base class, it
is called as hierarchical inheritance.
class A
{ A
Member data and methods
}
class B: public A
{
B C
Member data and methods
}
class C: public A
{
Member data and methods
}
Hybrid Inheritance
D
class C: public B
{ Member data and methods
}
Virtual base class is a class that contains only one copy of the base
class when two or more objects are derived from the
class C: virtual base
public A class.
Example: {
class A ————
};
{
class D: public B, public C
————
{
}; ————
class B: virtual public A };
{
————
};
Abstract classes
An abstract class is the one that
is not used to create objects.
An abstract class is designed
only to act as a base class (to be
inherited by other classes).
It is a design concept in program
development and provides a base
upon which other classes may be
built.
Constructors in derived classes
Constructors are used to initialize data members of an object.
If any base class contains a constructor with one or more
arguments, then the derived class must have a constructor
that passes the arguments to the base class constructor.
We usually create objects of the derived class. The derived
class passes arguments to the base class constructor. When
both the derived and base classes contain constructors, the
base constructor is executed first and then the constructor in
the derived class is executed.
The base class constructors are called and executed before
executing the statements in the body of the derived
constructor.
If there is a default user defined constructor in base class, it
is called automatically when the object of the derived class is
created.
If there are no constructors defined in the base class then
compiler calls the default constructor of its own
If the base class has overloaded constructor then base class
constructor has to be called explicitly by the derived class
class A
{
A()
{// default constructor}
A(int a)
{ // parameterized constructor}
};
class B: public A
{
B():A(10)
{// B constructor should call A’s constructor
explicitly}
Destructors in derived classes
If the constructors are called down the line
from the base to the derived class, the
destructors are called just in the reverse
order.
That is from the derived class up to the
base class.
The destructor of the derived class is called
first then the destructor of the base class
will be called.
Since destructors do not take any
arguments they cannot be overloaded and
thus ambiguity does not occur