0% found this document useful (0 votes)
12 views19 pages

WINSEM2024-25

The document explains the concept of inheritance in C++, which allows the creation of new classes based on existing ones, facilitating code reuse and easier maintenance. It details the types of inheritance, including single and multiple inheritance, and discusses access control for derived classes. Additionally, it covers ambiguity issues that can arise in multiple inheritance and how to resolve them using scope resolution operators.

Uploaded by

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

WINSEM2024-25

The document explains the concept of inheritance in C++, which allows the creation of new classes based on existing ones, facilitating code reuse and easier maintenance. It details the types of inheritance, including single and multiple inheritance, and discusses access control for derived classes. Additionally, it covers ambiguity issues that can arise in multiple inheritance and how to resolve them using scope resolution operators.

Uploaded by

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

C++ Inheritance

◼ One of the most important concepts in object-oriented


programming is that of inheritance.
◼ Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an
application.
◼ This also provides an opportunity to reuse the code
functionality and fast implementation time.
◼ Inheritance is a feature or a process in which, new classes
are created from the existing classes.
◼ The new class created is called “derived class” or “child
class” and the existing class is known as the “base class” or
“parent class”.
◼ The derived class now is said to be inherited from the base
class.

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.

Access public protected private


Same class yes yes yes

Derived yes yes no


classes
Outside yes no no
classes

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.

➢ Overloaded operators of the base class.

➢ The friend functions 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.

Protected Inheritance − When deriving from a protected base class,


public and protected members of the base class become protected
members of the derived class.

Private Inheritance − When deriving from a private base class, public


and protected members of the base class become private members of
the derived 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; {

// first base class };


class Vehicle {
public: // main function
Vehicle() int main()
{ {
cout << "This is a Vehicle\n"; // Creating object of sub class will
} // invoke the constructor of base classes.
}; Car obj;
return 0;
// second base class }
class FourWheeler {
public:
FourWheeler() Output
{ This is a Vehicle
cout << "This is a 4 wheeler This is a 4 wheeler
Vehicle\n"; Vehicle
}
};
Program to demonstrate the Ambiguity Problem in
Multiple Inheritance
#include <iostream>
#include <conio.h>
using namespace std;
// create class A
class A
{
public:
void show()
{
cout << " It is the member function of class A " << endl;
}
}; // create class B
class B
{
public:
void show()
{
cout << " It is the member function of class B " << endl;
}
3/13/2025 }; S.Rohini(18985) 17
// create a child class to inherit the member function of class A and class B

class child: public A, public B


{
public:
void disp()
{
cout << " It is the member function of the child class " << endl;
}
};

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,

ch.A:: show(); // class_name and scope reso


lution operator with member function
ch.B::show();

It is the member function of the child class

It is the member function of class A


It is the member function of class B

3/13/2025 S.Rohini(18985) 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy