This document discusses types of inheritance in object-oriented programming including single, multilevel, multiple, hierarchical, and hybrid inheritance. It provides code examples and explanations of:
- Single, multilevel, multiple, hierarchical, and hybrid inheritance structures
- Access specifiers for base and derived classes and their effects
- Calling base class constructors from derived class constructors
- The virtual keyword and dynamic binding in inheritance
The document contains code examples demonstrating inheritance concepts like defining base and derived classes, accessing members of base classes, and calling base class constructors from derived classes. It also provides explanations of multilevel, multiple, and hybrid inheritance with diagrams.
13. void A :: getdata ()
{
cout<<"nn Enter value
a:";
cin>>a;
cout<<"Enter value b:";
cin>>b;
}
void B :: calculate ()
{
c = (a + b);
}
void B :: display ()
{
cout<<"n"<<a<<"+"<<b<
<"="<<c;
}
14.
15.
16. Access Public Protected Private
Same class/
Base class
yes yes yes
Derived
classes
yes yes no
Outside
classes
yes no no
Public inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Public Yes Yes
Private Private No No
Protected Protected Yes No
17. Private inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Private Yes No
Private Private No No
Protected Private Yes No
Protected inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Protected Yes No
Private Private No No
Protected Protected Yes No
21. class sub : public superA, private
superB
{
:
: //members of class sub
:
}
FOR INSTANCE:
22.
23.
24.
25. Multilevel Inheritance :-
Definition:
“It is the inheritance hierarchy wherein subclass acts as a
base class for other classes”
It is implemented by defining at least three classes.
In multilevel inheritance, there is one base class and the
remaining two is derived class.
26. Declaration :-
class A
{………..
……….}; // Base class
class B : public A
{………..
……….}; // B derived from A
class C : public B
{……….
………}; // C derived from B
28. Hybrid inheritance :-
Definition:
“Inheritance hierarchy that reflects any legal combination of
other four types of inheritance.”
Simple word:-
“The method of combining any two or more forms of
inheritance in single form is called hybrid inheritance.”
29. Fig. Hybrid Inheritance :-
Student
Test
Records
Result
Sports
Base
class
Intermediate
base class
Derived
class
Base
class
32. Constructors in Inheritance :-
• Base class constructors are automatically called for you if
they have no argument.
• If you want to call a superclass constructor with an argument,
you must use the subclass's constructor initialization list.
• C++ supports multiple inheritance (for better or worse), so the
base class must be referred to by name, rather than "super()".
33. Constructor Example :-
class SuperClass
{
public:
SuperClass(int foo)
{ // do something with foo }
};
class SubClass : public SuperClass
{
public: SubClass(int foo, int bar) : SuperClass(foo)
{ // do something with bar }
};
34. Virtual keyword determines if a member function of a class can
be over-ridden in its derived classes.
The non-virtual member functions are resolved at compiling
time and it’s called static binding. However, the c++ virtual
member functions are resolved during runtime and it’s called
as dynamic binding