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
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, ______ ______ ________ _______ ___ _______ ________ _________ ___ _________ _______ _________.
_____ ______ _____ __________ __________.
___ ______ _______ ___ _________ ______ ________ _______ _________.
_____ ____ _______ _____ ________ _________.
___ __________ ______ __________ ____ _____ _____.
_________ ________ _______ ______ _____ ___ ______.
_____ ______ ________ _____ _______ _______ _______ ___.
_________ ______ __________ ______ __________ ____ _______ ______ ___ ________ ______.
____ ___ ______ _________ _______ _______.
______ _______ __________ _______ ___ ____ ________.
______ ___ ____ ________ _______ ________ __________ _____ ________.
_______ _____ _____ ________ __________ ______ ____ ____ ________ _____ ________.
_____ _________ ___ _____ ___ __________.
__________ _______ _______ ____ ________ ______ _____ ______.
_____ __________ ______ ______ ______ ____ _________ ____ _____ ______.
________ _______ __________ ___ _________ ______ _________ ___.
_________ _______ ________ ____ ____ ___.
_________ __________ ________ ___ ____ _________ ___ ______.
_______ ______ _________ ____ _______ ________ ______.
______ _______ _______ ___ __________ _______ ______ ________ ______ ____ ___.
_____ _______ ________ ___ ______ _____ __________ _________.
_______ _________ _______ __________ ___ ______ __________ _______ ______ _________.
_____ _______ _____ _________ __________ ____ _________ _____ _____ _____ ________.
________ ___ _______ __________ ___ __________.
__________ ________ ___ ______ _________ _________ _________ __________ _______ ______.
____ _______ ______ ___ _______.
___ ____ ______ ______ _____ _____ __________ __________ __________ _______.
______ _______ __________ __________ ___ ____ ___.
________ ________ _________ ______ ____ _____ _______.
_______ __________ ___ _______ ________ ___ _____ ________ _____ ______.
______ _________ _____ ________ _____ _______ __________ ______ _____ _____ ___ ________.
________ _______ _____ _________ ______.
_____ _____ _________ ________ _____ ________ _______ _________.
_______ _______ ____ _____ ____ _________ __________ ________ ___ ______.
_______ _____ _________ ___ ______ _________.
_________ __________ ___ _____ _____ _____ ________ ____ ___ _____.
_____ ____ _______ ___ __________ ________ _________ _____ _________ __________ ___.
____ ____ ___ ___ _____.
_________ _______ __________ ____ _________.
___ ______ _________ ____ _____ ________ ______ ___ ________ ________.
_______ ___ ____ ________ _____ _________ __________.
_____ ________ _____ ______ _____ ___ _________ _______ ____ _________.
___ _________ ______ __________ _________ _______ __________.
_____ ____ ____ __________ _____ ____.
________ ____ _________ ___ ____ _______ _____ __________ __________ __________ ________ _____.
_____ ______ __________ __________ ________ ___ _______.
____ ____ ____ _____ ______ ______ ________ ____ _________ __________ ______ _________.
_______ _____ _________ _______ ________ _________ __________ ____.
__________ __________ ____ ________ ____.
__________ ____ _____ ___ _______ ______ _____ __________ __________.
______ _____ __________ __________ ____ ______ _________ _____.
__________ __________ ________ ______ _____.
___ _____ _______ ___ __________ _______ __________ ____ _____ ______ ________.
________ _____ _____ ______ ____ ________.
__________ ____ _____ ____ ______ _______ ____.
____ ________ __________ ________ __________ ________ __________ ____ _______.
______ ________ ________ _____ __________ ____ _______ ___.
_________ _____ __________ ________ ________ ___.
____ __________ __________ ________ _________ ________ _____ __________ _________ ______ _________.
___ _________ _____ ___ ___ _____.
________ ______ _________ ____ _______ _________ ________ __________ _____ ____.
_______ _________ __________ ____ ________ ____ _______ __________ __________ _____ ___.
__________ _______ ________ __________ _______ _____ ___ ______ _____.
________ _____ _________ ____ ___ ___ ______.
________ __________ _______ __________ _________.
_________ __________ _______ ____ _________ ______ ___.
_____ ____ _________ ____ _______ ________ _____ ________ ___.
___ _______ ______ _____ _______ ______ ___ ______ _______ __________ _________.
_________ _______ __________ ______ __________ _______ ________.
________ ________ ___ ___ __________ ______ _________ ____ ________.
________ ________ _______ __________ ___ ___ ________ __________ ___ _______ _______.
_______ ______ ____ ___ _________.
_________ ___ _________ _____ ________ ____ _________ ________ ____ ____.
_________ _________ ________ __________ ______ _____ _______ ___ __________ ____ ____ _______.
_______ ___ _________ ___ _____ ___.
___ _______ ____ __________ ____ ___ ___ ________ _________ ____ ________ _________.
_________ ________ ________ __________ _______ ______ ____ _________ ________ _________ ___.
_______ ________ ________ _____ ____ _______.
___ ________ _________ ___ __________ _______ ________ ______ ______.
_________ ________ ________ _____ __________ ___ ________ ____ ________.
______ ____ _________ ______ _______ ____ _______ ______ ____ ______ ___ ___.
_________ ______ ___ _______ _______ ______ ________ __________ __________.
__________ _____ _______ _____ _____ ________ ________ ___ _____ _____.
_________ ___ __________ _____ __________ ________ ____ __________ ___ _______ ________ ____.
______ ____ ___ ______ __________.
____ ________ __________ ___ ______ ________ _____ __________ ________ _______ _________.
___ ______ ___ _________ __________ __________ _________ __________ ________ _________ _______ ____.
_______ ________ ____ ____ _____ ____ ____ ___.
__________ _________ ________ ________ _____ _________ ________ ____ _______ _________.
__________ __________ __________ ______ ____ __________ ____ __________.
_______ ________ _________ ________ ______ ____ ______ ___ ___ ______ ______.
_________ _____ __________ ______ _____ ___ _______ ______.
________ ____ ________ ___ _______ _____ ___.
_____ ___ _________ _______ ____ ________ ______.
_____ __________ ______ ____ ___ _____ ________ __________ __________ ________.
Get Full Answer on WhatsApp
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
_____ ______ _____ __________ __________.
___ ______ _______ ___ _________ ______ ________ _______ _________.
_____ ____ _______ _____ ________ _________.
___ __________ ______ __________ ____ _____ _____.
_________ ________ _______ ______ _____ ___ ______.
_____ ______ ________ _____ _______ _______ _______ ___.
_________ ______ __________ ______ __________ ____ _______ ______ ___ ________ ______.
____ ___ ______ _________ _______ _______.
______ _______ __________ _______ ___ ____ ________.
______ ___ ____ ________ _______ ________ __________ _____ ________.
_______ _____ _____ ________ __________ ______ ____ ____ ________ _____ ________.
_____ _________ ___ _____ ___ __________.
__________ _______ _______ ____ ________ ______ _____ ______.
_____ __________ ______ ______ ______ ____ _________ ____ _____ ______.
________ _______ __________ ___ _________ ______ _________ ___.
_________ _______ ________ ____ ____ ___.
_________ __________ ________ ___ ____ _________ ___ ______.
_______ ______ _________ ____ _______ ________ ______.
______ _______ _______ ___ __________ _______ ______ ________ ______ ____ ___.
_____ _______ ________ ___ ______ _____ __________ _________.
_______ _________ _______ __________ ___ ______ __________ _______ ______ _________.
_____ _______ _____ _________ __________ ____ _________ _____ _____ _____ ________.
________ ___ _______ __________ ___ __________.
__________ ________ ___ ______ _________ _________ _________ __________ _______ ______.
____ _______ ______ ___ _______.
___ ____ ______ ______ _____ _____ __________ __________ __________ _______.
______ _______ __________ __________ ___ ____ ___.
________ ________ _________ ______ ____ _____ _______.
_______ __________ ___ _______ ________ ___ _____ ________ _____ ______.
______ _________ _____ ________ _____ _______ __________ ______ _____ _____ ___ ________.
________ _______ _____ _________ ______.
_____ _____ _________ ________ _____ ________ _______ _________.
_______ _______ ____ _____ ____ _________ __________ ________ ___ ______.
_______ _____ _________ ___ ______ _________.
_________ __________ ___ _____ _____ _____ ________ ____ ___ _____.
_____ ____ _______ ___ __________ ________ _________ _____ _________ __________ ___.
____ ____ ___ ___ _____.
_________ _______ __________ ____ _________.
___ ______ _________ ____ _____ ________ ______ ___ ________ ________.
_______ ___ ____ ________ _____ _________ __________.
_____ ________ _____ ______ _____ ___ _________ _______ ____ _________.
___ _________ ______ __________ _________ _______ __________.
_____ ____ ____ __________ _____ ____.
________ ____ _________ ___ ____ _______ _____ __________ __________ __________ ________ _____.
_____ ______ __________ __________ ________ ___ _______.
____ ____ ____ _____ ______ ______ ________ ____ _________ __________ ______ _________.
_______ _____ _________ _______ ________ _________ __________ ____.
__________ __________ ____ ________ ____.
__________ ____ _____ ___ _______ ______ _____ __________ __________.
______ _____ __________ __________ ____ ______ _________ _____.
__________ __________ ________ ______ _____.
___ _____ _______ ___ __________ _______ __________ ____ _____ ______ ________.
________ _____ _____ ______ ____ ________.
__________ ____ _____ ____ ______ _______ ____.
____ ________ __________ ________ __________ ________ __________ ____ _______.
______ ________ ________ _____ __________ ____ _______ ___.
_________ _____ __________ ________ ________ ___.
____ __________ __________ ________ _________ ________ _____ __________ _________ ______ _________.
___ _________ _____ ___ ___ _____.
________ ______ _________ ____ _______ _________ ________ __________ _____ ____.
_______ _________ __________ ____ ________ ____ _______ __________ __________ _____ ___.
__________ _______ ________ __________ _______ _____ ___ ______ _____.
________ _____ _________ ____ ___ ___ ______.
________ __________ _______ __________ _________.
_________ __________ _______ ____ _________ ______ ___.
_____ ____ _________ ____ _______ ________ _____ ________ ___.
___ _______ ______ _____ _______ ______ ___ ______ _______ __________ _________.
_________ _______ __________ ______ __________ _______ ________.
________ ________ ___ ___ __________ ______ _________ ____ ________.
________ ________ _______ __________ ___ ___ ________ __________ ___ _______ _______.
_______ ______ ____ ___ _________.
_________ ___ _________ _____ ________ ____ _________ ________ ____ ____.
_________ _________ ________ __________ ______ _____ _______ ___ __________ ____ ____ _______.
_______ ___ _________ ___ _____ ___.
___ _______ ____ __________ ____ ___ ___ ________ _________ ____ ________ _________.
_________ ________ ________ __________ _______ ______ ____ _________ ________ _________ ___.
_______ ________ ________ _____ ____ _______.
___ ________ _________ ___ __________ _______ ________ ______ ______.
_________ ________ ________ _____ __________ ___ ________ ____ ________.
______ ____ _________ ______ _______ ____ _______ ______ ____ ______ ___ ___.
_________ ______ ___ _______ _______ ______ ________ __________ __________.
__________ _____ _______ _____ _____ ________ ________ ___ _____ _____.
_________ ___ __________ _____ __________ ________ ____ __________ ___ _______ ________ ____.
______ ____ ___ ______ __________.
____ ________ __________ ___ ______ ________ _____ __________ ________ _______ _________.
___ ______ ___ _________ __________ __________ _________ __________ ________ _________ _______ ____.
_______ ________ ____ ____ _____ ____ ____ ___.
__________ _________ ________ ________ _____ _________ ________ ____ _______ _________.
__________ __________ __________ ______ ____ __________ ____ __________.
_______ ________ _________ ________ ______ ____ ______ ___ ___ ______ ______.
_________ _____ __________ ______ _____ ___ _______ ______.
________ ____ ________ ___ _______ _____ ___.
_____ ___ _________ _______ ____ ________ ______.
_____ __________ ______ ____ ___ _____ ________ __________ __________ ________.
Get Full Answer on WhatsApp
IGNOU NEWS
Assignment Submission Last Date Extended Till 30 June 2026 Click Here★★★IGNOU June 2026 TEE Date Sheet Released Click Here★★★