WINSEM2024-25
WINSEM2024-25
3/13/2025 S.Rohini(18985) 2
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 the same for all 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 below figure:
3/13/2025 S.Rohini(18985) 3
3/13/2025 S.Rohini(18985) 4
◼ Implementing inheritance in C++: For creating a sub-class
that is inherited from the base class we have to follow the
below syntax.
◼ Derived Classes: A Derived class is defined as the class
derived from the base class.
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{ //body }
Where
class — keyword to create a new class
derived_class_name — name of the new class, which will
inherit the base class
access-specifier — either of private, public or protected. If
neither is specified, PRIVATE is taken as default
base-class-name — name of the base class
3/13/2025 S.Rohini(18985) 5
Access Control and Inheritance
◼ A derived class can access all the non-private members of its
base class. Thus base-class members that should not be
accessible to the member functions of derived classes should
be declared private in the base class.
3/13/2025 S.Rohini(18985) 6
◼ A derived class inherits all base class methods
with the following exceptions −
➢ Constructors, destructors and copy constructors of the
base class.
3/13/2025 S.Rohini(18985) 7
Type of Inheritance is specified by the access-
specifier
Public Inheritance − When deriving a class from a public base class,
public members of the base class become public members of the
derived class and protected members of the base class become
protected members of the derived class. A base class's private
members are never accessible directly from a derived class, but can
be accessed through calls to the public and protected members of the
base class.
3/13/2025 S.Rohini(18985) 8
3/13/2025 S.Rohini(18985) 9
C++ Single Inheritance
◼ If a single class is derived from one base class then it is
called single inheritance. In C++ single inheritance base
and derived class exhibit one to one relation.
3/13/2025 S.Rohini(18985) 10
C++ Programming Single Inheritance Syntax
class base_class
{
//code
};
class derived_class : public(access_modifier)
base_class
{
//code
};
int main()
{
base_class object_1;
derived_class object_2;
//code
}
3/13/2025 S.Rohini(18985) 11
class Parent
{
public:
int id_p;
};
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{ Output :
public:
int id_c; Child id is 7
}; Parent id is 91
int main()
{
Child obj1;
// An object of class child has all data members
// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';
return 0;
}
Multiple Inheritance
It is the process of creating new class from more than one
base classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
Person Employee
// members;
public :
//memebers;
};
Teacher
// C++ program to explain // sub class derived from two base classes
// multiple inheritance class Car : public Vehicle, public
#include<iostream> FourWheeler
using namespace std; {
int main ()
{
// create an object of the child class to access the member function
child ch;
ch.show(); // It causes ambiguity
ch.disp();
return 0;
}
3/13/2025 S.Rohini(18985) 18
Syntax of the Ambiguity Resolution
Derived_obj_name.parent_class_name : : same_named_m
emberFunction ( [parameter] );
For example,
3/13/2025 S.Rohini(18985) 19