Area of Polygon using C++ Inheritance

/*

This is a menu-based program where the user will choose an option in the menu to find the area

Base Class: Polygon

Sub Base Class: Triangle, Qudrangle, Pentagon

Sub Sub Base Class (Qudrangle): Rectangle, Square, Parallelogram

*/

 #include <iostream>


using namespace std;


class polygon{

protected:

    int length, height;

    float area;

public:

    void input(){

    cout << "Enter the length and height of polygon: ";

    cin >> length >> height;

    }


    void display(){

    cout << "Length: " << length << " units" << endl;

    cout << "Height: " << height << " units" << endl;

    cout << "Area: " << area << " units square" << endl;


    }


};


class triangle : public polygon{

public:

    void area_of_polygon(){

    input();

    area = 0.5*length*height;

    cout << endl;

    cout << "Formula used: 0.5*base*height" <<endl;

    display();

    }

};


class pentagon : public polygon{

public:

    void area_of_polygon(){

    input();

    area = 2.5*length*height;

    cout << endl;

    cout << "Formula used: 2.5*side*apothem_height" <<endl;

    display();

    }

};


class qudrangle : public polygon{

public:


    void getdata(){

    input();

    }


    void showdata(){

    display();

    }


};


class rectangle : public qudrangle{

public:


    void area_of_polygon(){

    getdata();

    area = height*length;

    cout << endl;

    cout << "Formula used: length*breadth" <<endl;

    showdata();

    }

};


class square : public qudrangle{

public:


    void area_of_polygon(){

    getdata();

    area = height*length;

    cout << endl;

    cout << "Formula used: side*side" <<endl;

    showdata();

    }

};


class parallelogram : public qudrangle{

public:

    void area_of_polygon(){

    getdata();

    area = height*length;

    cout << endl;

    cout << "Formula used: base*height" <<endl;

    showdata();

    }

};


int main()

{

    cout << "Polygon Area" << endl;

    cout << "1. Area of Triangle" << endl;

    cout << "2. Area of Pentagon" << endl;

    cout << "3. Area of Rectangle" << endl;

    cout << "4. Area of Square" << endl;

    cout << "5. Area of Parallelogram" << endl;

    cout << "Select from above like 1,2,3: ";


    int ch;

    cin >> ch;


    switch(ch){

case 1:

    triangle P1;

    P1.area_of_polygon();

    break;

case 2:

    pentagon P2;

    P2.area_of_polygon();

    break;

case 3:

    rectangle P3;

    P3.area_of_polygon();

    break;

case 4:

    square P4;

    P4.area_of_polygon();

    break;

case 5:

    parallelogram P5;

    P5.area_of_polygon();

    break;

default:

    cout << "Wrong Input" << endl;

    break;

    }


    return 0;

}


Comments

Popular posts from this blog

Tic Tac Toe C++ code

Guess The Number C code

Menu Based Program using C++