Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (the derived class or child class) to inherit properties and behaviors from an existing class (the base class or parent class).
This promotes code reusability by letting you create a hierarchy of classes that share common features, avoiding redundant code.
In this type, a single derived class inherits from a single base class.
It’s the simplest form of inheritance, creating a one-to-one relationship between parent and child.

Example:
The car
class inherits from the vehicle
class, meaning a car is a type of vehicle.
It inherits the brand
property and the colour()
method from the vehicle
class.
This involves a chain of inheritance where a derived class inherits from another derived class.

Example:
The hair_style
class inherits from hair_type
, which in turn inherits from hair_texture
.
A derived class can inherit from more than one base class, combining properties from all its parent classes.

Example:
A Specialist
class inherits from hospital
, doctor
, and doc_type
.
In this model, multiple classes are derived from a single base class.

Example:
The Habitat
, Food
, and Behaviour
classes all inherit from the Animals
class.
When a class inherits from a base class, the access specifier (public
, protected
, or private
) used in the inheritance declaration determines how the inherited members can be accessed in the derived class.
👉 Note: Private members of the base class are never directly accessible to any derived class.
public
members remainpublic
.protected
members remainprotected
.
Example:
Harley
inherits publicly from Vehicle
.
- You can access
h.brand
directly because it’spublic
. - You cannot access
h.fuel
because it’sprotected
.
- Both
public
andprotected
members of the base class becomeprotected
in the derived class.
Example:
RoyalEnfield
inherits protectedly from Vehicle
.
brand
andfuel
are nowprotected
inRoyalEnfield
.- They are accessible within the class, but not outside it.
- Both
public
andprotected
members of the base class becomeprivate
in the derived class.
Example:
Jawa
inherits privately from Vehicle
.
brand
andfuel
becomeprivate
inJawa
.- They are accessible only within the
Jawa
class itself.
This repository demonstrates different types of inheritance in C++ with simple examples.
Each experiment highlights how members are inherited and accessed.
vehicle
:brand="Ford"
, methodcolour() → "Red!"
car
: inheritsvehicle
, addsmodel="Mustang"
main()
: createmycar
, printbrand
,model
, callcolour()
hospital
:nearby="Jupiter"
doctor
:doc="Dr.Sharma"
doc_type
:type="Cardiologist"
Specialist
: inherits all threemain()
: createsp
, printnearby
,doc
,type
hair_texture
:texture="Rough"
hair_type
: inheritshair_texture
, addstype="Straight"
hair_style
: inheritshair_type
, addsstyle="Layers"
main()
: createhair
, printtexture
,type
,style
Animals
:animal_1="Tiger"
,animal_2="Dolphin"
,animal_3="Crocodile"
Habitat
,Food
,Behaviour
: each inheritAnimals
and add detailsmain()
: createh
,f
,b
, print combined info for each animal
Vehicle
:brand
(public),fuel
(protected),year
(private)Harley
(public inheritance): can accessbrand
&fuel
RoyalEnfield
(protected inheritance):brand
&fuel
→ protectedJawa
(private inheritance):brand
&fuel
→ privatemain()
: test access for each class
File | Type of Inheritance | Description |
---|---|---|
exp14a.cc | Single Inheritance | A car class inherits from a single vehicle class, demonstrating a basic "is-a" relationship (a car is a vehicle). |
exp14b.cc | Multiple Inheritance | A Specialist class inherits from three separate base classes (hospital , doctor , and doc_type ), combining their properties into a single child class. |
exp14c.cc | Multilevel Inheritance | A hierarchical chain is created, with hair_style inheriting from hair_type , which in turn inherits from hair_texture . |
exp14d.cc | Hierarchical Inheritance | Multiple classes (Habitat , Food , and Behaviour ) inherit from a single base class (Animals ). |
exp14e.cc | Access Modes | Demonstrates the different access specifiers (public, protected, private) in inheritance and how they affect member accessibility. |