Institute - Uie Department-Academic Unit-1
Institute - Uie Department-Academic Unit-1
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.
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:
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
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
1. SINGLE INHERITANCE
3. MULTI-LEVEL INHERITANCE
2. MULTIPLE INHERITANCE
5. HYBRID/VIRTUAL INHERITANCE
4. HIERARCHICAL INHERITANCE
15
Figure 4.8 Program made on Dev-C++
MULTIPLE INHERITANCE
CLASS
16
Figure 4.8 Program made on Dev-C++
MULTILEVEL INHERITANCE
26
FUNCTION OVERRIDING
33
SOLUTION- VIRTUAL BASE CLASS
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.
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]