Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Use the concept of constructor override in C++ and create a class

+4 votes
asked Jul 7, 2019 by anonymous
Use the concept of constructor override in C++ and create a class, which calculates areas of square, rectangle and a triangle. Override of the constructor should be controlled using the number of variables used for the object initialization. Feel free to use the method override in the same class. In the main function you should demonstrate examples for all three cases of the shapes.

1 Answer

+1 vote
answered Mar 22 by harshita (160 points)
#include <iostream>
using namespace std;

class Area {
    float a, b, c;

public:
    // Constructor for Square
    Area(float side) {
        a = side;
        cout << "Square Area = " << a * a << endl;
    }

    // Constructor for Rectangle
    Area(float length, float breadth) {
        a = length;
        b = breadth;
        cout << "Rectangle Area = " << a * b << endl;
    }

    // Constructor for Triangle
    Area(float base, float height, float dummy) {
        a = base;
        b = height;
        cout << "Triangle Area = " << 0.5 * a * b << endl;
    }
};

int main() {
    // Square
    Area obj1(5);

    // Rectangle
    Area obj2(5, 4);

    // Triangle
    Area obj3(6, 3, 0);

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...