Inheritance
Inheritance
Inheritance
B Class
Syntax:
class derived-class-name : visibility-mode base-class-name
{
------------------------------
Members of the derived class;
-------------------------------
};
Note:
1) If visibility mode is not given, then by default the visibility
mode is private.
2) All members of the class except private are inherited
Derriving classes
3)Protected Inheritance
Public members of the base class become protected in
the derived class
Private members cannot be inherited to the derived
class.
Protected members of the base class become protected
in a derived class
Levels/Types of Inheritance
1)Single Inheritance
If a class is derived from a single base class, it is called as
single inheritance.
2)Multilevel Inheritance
The classes can be also derived from the classes that are
already derived is called as multilevel inheritance.
3)Multiple Inheritance
If a class is derived from more than one base class is
called as multiple inheritance.
4)Hierarchical Inheritance
If a number of classes are derived from a single base
class is called as hierarchical inheritance.
5)Hybrid Inheritance
A combination of one or more types of inheritance is
called as hybrid inheritance.
OR
It is a combination of Hierarchical and multilevel
inheritance.
Hierarchical Inheritance
Multilevel Inheritance
Abstract Class
An abstract class is one that is not used to create an
object. An abstract class is designed only to act as a base
class(to be inherited by other classes).
Constructor in derived classes
When constructor is used in the inheritance ie both in the
base class and derived class, then base constructor is
executed first and then the constructor in the derived class is
executed.
Ex: use of constructor in single level inheritance.
class base
{
public: base() // base class constructor
{
}
};
d.display();
d.read1();
//Member functions of derived class
d.display1();
getch();
}