Inheritance in C++ - GeeksforGeeks
Inheritance in C++ - GeeksforGeeks
Inheritance in C++
Last Updated : 16 Jan, 2025
where,
https://www.geeksforgeeks.org/inheritance-in-c/ 1/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Example:
// main function
https://www.geeksforgeeks.org/inheritance-in-c/ 2/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
int main()
{
// creating a child class object
Child obj1;
return 0;
}
Output
Base ID: 7
Child ID: 91
In the above program, the ‘Child’ class is publicly inherited from the
‘Parent’ class so the public data members of the class ‘Parent’ will also
be inherited by the class ‘Child’.
// Base class
class Base {
public:
// data member
int publicVar;
// member method
void display()
{
cout << "Value of publicVar: " << publicVar;
}
};
// Derived class
class Derived : public Base {
https://www.geeksforgeeks.org/inheritance-in-c/ 3/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
public:
// Function to display inherited member
void displayMember()
{
// accessing public base class member method
display();
}
int main()
{
// Create an object of Derived class
Derived obj;
return 0;
}
Output
Value of publicVar: 10
Public Mode
Protected Mode
Private Mode
Example:
Example:
Private mode is the default mode that is applied when we don’t specify
any mode.
Example:
The below table summarizes the above three modes and shows the
access specifier of the members of the base class in the subclass when
derived in public, protected and private modes:
protected:
int y;
private:
int z;
};
class B : public A {
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A {
// x is protected
// y is protected
// z is not accessible from C
};
https://www.geeksforgeeks.org/inheritance-in-c/ 6/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
// Base class
class Base {
private:
int privateVar;
public:
// Constructor to initialize privateVar
Base(int val): privateVar(val){}
// Derived class
class Derived : public Base {
public:
// Constructor to initialize the base class
Derived(int val) : Base(val){}
int main()
{
// Create an object of Derived class
Derived obj(10);
return 0;
}
Output
The above program shows the method in which the private members of
the base class remain encapsulated and are only accessible through
controlled public or protected member functions.
We can also access the private members of the base class by declaring
the derived class as friend class in the base class.
// Base class
class Base {
private:
int privateVar;
public:
// Constructor to initialize privateVar
Base(int val)
: privateVar(val)
{
}
https://www.geeksforgeeks.org/inheritance-in-c/ 8/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
// Declare Derived class as a friend
friend class Derived;
};
// Derived class
class Derived {
public:
// Function to display the private member of the base
// class
void displayPrivateVar(Base& obj)
{
// Accessing privateVar directly since Derived is a
// friend of Base
cout << "Value of privateVar in Base class: "
<< obj.privateVar << endl;
}
int main()
{
// Create an object of Base class
Base baseObj(10);
return 0;
}
Output
To know more about access modes in a C++ class, refer to the article –
Access Modifiers in C++
https://www.geeksforgeeks.org/inheritance-in-c/ 9/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e.
one base class is inherited by one derived class only.
Syntax
Example:
Single Inheritance
class A
{
https://www.geeksforgeeks.org/inheritance-in-c/ 10/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
... .. ...
};
class B: public A
{
... .. ...
};
Implementation:
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
This Vehicle is Car
2. Multiple Inheritance
Syntax
https://www.geeksforgeeks.org/inheritance-in-c/ 11/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Here, the number of base classes will be separated by a comma (‘, ‘) and
the access mode for every base class must be specified and can be
different.
Example:
class A
{
... .. ...
};
class B
{
... .. ...
};
class C: public A, public B
{
... ... ...
};
Implementation:
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 Wheeler
This 4 Wheeler Vehical is a Car
3. Multilevel Inheritance
Syntax
https://www.geeksforgeeks.org/inheritance-in-c/ 13/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Example:
class C
{
... .. ...
};
class B : public C
{
... .. ...
};
class A: public B
{
... ... ...
};
Implementation
// base class
class Vehicle {
public:
https://www.geeksforgeeks.org/inheritance-in-c/ 14/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehical is a Car
4. Hierarchical Inheritance
Syntax
https://www.geeksforgeeks.org/inheritance-in-c/ 15/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
... .. ...
}
Example:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Implementation
// base class
class Vehicle {
https://www.geeksforgeeks.org/inheritance-in-c/ 16/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus
5. Hybrid Inheritance
Example:
https://www.geeksforgeeks.org/inheritance-in-c/ 17/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
class F
{
... .. ...
}
class G
{
... .. ...
}
class B : public F
{
... .. ...
}
class E : public F, public G
{
... .. ...
}
class A : public B {
... .. ...
}
class C : public B {
... .. ...
}
Implementation:
https://www.geeksforgeeks.org/inheritance-in-c/ 18/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare
https://www.geeksforgeeks.org/inheritance-in-c/ 19/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Example
#include <iostream>
using namespace std;
C++ C++base
// Classes and Objects C++ Polymorphism C++ Inheritance
class C++ Abstraction C++ Encapsulatio
class Parent {
public:
// base class constructor
Parent() { cout << "Inside base class" << endl; }
};
// sub class
class Child : public Parent {
public:
// sub class constructor
Child() { cout << "Inside sub class" << endl; }
};
// main function
int main()
{
return 0;
}
https://www.geeksforgeeks.org/inheritance-in-c/ 20/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Output
Polymorphism in Inheritance
In Inheritance, we can redefine the base class member function in the
derived class. This type of inheritance is called Function Overriding.
Generally, in other programming languages, function overriding is
runtime polymorphism but in C++, we can do it at both runtime and
complile time. For runtime polymorphism, we have to use the virtual
functions.
Example
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
https://www.geeksforgeeks.org/inheritance-in-c/ 21/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Output
Derived Function
https://www.geeksforgeeks.org/inheritance-in-c/ 22/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
your skills. Take the Three 90 Challenge: finish 90% in 90 days for a
90% refund. Start now and level up your C++ programming!
Similar Reads
Difference between Single and Multiple Inheritance in C++
Single InheritanceSingle inheritance is one in which the derived class
inherits the single base class either public, private, or protected access…
4 min read
https://www.geeksforgeeks.org/inheritance-in-c/ 23/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
https://www.geeksforgeeks.org/inheritance-in-c/ 24/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Privacy Policy GfG Weekly Contest
Careers Offline Classes (Delhi/NCR)
In Media DSA in JAVA/C++
Contact Us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial
https://www.geeksforgeeks.org/inheritance-in-c/ 25/27
2/23/25, 8:08 PM Inheritance in C++ - GeeksforGeeks
DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
Core CS Subject for Interview Preparation JAVA Backend Development - [LIVE]
Mastering System Design: LLD to HLD Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]
Clouds/Devops GATE
DevOps Engineering GATE CS & IT Test Series - 2025
AWS Solutions Architect Certification GATE DA Test Series 2025
Salesforce Certified Administrator Course GATE CS & IT Course - 2025
GATE DA Course 2025
GATE Rank Predictor
https://www.geeksforgeeks.org/inheritance-in-c/ 27/27