2. Contents
Types of inheritance with examples
Constructors and Destructors in derived class
Virtual Base Classes, Virtual function and Pure
Virtual function
Abstract Base Classes
3. Inheritance
The mechanism of deriving a new class from an old one
is called inheritance.
C++ strongly supports the concept of reusability.
Once the class has been written and tested, it can be
adapted by other programmers to suit their
requirements.
The old class is referred to as the base class and the
new one is called the derived class or subclass.
4. Types of Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
5. Single Inheritance
A derived class with only one base class is called
single inheritance.
6. Multiple Inheritance
A derived class with more than one base classes is
called multiple inheritance.
8. Multilevel Inheritance
The mechanism of deriving a class from another
derived class is known as multilevel inheritance.
9. Hybrid Inheritance
A combination of any two above types of
inheritance is known as hybrid inheritance.
10. Defining Derived Classes
A derived class can be defined by specifying its
relationship with the base class in addition to its own
details.
General Syntax:
class derived_class_name : visibility-mode base_class_name
{
........... // members of derived class
...........
};
11. Defining Derived Classes
The visibility mode is optional and may be private and
public.
The default visibility mode is private.
Visibility mode specifies whether the features of the
base class are privately derived or publicly derived.
Example:
Class ABC : private | public XYZ
{
members of ABC
};
12. Visibility mode - Private
When a base class is privately inherited by a
derived class, public members of the base class
become private members of the derived class.
Members of base class is accessed by the member
functions of the derived class.
13. Visibility mode - Public
When the base class is publicly inherited, public
members of the base class becomes public members of
the derived class.
The members are accessible to the objects of derived
class.
In both the cases, the private members are not
inherited.
14. Single Inheritance Example (Public)
Single Inheritance ( public mode)
The class D is a public derivation of the base class B. Thus inherits
all the public members of B and retains their visibility.
The public member of class B is also a public member of the class
D.
The private members are not inherited by D.
The class D after inheritance will have more members than what it
contains at the time of declaration.
Class D (publically inherited)
15. Single Inheritance Example (Private)
Single inheritance ( private mode)
In private derivation the public members of the base
class become private members of the derived class.
Therefore the objects of D can not have direct access to
the public member functions of B.
Class D (privately inherited)
16. Making private Member Inheritable
C++ provides a third visibility modifier, protected,
which serve a limited purpose in inheritance.
A member declared as protected is accessible by the
member functions within its class and any class
immediately derived from it.
It cannot be accessed by the functions outside these two
classes.
17. Making private Member Inheritable
class alpha
{
private: // optional visible to
........... // member functions within its class
...........
protected: // visible to member functions of its own
............ // and derived class
............
public:
............ // visible to all functions in the program
............
};
19. Base Class (Protected Derivation)
It is also possible to inherit a base class in protected
mode.
Here, both the public and protected members of the
base class becomes protected members of the
derived class.
20. Practice Statement 1
Write a program to prepare marksheet of the college
examination with the following items read from the
keyboard: Student name, roll number, subject name,
subject code, internal marks, external marks. Design the
base class consisting of data members such as student
name, roll no, subject name. The derived class consists of
members subject code, internal and external marks.
21. Multilevel Inheritance
The method of deriving a class from another derived class is
known as multilevel inheritance.
The class A serves as a base class for the derived class B,
which in turn serves as a base class for the derived class C.
The class B is known as intermediate base class.
Example: Suppose the test results of students are stored in
three different classes. Class student stores roll number, class
test stores the marks obtained in two subjects and class result
contains the total marks obtained in the test. The class result
can inherit the details of marks obtained and the roll number
of students through multilevel inheritance.
22. Multiple Inheritance
A class can inherit the attributes of two or more classes.
It allows us to combine the features of several existing
classes as a starting point for defining new classes.
Syntax:
class D : visibility B1, visibility B2 .....
{
...........
...........
};
24. Ambiguity Resolution in Inheritance
Ambiguity occurs when a function with the same name appears in more
than one base class or appears in both base and derived class.
class M
{
public:
void display() { cout << “n class M” ; }
};
class N
{
public:
void display() { cout << “n class N” ; }
};
class P : public M, public N
25. Ambiguity Resolution in Inheritance
The problem can be solved by defining a named
instance within the derived class using the class
resolution operator with the function name.
Class P : public M, public N
{
public:
void display()
{ // overrides display()
M :: display() // of M and N
}
};
26. Hierarchical Inheritance
Inheritance support to the hierarchical design of a
program where certain features of one level are
shared by many others below that level.
Example
27. Hybrid Inheritance
There could be some situations where we need to
apply two or more types of inheritance to design a
program.
Example
28. Virtual Base Classes
Consider a situation where three kinds of
inheritance namely multilevel, multiple and
hierarchical inheritance are involved.
29. Virtual Base Classes
The child has two direct base classes parent1 and
parent2 which themselves have a common base class
grandparent.
The child inherits the public and protected members of
grandparent twice via parent1 and parent2.
This duplication can be avoided by making the common
base class virtual base class.
30. Virtual Base Classes
class A // grandparent
{
..............
};
class B1 : virtual public A // parent 1
{
...............
};
class B2 : public virtual A //parent 2
{
...............
}
class C : public B1, public B2 //child
{
............... // only one copy of A will be inherited
};
31. Virtual Base Classes
When a class is made virtual base class, C++ takes
necessary care to see that only one copy of that
class is inherited.
Example
32. Abstract Classes
An abstract class is one that is not used to create
objects.
An abstract class is designed only to act as a base
class.
Eg: Student is the abstract class since it was not used
to create any objects.
33. Constructors in Derived Classes
If no base class constructor takes any arguments, the derived
class need not have a constructor function.
However, if any base class contains a constructor with one or
more arguments then it is mandatory for the derived class to
have a constructor.
When both derived and base classes contain constructors,
the base constructor is executed first and then the
constructor in the derived class is executed.
34. Constructors in Derived Classes
The constructor of the derived class receives the
entire list if values as its arguments and passes them
on to the base constructors in the order in which they
are declared in the derived class.
35. Constructors in Derived Classes
The header line of the derived constructor function
contains two parts separated by a colon (:).
The first part provides the declaration of the arguments
that are passed to the derived constructor.
Second part list the function calls to the base
constructors.
Example.
37. Quiz 1
What is polymorphism?
An ability to have multiple forms is known as
polymorphism.
What is static/early binding?
Static Binding is the determination of which method to call
at compile time.
What is Dynamic/late binding?
Dynamic Binding is the determination of which method to
call at run time.
39. Run time Polymorphism
Selection of an appropriate member function at run
time is known as run time polymorphism.
C++ supports a mechanism known as virtual function to
achieve run time polymorphism.
At runtime when it is known what class objects are under
consideration, the appropriate version of the function is
invoked.
This requires the use of pointers to objects.
40. Pointers to objects
A pointer can point to an object created by a class.
Syntax:
item * it_ptr ;
Object pointers are useful in creating objects at run time.
We can refer to the member functions of item by using the
arrow operator and the object pointer.
Example:
item x;
item *ptr = &x;
ptr -> getdata(100,75.5);
ptr -> show();
41. Pointers to Derived Classes
Pointers can be used to point both base and derived class
objects.
Pointers to objects of Base class are type compatible with
pointers to objects of a derived class.
Thus, a single pointer can be used to points the objects
belonging to different classes.
If B is a base class and D is a derived class then, a pointer
declared as a pointer to B can also be a pointer to D.
42. Pointers to Derived Classes
Consider the example:
B *ptr;
B b;
D d;
ptr = &b;
ptr = &d;
The pointer ptr can point to both b and d.
Using ptr we can access only the members that are
inherited from B and not the members that belongs to D.
If D has the same name as that of members of B then
any reference to that member by ptr will always access
the base class member.
43. Virtual Functions
When we use the same function name in both the base
and derived classes, the function in base class is
declared as virtual.
When a function is made virtual, C++ determines which
function to use at run time based on the type of object
pointed to by the base pointer, rather than the type of
the pointer.
Example
44. Rules for Virtual Functions
The virtual functions must be members of some class.
They cannot be static members.
They are accessed by using object pointers.
A virtual function can be a friend of another class.
A virtual function in a base class must be defined, even
though it may not be used.
The prototypes of the base class version of a virtual
functions and all the derived class versions must be
identical.
We cannot have virtual constructors, but we can have
virtual destructors.
45. Rules for Virtual Functions
While a base pointer can point to any type of the
derived object, the reverse is not true.
When a base pointer points to a derived class,
incrementing or decrementing it will not make it to
point to the next object of the derived class.
If a virtual function is defined in the base class, it
need not be necessarily redefined in the derived
class.
46. Pure Virtual Functions
Sometimes implementation of all function cannot be
provided in a base class because we don’t know the
implementation.
A pure virtual function (or abstract function) in C++ is
a virtual function for which we don’t have implementation,
we only declare it.
A pure virtual function is declared by assigning 0 in
declaration.
virtual void show() = 0;
47. Summary
The mechanism of deriving a new class from an old class is
called ___________.
A _____ member of class cannot be inherited either in public
mode or in private mode.
A class can be derived from another derived class which is
known as ______ inheritance.
A protected member inherited in the public mode becomes
_______, whereas inherited in private mode becomes private
in the derived class.
A public member inherited in public mode becomes _____,
whereas inherited in private mode becomes _____ in the
derived class.
There are two types of polymorphism namely ________ and
____________.
A virtual function equated to zero is called ______.
48. Short Answer Questions
What are the different forms of inheritance? Give
an example for each.
Different form of inheritance are:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Examples are described in the slides 5 – 9.
49. Short Answer Questions
Describe the syntax of multiple inheritance. When
do we use such inheritance?
When we need to combine the features of several
existing classes for defining new class multiple
inheritance is used.
Syntax:
class D : visibility B1, visibility B2 .....
{
...........
...........
};
50. Short Answer Questions
We know that a private member of a base class is not
inheritable. Is it anyway possible for the objects of a
derived class to access the private members of the
base class? If yes, how?
Private members are not inheritable.
Yes, it is possible to access the private members of base
class through derived class object this is achieved through
the member functions of base class.
51. Short Answer Questions
In what order are the class constructors called when
a derived class object is created?
52. Short Answer Questions
When do we use the protected visibility specifier to
a class member?
In a situation where we want the members to be
accessed by the class in which they are declared and
the class immediately derived from it. We use
protected visibility specifier. The members are not
accessed by the functions outside these two classes.
53. Short Answer Questions
Class D is derived from class B. The class D does not
contain any data members of its own. Does the class
D require constructors? If yes, Why?
The class D will have a constructor with the arguments
for the base class members . The derived class
constructor is used to construct the base class object.
54. Short Answer Questions
What is a Virtual Base Class?
Virtual base classes is a way of preventing multiple
instances of a given class appearing in an inheritance
hierarchy when using multiple inheritance.
What is an Abstract Class?
An abstract class is one that is not used to create
objects. An abstract class is designed only to act as a
base class.
55. Short Answer Questions
Why do we need virtual function?
Virtual Functions are used to support " Run time
Polymorphism".
What is run time polymorphism?
When the virtual function is called by using a Base Class
Pointer, the Compiler decides at Runtime which version of the
function i.e. Base Class version or the overridden Derived
Class version is to be called. This is called Run time
Polymorphism.
56. Short Answer Questions
When do we make a virtual function “pure”? What are
the implications of making a function a pure virtual
function.
When we assign virtual function as zero at the time of its
declaration we create a pure virtual function. It is a function
declared in base class that has no definition relative to the
base class. A pure virtual function is implemented by classes
which are derived from the abstract class.