0% found this document useful (0 votes)
38 views67 pages

Inheritance 1

oops concept notes

Uploaded by

trippin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views67 pages

Inheritance 1

oops concept notes

Uploaded by

trippin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

UNIT - 4

Syllabus
Inheritance: Defining derived classes, Single Inheritance, Making a
private member inheritable, Multilevel, Multiple inheritance, Hierarchical
inheritance, Hybrid inheritance, Virtual base classes, Abstract classes,
Constructors in derived classes, Member classes - Nesting of classes.

Pointers, Virtual Functions, and Polymorphism: Pointer to objects, this


pointer, polymorphism, Pointer to derived Class -Virtual functions – Pure
Virtual Functions, virtual constructors, and destructors
INHERITANCE
INHERITANCE
 The process by which new classes (Derived classes) are created from existing
classes (Base classes).
 The derived classes have all the features of the base class and the programmer can
add new features specific to the newly created derived class.
 Inheritance implements is a relationship.
Ex: Mammal IS-A animal, dog IS-A Mammal hence dog IS-A animal.
INHERITANCE
Features or Advantages of Inheritance
 Reusability of code
 Saves time and effort
 Faster development, easier maintenance and easy to extend.
 Capable of expressing the inheritance relationship and its transitive nature which
ensures closeness with real world problems.
Ex: Insect Taxonomy
INHERITANCE
The "is a" Relationship
Inheritance establishes an "is a" relationship between classes.
A poodle is a dog
A car is a vehicle
A flower is a plant
A football player is an athlete

Syntax to create a derived class from an existing class:


class derived-class : access-specifier base-class
{
Body of the class;
}
where access-specifier can be public, protected or private. 5
INHERITANCE
 Access Modes – The various ways of
deriving classes.
 Access modes have the following
effect:
1. Public: If a derived class is declared in
public mode, then the members of the
base class are inherited by the derived
class.
2. Private: All the members of the base
class become private members in the
derived class.
3. Protected: The public members of the
base class becomes protected members
in the derived class.
Effects of using different Inheritance Specifiers
 The private members of the base class
are always private in the derived class.
INHERITANCE
Inheritance v/s Access
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z

private: x protected x is inaccessible


protected: y base class protected: y
public: z protected: z

private: x public x is inaccessible


protected: y base class protected: y
public: z public: z
INHERITANCE
Types of inheritance
INHERITANCE
Types of inheritance
INHERITANCE
1. Single Inheritance:
Derived class inherits only from only one base class.

2. Multiple Inheritance:
Derived class inherits only from multiple base classes.

3. Hierarchical Inheritance:
More than one derived are created from a single class base classes.

4. Multilevel Inheritance:
When a class is derived from a class which is a derived class.

5. Hybrid Inheritance:
Combination of more than one type of inheritance.

6. Multipath Inheritance:
Derivation of a class from other derived classes, which are derived from the same base class.
SINGLE INHERITANCE
Inheritance in which the derived class is derived from only one base class.
Ex: Program shows a base class B and a derived class D.
The class B contains one private data member, one public data member and three
public member functions.
The class D contains one private data member and two public member functions.
Single Inheritance – Public Inheritance
Single Inheritance – Public Inheritance
Single Inheritance – Public Inheritance
Single Inheritance – Public Inheritance
 Class D is a public derivation of the base class B.
 D inherits all the public members of B and retains
their visibility.
 A public member of the base class is also a public
member of the derived class D.
 The private members of B cannot be inherited by
D.
 Class D have more members than what it
contains at the time of declaration.
 Data member a is private in B and cannot be
inherited, objects of D are able to access it
through an inherited member function of B
Single Inheritance – Private Inheritance

 The public members of the base class become


private members of the derived class.
 The objects of D can not have access to the
public member functions of B.
Single Inheritance – Private Inheritance
The statements d.get_ab();
d.get_a();
d.show_a(); will not work.
These functions can be used inside mul() and display().
Single Inheritance – Private Inheritance
Making a Private Member Inheritance
 The private data can be inherited by a derived class by modifying the visibility
limit i.e making private member to public.
 C++ provides a third visibility modifier protected.
 Protected member is accessible by the member functions within its class
immediately derived from it.
 It cannot be accessed by the function outside these two classes.
 When a protected member is inherited in public mode, it becomes protected in
the derived class & is accessible by the member functions of the derived class,
available for further inheritance.
 When a protected member is inherited in private mode, it becomes private in the
derived class & not available for further inheritance.
Making a Private Member Inheritance
Making a Private Member Inheritance
 The keywords private, protected and public may appear in any order and any
number of times in the declaration of a class.
class beta
{
protected: ………
public: ………
private: ………
public: ………
}
 Normal practice is
class beta
{
……… //Private by default
private: ………
public: ………
}
Making a Private Member Inheritance
 Various functions that can have
access to the private and protected
members of a class:
1. A function that is a friend of the
class.
2. A member function of a class that is
a friend of the class.
3. A member function of a derived
class.
 Friend and member functions of a
friend class can have direct access to
both the private and protected data,
the member function of a derived
class can directly access only the
protected data.
 They can access the private data
through the member functions of the
MULTI LEVEL INHERITANCE
The mechanism of deriving a
class from another derived
class.
Here 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.
Class B is known as
Intermediate base class – it
provides a link for the
inheritance between A & C.
The chain A -> B -> C is
known as Inheritance path.
Declaration with Multilevel Inheritance
MULTI LEVEL INHERITANCE
Ex: The test results of a batch of students are stored in three different classes.
Class student stores the 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 the marks obtained in the test and the roll number
of the students through multilevel inheritance.
MULTI LEVEL INHERITANCE
The class result after inheritance from class student through class test would
contain the following members and member functions:
private:
float total; //Own data member
protected:
int roll_number; //Inherited from student via test
float sub1, sub2; //Inherited from test
public:
void get_number(int); //Inherited from student via test
void put_number (void); //Inherited from student via test
void get_marks(float x, float y) //Inherited from test
void put_marks(void) //Inherited from test
void display(); //Own data member
MULTI LEVEL INHERITANCE
MULTI LEVEL INHERITANCE
MULTIPLE INHERITANCE
A derived class is derived from several base class – Multiple Inheritance.
A class can inherit the attributes of two or more classes - Multiple Inheritance.
It combines the features of several existing classes as a string point for defining
new classes..
Syntax:
class C : visibility A, visibility B, .….
{
……… //Body of C
}
Base classes are separated by comma.
MULTIPLE INHERITANCE
MULTIPLE INHERITANCE
MULTIPLE INHERITANCE
Ambiguity Resolution in Inheritance
When a function with the same name appears in more than one base class.
class M
{
public:
void display(void) { cout << “ Class M \n” ; }
};
class N
{
public:
void display(void) { cout << “ Class N \n” ; }
};
Which display() function is used by the derived function.
Ambiguity Resolution in Inheritance
Ambiguity Resolution in Inheritance
 Derived class C inherited the two base classes A and B having same function
name func().
 When the object of class C is created and called the function func() then the
complier gets confused that which base class member function func() should
be called.

 Solution: Scope resolution operator is used denoted by “::”.


Syntax: ObjectName.ClassName :: FunctionName();
Ambiguity Resolution in Inheritance
HIERARCHICAL INHERITANCE
More than one sub-class inherits the property of a single base class.
There is one base class and multiple derived classes.
Several other classes inherit the derived classes as well.
Hierarchical structures thus form a tree-like structure.
Ex: Mango and apple both are fruits; both inherit the property of fruit. Fruit will be
the Base class, and mango and apple are sub-classes.
Class A is a base class. B is a subclass
inherited from class A and C is also a subclass
inherited from class A.
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
Advantages of Hierarchical Inheritance
1. It allows for code reuse and refactoring, as the same code can be reused in
multiple classes.
2. It makes easier to extend the code, as the same code can be used to create
different classes that all have access to the same members.
3. It helps to create a more organized and efficient code, as all the members of the
base classes are available to the derived classes.
4. It allows a single member to be used in multiple derived classes without having to
rewrite the code multiple times.
5. It creates a clear hierarchy of classes, making debugging and maintaining the
code easier.
HIERARCHICAL INHERITANCE
Disadvantages of Hierarchical Inheritance
1.It can lead to a lot of code duplication, as the derived classes will have to
duplicate the code from the base class to use the same methods.
2.It can create a rigid hierarchy of classes, making it difficult to extend the code in
the future without having to rewrite lots of code.
3.It can be difficult to debug and maintain, as the same errors can occur in multiple
classes.
4.It can lead to tight coupling between classes, making it easier to make changes to
the code by breaking other parts of the code.
5.Changes to parent classes may have unexpected effects on unrelated child classes
leading to increased complexity and debugging time.
HIERARCHICAL INHERITANCE
An educational institution wishes to
maintain a database of its employees. The
database is divided into several classes
whose hierarchical relationships are shown
in following figure. The figure also shows
the minimum information required for each
class. Specify all classes and define
functions to create the database and retrieve
individual information as and when required.
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
HYBRID INHERITANCE
Hybrid inheritance is a combination of simple, multiple inheritance and
hierarchical inheritance.
In multiple inheritances, a class is derived from two classes where one of the
parent classes is also a derived class and not a base class.
A class is derived from more than one form or combination of any inheritance.
The hybrid inheritance in C++ is also called multipath inheritance, where one
derived class can inherit properties of the base class in different paths.
Multipath inheritance - it can be achieved with a combination of both multilevel
and hierarchical inheritance.
Hybrid inheritance is a combination of two or more types of inheritance.
Class B is derived from class A – Single Inheritance.
Class D is inherited from Multiple classes –
Multiple Inheritance.
Mix of Single Inheritance & Multiple Inheritance form
HYBRID INHERITANCE
A class ‘test’ is derived from class ‘student’ by single level inheritance. Another class
‘sports’ is created, and a class ‘result’ is derived from both ‘test’ and ‘sports’ classes by
multiple inheritances. The class ‘result’ is derived using hybrid inheritance.
HYBRID INHERITANCE
MULTIPATH INHERITANCE
Multipath Inheritance - derivation of a class from other derived classes, which
are derived from the same base class.
Involves other inheritance like multiple, multilevel, hierarchical etc.
Class D is derived from class B & C.
Class B & C are child of class A.
Class D is indirectly derived from class A.
VIRTUAL BASE CLASS
Three kinds of inheritance namely
multilevel, multiple, and hierarchical
inheritance are involved.
The child class has two direct base
‘parent 1 and parent 2’ which
themselves have a common base class ‘grandparents’.
The ‘child’ inherits the traits of ‘grandparent’ via two separate paths - shown by
the broken line.
The ‘grandparent’ - indirect base class.
All the public and protected members of grandparent are inherited into child
twice, first via parent1, and again via parent2.
Child would have duplicate sets of the members inherited from grandparent.
This introduces ambiguity.
The duplication of inherited members due to these multiple paths can be
avoided by making the common base class as virtual base class
VIRTUAL BASE CLASS
VIRTUAL BASE CLASS
VIRTUAL BASE CLASS
Abstract Class
An abstract class is designed to act as a base class (to be inherited by other
objects).
Contains at least one pure virtual function.
An abstract base class is one that is not used to create objects.
It is a designed concept in program development and provides a base upon which
other class may be built.
Pure virtual function
A pure virtual function (or
abstract function) in C++ is a
virtual function for which there is
an implementation,
Override that function in the
derived class, otherwise, the
derived class will also become an
abstract class.
A pure virtual function is declared
by assigning 0 in the declaration.
Constructor in derived class
If a base class constructor takes no arguments, the derived class need not to have
constructor function.
If any base class contain constructor with one or more arguments, then it is
mandatory for the derived class to have a constructor and pass the arguments to
the base class constructor.
When both the derived and base classes contain constructor, the base class
constructor is executed first and then the constructor in the derived class is
executed.
Order of Constructors and Destructors in derived classes
Derived class constructor
Calls the constructor for its base class first to initialise its base class members
If the derived class constructor is omitted, it’s a default constructor calls the base-
class default constructor.
Destructors are called in the reverse order of constructor calls
Derived class destructor is called before its base class destructor.
Constructor in derived class
The general form of defining a derived constructor is:
Der_constructor(par_list) : base1(par_list1), base2(par_list2)
{
//Implementation of derived class constructor
}
Der_constructor – name of the derived class constructor
par_list specifies the list of parameters that the derived class constructor will
receive.
Some are used to initialize its own data members whereas remaining may be passed
to its base class constructors.
Initialization list starts with colon(:) and consists of calls to base class construtors
where each call is separated by comma.
Constructor in derived class
Ex: Derived class object created as:
D objD(5, 12,2.5, 7.54, 30)

Definition of constructor called:


D(int a1, int a2, float b1, float b2, int d1):
A(a1, a2), /* Call to constructor A*/
B(b1, b2) /* Call to constructor A*/
{
D = d1; //Executes its own body
}
Constructor in derived class
Order of Constructors in derived classes
Constructor in derived class
When to use initializer lists
Using initialization lists to initialize data members in a constructor can be
convenient if no need of any error checking constructor arguments.
Data members that are const but not static must be initialization list.
Data members that are references must be initialized using an initialization list.
An initialization list can be used to explicitly call a constructor that takes
arguments for a data member that is an object of another class.
In a derived class constructor, an initialization list can be used to explicitly call a
base class constructor that takes arguments.
Constructor in derived class
Constructor in derived class
Constructor in derived class
constructor (arglist) : initialization - section
{
assignment-section
}
Initializer list is used to initialize data members.
The syntax begins with a colon(:) and then each variable along with its value
separated by a comma.
The initializer list does not end in a semicolon.
Constructor in derived class
Constructor in derived class
Member classes : Nesting of class
 A nested class is a
class which is
declared in another
class.
 A nested class is a
member and as such
has the same access
rights as any other
member.
 The members of an
encoding class have
no special access to
members of a nested
class; the usual
access rules shall be
obeyed.
Member classes : Nesting of class

You might also like