0% found this document useful (0 votes)
40 views

Institute - Uie Department-Academic Unit-1

This document provides an overview of inheritance in object-oriented programming using C++. It defines inheritance as creating a new child class from an existing parent class. The key topics covered include the advantages of inheritance, implementing inheritance in C++ syntax, the different modes and types of inheritance such as single, multiple, multilevel, hierarchical and hybrid inheritance. It also discusses function overriding and how to resolve ambiguity that can occur in hybrid inheritance.

Uploaded by

Lovey Preet
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)
40 views

Institute - Uie Department-Academic Unit-1

This document provides an overview of inheritance in object-oriented programming using C++. It defines inheritance as creating a new child class from an existing parent class. The key topics covered include the advantages of inheritance, implementing inheritance in C++ syntax, the different modes and types of inheritance such as single, multiple, multilevel, hierarchical and hybrid inheritance. It also discusses function overriding and how to resolve ambiguity that can occur in hybrid inheritance.

Uploaded by

Lovey Preet
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/ 45

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-1


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Object-oriented Programming using
C++
CSP-152
Prepared by Ms. Jasleen Kaur
Inheritance DISCOVER . LEARN . EMPOWER
CHAPTER -4
Course Outcome
CO Title Level
Number
CO1 provide the environment that allows students to Remember
understand object-oriented programming Concepts.  
CO2 demonstrate basic experimental skills for Understand Will be covered in this
differentiating between object-oriented and  
procedural programming paradigms and the lecture
advantages of object-oriented programs.

CO3 Ability to demonstrate their coding skill on complex Understand


programming concepts and use it for generating
solutions for engineering and mathematical problems.
CO4 Students will develop skills to design the application of Understand
classes, objects, constructors, destructors, inheritance,  
operator overloading and polymorphism, pointers,
virtual functions, templates, exception handling, file
operations and handling

2
• Defining derived class,
• modes of inheritance,
• types of inheritance,
CONTENTS • ambiguity in inheritance,
• virtual base class,
• Function overriding,
The capability of a class to derive properties and • Member Classes: Nesting of Classes.
characteristics from another class is called
Inheritance.
Inheritance is one of the most important feature
of Object Oriented Programming

3
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class
is called as Child or Derived Class.
Advantages
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without modifying it derive other classes from it that
are suited to particular situations
• Improve Program Reliability
• It Permits code sharing
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without modifying it derive other classes from it that
are suited to particular situations
• Improve Program Reliability
• It Permits code sharing
• The base class need not be changed but can be adapted to suit the requirements in different applications.

4
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class
is called as Child or Derived Class.

Base/Sub Class Derived/Super class


Methods Base class methods
and +
Properties Additional methods

5
WHY AND WHEN TO USE
INHERITANCE?
• Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(),
applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write
all of these functions in each of the three classes as shown in below figure:

Figure 4.1 Need of inheritance [1]

6
WHY AND WHEN TO USE
INHERITANCE?
• If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then
we can simply avoid the duplication of data and increase re-usability

Figure 4.2 Need of inheritance [1]

7
IMPLEMENTING INHERITANCE IN C++
• For creating a sub-class which is inherited from the base class we have to follow the below syntax.
• Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
• Here, subclass_name is the name of the sub class, access_mode is the mode in which you want to inherit this sub class for
example: public, private etc. and base_class_name is the name of the base class from which you want to inherit the sub
class.

8
IMPLEMENTING INHERITANCE IN C++
• EXAMPLE

Figure 4.3 Example inheritance made on Dev C++ 9


IMPLEMENTING INHERITANCE IN C++
• EXAMPLE

Figure 4.4 Example inheritance made on Dev C++ 10


MODES OF INHERITANCE
• Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public
in the derived class and protected members of the base class will become protected in derived class.
• Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of
the base class will become protected in derived class.
• Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the
base class will become Private in derived class.

Figure 4.5 Modes of inheritance[1] 11


TYPES OF INHERITANCE

1. SINGLE INHERITANCE

3. MULTI-LEVEL INHERITANCE

2. MULTIPLE INHERITANCE

Figure 4.6 Types of inheritance[1] 12


TYPES OF INHERITANCE

5. HYBRID/VIRTUAL INHERITANCE

4. HIERARCHICAL INHERITANCE

Figure 4.7 Types of inheritance[1] 13


SINGLE INHERITANCE
CLASS

Figure 4.8 Program made on Dev-C++


14
MULTIPLE INHERITANCE
CLASS

15
Figure 4.8 Program made on Dev-C++
MULTIPLE INHERITANCE
CLASS

16
Figure 4.8 Program made on Dev-C++
MULTILEVEL INHERITANCE

Figure 4.9 Program made on Dev-C++ 17


MULTILEVEL INHERITANCE

Figure 4.10 Program made on Dev-C++ 18


MULTILEVEL INHERITANCE

Figure 4.10 Program made on Dev-C++ 19


HIERARCHICAL INHERITANCE

Figure 4.11 Program made on Dev-C++ 20


HIERARCHICAL INHERITANCE

Figure 4.12 Program made on Dev-C++ 21


HIERARCHICAL INHERITANCE

Figure 4.13 Program made on Dev-C++ 22


HYBRID INHERITANCE

Figure 4.14 Program made on Dev-C++ 23


HYBRID INHERITANCE

Figure 4.15 Program made on Dev-C++ 24


HYBRID INHERITANCE

Figure 4.16 Program made on Dev-C++ 25


FUNCTION OVERRIDING
If the derived class has the function with same name as the function in the base class, then it will hide the function of base
class.
If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived
class, then that function is said to be overridden, and this mechanism is called Function Overriding
REQUIREMENTS FOR OVERRIDING-
• Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a
base class.
• Function that is redefined must have exactly the same declaration in both base and derived class, that means same name,
same return type and same parameter list.

26
FUNCTION OVERRIDING

Figure 4.17 Program made on Dev-C++ 27


FUNCTION OVERRIDING- SOLUTION

Figure 4.18 Program made on Dev-C++


28
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.19 Program made on Dev-C++


29
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.20 Program made on Dev-C++


30
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.21 Program made on Dev-C++


31
OUTPUT

Figure 4.22 Program output made on Dev-C++


32
SOLUTION- VIRTUAL BASE CLASS
• When two or more objects are derived from a common base class, we can prevent multiple copies of the
base class being present in an object derived from those objects by declaring the base class as virtual when
it is being inherited. Such a base class is known as virtual base class.
• This can be achieved by preceding the base class’ name with the word virtual.
• Virtual base classes are created before non-virtual base classes, which ensures all bases get created before
their derived classes.
• Suppose you have two derived classes B and C that have a common base class A,
• You also have another class D that inherits from B and C.
• You can declare the base class A as virtual to ensure that B and C share the same sub-object of A.

33
SOLUTION- VIRTUAL BASE CLASS

Figure 4.23 Program 4 made on Dev-C++


34
OUTPUT

Figure 4.24 Program output made on Dev-C++

35
NESTING OF CLASSES
• A declaration of a class/struct or union may appear in within another class. Such declaration declares a nested class[4].
• A nested class is a member and as such has the same access rights as any other member.
• The name of the nested class exists in the scope of the enclosing class, and name lookup from a member function of a
nested class visits the scope of the enclosing class after examining the scope of the nested class.
• Like any member of its enclosing class, the nested class has access to all names (private, protected, etc) to which the
enclosing class has access, but it is otherwise independent and has no special access to the this pointer of the enclosing
class.
• Nested classes can be forward-declared and later defined, either within the same enclosing class body, or outside of it[4]:
class enclose {
class nested1; // forward declaration
class nested2; // forward declaration
class nested1 {}; // definition of nested class
};
class enclose::nested2 { }; // definition of nested class

36
NESTING OF CLASSES
• They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and
create more readable and maintainable code.

37
NESTING OF CLASSES-EXAMPLE 1
• A nested class is a class which is declared in another enclosing class.
• A nested class is a member and as such has the same access rights as any other member.
• The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be
obeyed.

Figure 4.25 Program made on Dev-C++


38
OUTPUT

Figure 4.26 Program OUTPUT made on Dev-C++


39
NESTING OF CLASSES-EXAMPLE 2
• A nested class is a class which is declared in another enclosing class.

Figure 4.27 Program made on Dev-C++


40
NESTING OF CLASSES-EXAMPLE 2

Figure 4.28 Program made on Dev-C++


41
Assessment Pattern
Section-A
1. (a) Define ambiguity in inheritance.
(b) What are modes of inheritance?
(c) What is nesting of classes?
Section-B
2. Write a C++ program to demonstrate hierarchical inheritance.
3. Can we call private constructor? If yes , then how?
4. Write a C++ program to differentiate between three modes of inheritance.

42
APPLICATIONS
• You use nested classes to reflect and to enforce the relationship between two classes. You should define a class within
another class when the nested class makes sense only in the context of its enclosing class or when it relies on the enclosing
class for its function. For example, a text cursor might make sense only in the context of a text component.

43
REFERENCES

• Reference Website
[1] https://www.geeksforgeeks.org/inheritance-in-c/
[2] https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
[4] https://en.cppreference.com/w/cpp/language/nested_types

44
THANK YOU

For queries
Email: [email protected]

You might also like