codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

48 / 87 topics
48Inheritance Basics49Public, Protected, and Private Inheritance50Types of Inheritance51Polymorphism52Function Overriding53Virtual Functions54Abstract Class and Pure Virtual Function
Tutorials/C++ Programming/Inheritance Basics
⚡C++ Programming

Inheritance Basics

Updated 2026-05-12
30 min read

Inheritance Basics

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create a new class, known as a derived class or subclass, from an existing class, called the base class or superclass. This relationship forms what is commonly referred to as an "is-a" relationship, where the derived class inherits properties and behaviors from the base class.

Understanding inheritance is crucial because it promotes code reusability, hierarchical classification of classes, and supports polymorphism, which is a core feature of OOP that enables objects to be treated as instances of their parent class. In this tutorial, we will explore the basics of inheritance in C++, including how to define base and derived classes, the syntax for inheritance, and the order of constructor and destructor calls.

Core Content

Base Class and Derived Class

A base class is a class from which other classes are derived. It contains properties and methods that can be inherited by derived classes. A derived class, on the other hand, is a class that inherits properties and methods from a base class. The derived class can extend or modify the behavior of the base class.

Real-World Analogy

Think of inheritance as a family tree. In this analogy, the base class is like an ancestor (e.g., Grandparent), and the derived classes are like children who inherit traits and characteristics from their ancestors.

Syntax for Inheritance

In C++, you can define a derived class by specifying the base class in the class declaration using the colon (:) followed by the access specifier (public, protected, or private) and the name of the base class. Here is the basic syntax:

C++
1class BaseClass {
2 // Base class members
3};
4
5class DerivedClass : public BaseClass {
6 // Derived class members
7};

Example

Let's define a simple Animal base class and a Dog derived class.

animal.cpp
1#include <iostream>
2using namespace std;
3
4class Animal {
5public:
6 void eat() {
7 cout << "This animal eats food." << endl;
8 }
9};
10
11class Dog : public Animal {
12public:
13 void bark() {
14 cout << "The dog barks." << endl;
15 }
16};
17
18int main() {
19 Dog myDog;
20 myDog.eat(); // Inherited method
21 myDog.bark(); // Derived class method
22 return 0;
23}
Output
This animal eats food.
The dog barks.

In this example, the Dog class inherits the eat() method from the Animal base class. The Dog class also has its own method, bark(), which is specific to dogs.

Constructor and Destructor Order

When an object of a derived class is created, constructors are called in a specific order: first, the constructor of the base class is called, followed by the constructor of the derived class. Similarly, when an object is destroyed, destructors are called in reverse order: first, the destructor of the derived class, and then the destructor of the base class.

Example

Let's modify the previous example to include constructors and destructors for both classes.

animal_constructors.cpp
1#include <iostream>
2using namespace std;
3
4class Animal {
5public:
6 Animal() {
7 cout << "Animal constructor called." << endl;
8 }
9 ~Animal() {
10 cout << "Animal destructor called." << endl;
11 }
12};
13
14class Dog : public Animal {
15public:
16 Dog() {
17 cout << "Dog constructor called." << endl;
18 }
19 ~Dog() {
20 cout << "Dog destructor called." << endl;
21 }
22};
23
24int main() {
25 Dog myDog;
26 return 0;
27}
Output
Animal constructor called.
Dog constructor called.
Dog destructor called.
Animal destructor called.

In this example, the Animal constructor is called first, followed by the Dog constructor. When the program exits, the Dog destructor is called first, followed by the Animal destructor.

Access Specifiers

Access specifiers (public, protected, and private) play a crucial role in inheritance. They determine how members of the base class are accessible to the derived class.

  • Public: Members are accessible from anywhere.
  • Protected: Members are accessible within the same class, derived classes, and friends.
  • Private: Members are accessible only within the same class.

Example

Let's modify the Animal class to use different access specifiers.

animal_access.cpp
1#include <iostream>
2using namespace std;
3
4class Animal {
5public:
6 void eat() {
7 cout << "This animal eats food." << endl;
8 }
9protected:
10 void sleep() {
11 cout << "This animal sleeps." << endl;
12 }
13private:
14 void breathe() {
15 cout << "This animal breathes." << endl;
16 }
17};
18
19class Dog : public Animal {
20public:
21 void display() {
22 eat(); // Accessible
23 sleep(); // Accessible
24 // breathe(); // Error: cannot access private member
25 }
26};
27
28int main() {
29 Dog myDog;
30 myDog.display();
31 return 0;
32}
Output
This animal eats food.
This animal sleeps.

In this example, the eat() method is public and accessible by the Dog class. The sleep() method is protected and also accessible by the Dog class. The breathe() method is private and cannot be accessed by the Dog class.

Practical Example

Let's create a more practical example that demonstrates inheritance with multiple classes and different access specifiers.

vehicle.cpp
1#include <iostream>
2using namespace std;
3
4class Vehicle {
5public:
6 Vehicle() {
7 cout << "Vehicle constructor called." << endl;
8 }
9 ~Vehicle() {
10 cout << "Vehicle destructor called." << endl;
11 }
12 void startEngine() {
13 cout << "Engine started." << endl;
14 }
15protected:
16 int wheels;
17private:
18 string engineType;
19};
20
21class Car : public Vehicle {
22public:
23 Car() : wheels(4), engineType("Petrol") {
24 cout << "Car constructor called." << endl;
25 }
26 ~Car() {
27 cout << "Car destructor called." << endl;
28 }
29 void displayInfo() {
30 cout << "Number of wheels: " << wheels << endl;
31 // cout << "Engine type: " << engineType << endl; // Error: cannot access private member
32 }
33};
34
35class Motorcycle : public Vehicle {
36public:
37 Motorcycle() : wheels(2), engineType("Petrol") {
38 cout << "Motorcycle constructor called." << endl;
39 }
40 ~Motorcycle() {
41 cout << "Motorcycle destructor called." << endl;
42 }
43 void displayInfo() {
44 cout << "Number of wheels: " << wheels << endl;
45 // cout << "Engine type: " << engineType << endl; // Error: cannot access private member
46 }
47};
48
49int main() {
50 Car myCar;
51 myCar.startEngine();
52 myCar.displayInfo();
53
54 Motorcycle myMotorcycle;
55 myMotorcycle.startEngine();
56 myMotorcycle.displayInfo();
57
58 return 0;
59}
Output
Vehicle constructor called.
Car constructor called.
Engine started.
Number of wheels: 4
Vehicle destructor called.
Car destructor called.
Vehicle constructor called.
Motorcycle constructor called.
Engine started.
Number of wheels: 2
Vehicle destructor called.
Motorcycle destructor called.

In this example, we have a Vehicle base class and two derived classes, Car and Motorcycle. Both derived classes inherit the startEngine() method from the Vehicle class. The constructors for each class initialize the wheels member variable and set the engineType to "Petrol". The destructors are called in reverse order when the program exits.

Summary

ConceptDescription
Base ClassA class from which other classes inherit.
Derived ClassA class that inherits properties and methods from a base class.
Syntaxclass DerivedClass : public BaseClass { ... };
Constructor OrderBase class constructor -> Derived class constructor
Destructor OrderDerived class destructor -> Base class destructor
Access SpecifiersPublic, Protected, and Private determine member accessibility in inheritance.

What's Next?

In the next tutorial, we will explore different types of inheritance in C++, including public, protected, and private inheritance. Understanding these types is crucial for controlling the visibility of base class members in derived classes. Stay tuned!


PreviousOperator OverloadingNext Public, Protected, and Private Inheritance

Recommended Gear

Operator OverloadingPublic, Protected, and Private Inheritance