Question

Write a C++ program that demonstrates the use of inheritance and polymorphism by creating a base class Shape with a virtual function area(). Derive three classes, namely, Circle, Rectangle, and Triangle from Shape, each overriding the area() function to calculate and return the respective area

07 Aug 2026
Answer :
Word Count : 1072
Inheritance and polymorphism are two important concepts of object-oriented programming in C++. Inheritance allows a new class to acquire the properties and functions of an existing class, while polymorphism allows the same function call to behave differently depending on the object that invokes it. These concepts make programs more organized, reusable, flexible, and easier to maintain. The following C++ program demonstrates both inheritance and polymorphism by creating a base class named `Shape` and three derived classes named `Circle`, `Rectangle`, and `Triangle`. Each derived class overrides the virtual `area()` function of the base class to calculate the area of its respective shape. In the program, the `Shape` class acts as the base class. It contains a virtual function named `area()`. The keyword `virtual` is important because it enables runtime polymorphism. A virtual function tells the C++ compiler that the function can be overridden in derived classes and that the appropriate version of the function should be selected at runtime according to the actual object being referred to. The program can be written as follows: ```cpp #include using namespace std; class Shape { public: virtual double area() { return 0; } }; class Circle : public Shape { private: double radius; public: Circle(double r) { radius = r; } double area() override { return 3.14159 * radius * radius; } }; class Rectangle : public Shape { private: double length; double width; public: Rectangle(double l, double w) { length = l; width = w; } double area() override { return length * width; } }; class Triangle : public Shape { private: double base; double height; public: Triangle(double b, double h) { base = b; height = h; } double area() override { return 0.5 * base * height; } }; int main() { Shape *shape; Circle c(5); Rectangle r(10, 4); Triangle t(8, 6); shape = &c; cout << "Area of Circle
__________ ________ _________ _____ ______ __________ ______ ______ ________ ________.
________ __________ _______ __________ _________ _______ ________ ____ _____ _________ _________.
_____ _________ ________ _________ ________ ________ ________ ________.
____ ______ _______ _________ ___ ___ _______ ______ _________ ___ _____.
__________ ______ ______ __________ ______ _____ ___ _____ _________ ________ ___ _______.
_____ _____ ___ _______ _________ __________ ______ ________ ___.
_______ __________ _________ ________ _____ ________ _________ _______ _____.
_________ ________ _____ ________ _____ ___ _____ ________ _____ __________.
_________ _________ _______ ___ _______ ___ _____.
__________ _______ _______ ________ _________.
__________ ____ _______ ____ ________.
_______ _______ ______ __________ ______ _________ ________ ________ _____.
______ _____ ___ __________ ______.
_______ _____ ____ __________ __________.
__________ _______ ______ _______ ____.
_______ _________ ___ _____ ____ __________ _________ ______ _________ _______ ___ ______.
___ ___ __________ ______ ___ ____.
___ ______ ____ ____ ______ ______ _____ ___ ___.
___ ______ ___ _____ _______ ____.
_______ _____ ________ _______ __________ ___.
_____ ___ ___ ________ _____ __________ __________ ___ ____ _________ ____.
______ ______ __________ ________ ______ _________ ____ ________.
______ _________ ____ _________ ________ ____ _____ __________ __________ _______ ____.
___ ____ __________ ______ _______ ________ __________.
_________ _________ _________ _______ __________ _________ _____ __________ __________ _____.
____ _______ ___ ____ _________ __________ ___ _______ _____ ________ ____ _______.
______ ____ _______ _________ _______ __________ ____ ______.
___ _______ _______ ________ ____ ________ _______ _____ ___ __________ ________.
____ __________ ______ ____ ________ ________ _________.
_____ __________ ______ ______ ____ ________ _________ ____ ____ ____.
_______ ______ _____ _______ _________.
_____ _______ _____ _________ __________ _______ _______ _____ __________ _________ _______ ______.
__________ ______ ______ _______ ________ ___ _______ _______ _________.
_________ _________ _________ __________ _______ _______ _____ ______.
_________ _____ _________ _________ _______.
___ _______ _______ ___ _______.
_________ _____ ______ _______ ____ _____ __________ ___ ___ ______.
_____ ______ ______ _____ ______ ___ __________ ____ _____ _________ _________.
__________ ___ ____ _______ ______ ________ _____.
_______ ___ ________ ________ _____ ________ ______.
__________ ____ ______ _________ ___ ___ __________ ___ ________ ____.
_____ ____ __________ ______ __________ ____ _________.
_________ __________ _____ _______ _____ ___ ____.
________ ___ ____ ______ _________ ________ ____.
___ __________ ________ ____ ___.
________ __________ __________ _________ ______ ___ ______ ___ ______.
_________ ___ _____ _____ ____ ____ __________.
________ _________ _________ _________ ________ __________ _______ ____ ____ ____.
_______ ________ _________ ____ ________ ______ _________.
__________ _________ ___ ____ __________ ________ _________ ____ _______ ______ _____ _________.
____ _______ _____ ________ ______ _____ _______ __________ _______ ____ ______.
___ ____ ______ _______ _______ _____ ____ ______ __________ __________ ____ _________.
___ __________ ____ _______ _________.
______ _______ ________ _______ _______ ___ ____ ___ _______ _________.
____ ____ ___ ________ __________ ____ ________ _____ _________ _____ _______.
___ ___ _________ _______ ________.
_______ _____ ________ _________ ________ ______ _________ ____.
____ _____ ___ ___ _________ ____ __________ ______ ___.
__________ _____ ________ ______ ___ ___.
_______ ____ _____ ___ _______ _______ _______ ____ __________ _______ ____.
_____ ________ _________ __________ ______ ____ _____ _____ _______ _____ ______ ________.
_______ _________ ________ _____ __________.
___ ______ _______ ___ _________ ____ _______ ________.
___ ______ ______ ______ _________ __________ ________ _________ _______ _____ _______ _______.
__________ ______ _________ _______ ____ ______ ____ _________ ________ _____ _____ _________.
__________ ________ _________ _________ _______ ______ ________ _______ ____ _________ _______ _____.
________ _______ ___ ____ __________ ________ ____ ___.
______ ___ _________ __________ _____ _____ _____ __________ _________ __________ _________.
__________ ____ _________ ________ ______ _______ _____.
_________ ________ _______ _______ _______.
______ ______ ___ ___ ____ _______.
______ ___ __________ ___ ___ ______ ______ _________ _________ _____.
________ ____ ____ ___ _______ __________.
____ ___ ______ ______ ________ ____ ______.
_______ _____ ________ _________ _______.
_____ ________ _______ ______ __________ ______ ______ ______ ___ _____.
_________ _____ ________ _____ __________ __________ ________ _______ _____ _____ ________ _________.
________ ______ ____ _____ _____ _____.
___ _________ _____ _________ ___ __________ _____ ________.
____ __________ ____ _________ ____.
______ _________ ________ _____ ____ _________ _________ ________ _____.
______ ______ ____ _________ __________ ________ _____ ____ ____ ________ ___ _____.
____ ____ _____ ____ _______ ____ ____ _____ ________ _________ _______ ______.
______ ____ __________ ___ _________ ______ ___.
__________ ___ _________ _____ _________ _________ _____ ___ _____.
____ ________ _____ _______ ___ ___ ________ ___ ______.
___ _______ ______ ______ _________ ________ _____ ____ _________.
__________ ______ _______ ______ _________.
_______ _____ _______ ________ ________ ____ ______ ______ ______ __________.
______ _____ ___ ________ ______ _______.
_____ _______ ___.
IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
Top
📞
Call Support Instant phone assistance IGNOU BCSL 32 2026 2027 - Write a C++ program that demonstrates the use of inheritance and polym | IGNOU Academy
→
🟢
WhatsApp Chat Fast live messaging
→
Email Us Business enquiries & support
→