Inheritance and polymorphism are two of the most powerful concepts in Object-Oriented Programming (OOP). They allow developers to build scalable, reusable, and maintainable applications by extending existing classes and enabling objects to behave in multiple ways. C++ Inheritance and Polymorphism practice questions with solutions help to understand the concepts.
What is Inheritance?
Inheritance allows one class (called the derived class) to inherit properties and functions from another class (called the base class).
It promotes:
- Code Reusability
- Easy Maintenance
- Reduced Code Duplication
Example:
class Animal
{
public:
void eat()
{
cout << "Animal is eating";
}
};
class Dog : public Animal
{
};
Here, the Dog class automatically inherits the eat() function from the Animal class.
What is Polymorphism?
Polymorphism means “one interface, many forms.”
It allows the same function name or operator to perform different tasks depending on the object or parameters.
There are two main types:
- Compile-Time Polymorphism
- Function Overloading
- Operator Overloading
- Run-Time Polymorphism
- Function Overriding
- Virtual Functions
Types of Inheritance
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Inheritance and polymorphism are widely used in:
- Banking Software
- Hospital Management Systems
- Game Development
- Enterprise Applications
- GUI Applications
- Inventory Management
- E-commerce Platforms
In this chapter, you’ll practice inheritance and polymorphism through beginner-friendly coding problems with complete explanations.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Output
- Explanation
- Concepts Covered
Let’s begin.
1. C++ Program to Demonstrate Single Inheritance
Problem Statement
Write a C++ program to demonstrate single inheritance.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal is eating" << endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Dog is barking";
}
};
int main()
{
Dog dog;
dog.eat();
dog.bark();
return 0;
}
Sample Output
Animal is eating
Dog is barking
Explanation
The Dog class inherits the eat() function from the Animal class.
Concepts Covered
- Single Inheritance
- Base Class
- Derived Class
2. C++ Program to Demonstrate Multilevel Inheritance
Problem Statement
Write a C++ program to demonstrate multilevel inheritance.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal is eating" << endl;
}
};
class Mammal : public Animal
{
public:
void walk()
{
cout << "Mammal can walk" << endl;
}
};
class Dog : public Mammal
{
public:
void bark()
{
cout << "Dog is barking";
}
};
int main()
{
Dog dog;
dog.eat();
dog.walk();
dog.bark();
return 0;
}
Sample Output
Animal is eating
Mammal can walk
Dog is barking
Explanation
The Dog class inherits features through the Mammal class.
Concepts Covered
- Multilevel Inheritance
- Base Class
- Intermediate Class
3. C++ Program to Demonstrate Hierarchical Inheritance
Problem Statement
Write a C++ program to demonstrate hierarchical inheritance.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal is eating" << endl;
}
};
class Dog : public Animal
{
};
class Cat : public Animal
{
};
int main()
{
Dog dog;
Cat cat;
dog.eat();
cat.eat();
return 0;
}
Sample Output
Animal is eating
Animal is eating
Explanation
Multiple derived classes inherit from a single base class.
Concepts Covered
- Hierarchical Inheritance
- Base Class
- Multiple Derived Classes
4. C++ Program to Demonstrate Function Overloading
Problem Statement
Write a C++ program to demonstrate function overloading.
C++ Solution
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
};
int main()
{
Calculator calculator;
cout << calculator.add(5, 7) << endl;
cout << calculator.add(5.5, 2.5);
return 0;
}
Sample Output
12
8
Explanation
The same function name performs different tasks depending on the parameter types.
Concepts Covered
- Compile-Time Polymorphism
- Function Overloading
5. C++ Program to Demonstrate Function Overriding
Problem Statement
Write a C++ program to demonstrate function overriding.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
void sound()
{
cout << "Animal makes sound" << endl;
}
};
class Dog : public Animal
{
public:
void sound()
{
cout << "Dog barks";
}
};
int main()
{
Dog dog;
dog.sound();
return 0;
}
Sample Output
Dog barks
Explanation
The derived class provides its own implementation of the base class function.
Concepts Covered
- Function Overriding
- Inheritance
- Polymorphism
6. C++ Program to Demonstrate Multiple Inheritance
Problem Statement
Write a C++ program to demonstrate multiple inheritance.
C++ Solution
#include <iostream>
using namespace std;
class Person
{
public:
void displayName()
{
cout << "Name: Rahul" << endl;
}
};
class Student
{
public:
void displayRollNumber()
{
cout << "Roll Number: 101" << endl;
}
};
class CollegeStudent : public Person, public Student
{
};
int main()
{
CollegeStudent student;
student.displayName();
student.displayRollNumber();
return 0;
}
Sample Output
Name: Rahul
Roll Number: 101
Explanation
The derived class inherits properties and functions from more than one base class.
Concepts Covered
- Multiple Inheritance
- Base Classes
- Derived Class
7. C++ Program to Demonstrate Virtual Function
Problem Statement
Write a C++ program to demonstrate a virtual function.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void sound()
{
cout << "Animal makes sound" << endl;
}
};
class Dog : public Animal
{
public:
void sound() override
{
cout << "Dog barks" << endl;
}
};
int main()
{
Animal *animal;
Dog dog;
animal = &dog;
animal->sound();
return 0;
}
Sample Output
Dog barks
Explanation
A virtual function enables runtime polymorphism. The function call is resolved based on the actual object rather than the pointer type.
Concepts Covered
- Virtual Function
- Runtime Polymorphism
- Base Class Pointer
8. C++ Program to Demonstrate Runtime Polymorphism
Problem Statement
Write a C++ program to demonstrate runtime polymorphism using a base class pointer.
C++ Solution
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw()
{
cout << "Drawing Shape" << endl;
}
};
class Circle : public Shape
{
public:
void draw() override
{
cout << "Drawing Circle" << endl;
}
};
int main()
{
Shape *shape;
Circle circle;
shape = &circle;
shape->draw();
return 0;
}
Sample Output
Drawing Circle
Explanation
The base class pointer calls the overridden function of the derived class because the function is declared as virtual.
Concepts Covered
- Runtime Polymorphism
- Virtual Function
- Dynamic Binding
9. C++ Program to Demonstrate Base Class Pointer
Problem Statement
Write a C++ program to access a derived class object using a base class pointer.
C++ Solution
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void start()
{
cout << "Vehicle Started" << endl;
}
};
class Car : public Vehicle
{
public:
void start() override
{
cout << "Car Started" << endl;
}
};
int main()
{
Vehicle *vehicle;
Car car;
vehicle = &car;
vehicle->start();
return 0;
}
Sample Output
Car Started
Explanation
A base class pointer can reference a derived class object, enabling dynamic behavior through virtual functions.
Concepts Covered
- Base Class Pointer
- Virtual Function
- Dynamic Dispatch
10. C++ Program to Demonstrate Compile-Time Polymorphism
Problem Statement
Write a C++ program to demonstrate compile-time polymorphism using function overloading.
C++ Solution
#include <iostream>
using namespace std;
class Math
{
public:
int multiply(int a, int b)
{
return a * b;
}
double multiply(double a, double b)
{
return a * b;
}
};
int main()
{
Math math;
cout << "Integer Multiplication = "
<< math.multiply(5, 4) << endl;
cout << "Decimal Multiplication = "
<< math.multiply(2.5, 4.0);
return 0;
}
Sample Output
Integer Multiplication = 20
Decimal Multiplication = 10
Explanation
The compiler selects the correct overloaded function during compilation based on the argument types.
Concepts Covered
- Compile-Time Polymorphism
- Function Overloading
- Static Binding
11. C++ Program to Demonstrate Abstract Class
Problem Statement
Write a C++ program to demonstrate an abstract class using a pure virtual function.
C++ Solution
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw() override
{
cout << "Drawing Rectangle";
}
};
int main()
{
Rectangle rectangle;
rectangle.draw();
return 0;
}
Sample Output
Drawing Rectangle
Explanation
An abstract class contains at least one pure virtual function and cannot be instantiated directly.
Concepts Covered
- Abstract Class
- Pure Virtual Function
- Runtime Polymorphism
12. C++ Program to Demonstrate Dynamic Binding
Problem Statement
Write a C++ program to demonstrate dynamic binding using virtual functions.
C++ Solution
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void sound()
{
cout << "Animal Sound" << endl;
}
};
class Cat : public Animal
{
public:
void sound() override
{
cout << "Cat Meows";
}
};
int main()
{
Animal *animal;
Cat cat;
animal = &cat;
animal->sound();
return 0;
}
Sample Output
Cat Meows
Explanation
Dynamic binding determines the function to execute at runtime based on the actual object.
Concepts Covered
- Dynamic Binding
- Virtual Functions
- Polymorphism
13. C++ Program to Demonstrate Upcasting
Problem Statement
Write a C++ program to demonstrate upcasting.
C++ Solution
#include <iostream>
using namespace std;
class Person
{
public:
virtual void introduce()
{
cout << "I am a Person" << endl;
}
};
class Teacher : public Person
{
public:
void introduce() override
{
cout << "I am a Teacher";
}
};
int main()
{
Person *person = new Teacher();
person->introduce();
delete person;
return 0;
}
Sample Output
I am a Teacher
Explanation
Upcasting means assigning a derived class object to a base class pointer.
Concepts Covered
- Upcasting
- Runtime Polymorphism
- Base Class Pointer
14. C++ Program to Demonstrate Hybrid Inheritance
Problem Statement
Write a C++ program to demonstrate hybrid inheritance.
C++ Solution
#include <iostream>
using namespace std;
class A
{
public:
void displayA()
{
cout << "Class A" << endl;
}
};
class B : public A
{
};
class C : public A
{
};
class D : public B, public C
{
};
int main()
{
D object;
object.B::displayA();
object.C::displayA();
return 0;
}
Sample Output
Class A
Class A
Explanation
Hybrid inheritance combines multiple inheritance types. Scope resolution is used to avoid ambiguity.
Concepts Covered
- Hybrid Inheritance
- Scope Resolution Operator
- Multiple Inheritance
15. C++ Program to Demonstrate Inheritance and Polymorphism Together
Problem Statement
Write a C++ program that combines inheritance and polymorphism.
C++ Solution
#include <iostream>
using namespace std;
class Employee
{
public:
virtual void work()
{
cout << "Employee Working" << endl;
}
};
class Developer : public Employee
{
public:
void work() override
{
cout << "Developer Writing Code";
}
};
int main()
{
Employee *employee;
Developer developer;
employee = &developer;
employee->work();
return 0;
}
Sample Output
Developer Writing Code
Explanation
The derived class overrides the base class function, and the correct function is called at runtime through the base class pointer.
Concepts Covered
- Inheritance
- Polymorphism
- Virtual Function
- Runtime Binding
Chapter Summary
In this chapter, you learned how inheritance enables one class to reuse the properties and behavior of another class, reducing code duplication and improving maintainability. You also explored polymorphism, which allows the same interface to perform different tasks depending on the object. Through practical examples, you covered single, multiple, multilevel, hierarchical, and hybrid inheritance, function overloading, function overriding, virtual functions, abstract classes, dynamic binding, and runtime polymorphism.
Key Takeaways
- Inheritance enables code reuse by creating derived classes from base classes.
- Single inheritance involves one base class and one derived class.
- Multiple inheritance allows a class to inherit from more than one base class.
- Multilevel inheritance forms a chain of inheritance.
- Hierarchical inheritance allows multiple derived classes to share one base class.
- Polymorphism means one interface can have multiple implementations.
- Function overloading provides compile-time polymorphism.
- Virtual functions enable runtime polymorphism.
- Abstract classes cannot be instantiated directly and are used to define common interfaces.
- Inheritance and polymorphism are fundamental concepts for building scalable and reusable software.
Frequently Asked Questions (FAQs)
1. What is inheritance in C++?
Inheritance is an OOP feature that allows one class to acquire the properties and functions of another class.
2. What are the different types of inheritance?
The main types are:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
3. What is polymorphism?
Polymorphism allows the same function or interface to perform different tasks depending on the object or parameters.
4. What is the difference between function overloading and function overriding?
Function overloading occurs within the same class using different parameter lists (compile-time polymorphism), whereas function overriding occurs in derived classes using virtual functions (runtime polymorphism).
5. Why are virtual functions used?
Virtual functions allow the correct overridden function to be called at runtime through a base class pointer.
6. What is an abstract class?
An abstract class contains at least one pure virtual function and serves as a blueprint for derived classes.
7. What is dynamic binding?
Dynamic binding resolves function calls at runtime based on the actual object rather than the pointer type.
8. Why are inheritance and polymorphism important?
They improve code reusability, flexibility, extensibility, and maintainability, making them essential for developing large-scale Object-Oriented Programming applications.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
