Header Ads

Calculate Area of Different Shapes using C++ | Area of Circle Rectangle Triangle and Square using C++

 How to Calculate Area of Different Shapes using C++.How to Calculate  Area of Circle. How to Calculate  Area of Rectangle. How to Calculate  Area of Triangle. How to Calculate  Area of  Square using C++  . Use Polymorphism and Inheritance Concept and Abstract Classes


Program Source Code


#include<iostream>

using namespace std;

class Shape{

protected:

double x,y;

public:

virtual void get_data() = 0;

virtual void display_area()=0;

};

class Circle : public Shape{

public:

void get_data(){

cout<<"Enter Radius of Circle :";

cin>>x;

}

void display_area(){

double ac;

ac = 3.14*x*x;

cout<<"Area of Circle is :"<<ac<<endl;

}

};

class Rectangle : public Shape{

public:

void get_data(){

cout<<"Enter Width and Length of Rectangle :";

cin>>x>>y;

}

void display_area(){

double ar;

ar = x*y;

cout<<"Area of Rectangle is :"<<ar<<endl;

}

};

class Triangle  : public Shape{

public:

void get_data(){

cout<<"Enter Height and Base of Triangle :";

cin>>x>>y;

}

void display_area(){

double at;

at = 0.5* x*y;

cout<<"Area of Triangle is :"<<at<<endl;

}

};

class Square : public Shape{

public:

void get_data(){

cout<<"Enter Side of Square :";

cin>>x;

}

void display_area(){

double as;

as = x*x;

cout<<"Area of Square is :"<<as  <<endl;

}

};

main(){

Circle cr;

int op;

char choice;

Rectangle rect;

Triangle tr;

Square sr;

Shape *sh[4];

sh[0] = &cr;

sh[1] = &rect;

sh[2] = &tr;

sh[3] = &sr;

do{

system("cls");

cout<<"===== Calculate Area of Different Shape ======"<<endl;

cout<<"1: Area of Circle "<<endl;

cout<<"2: Area of Rectangle "<<endl;

cout<<"3: Area of Triangle "<<endl;

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

    cout<<"Plz Enter Your Choice"<<endl;

    cin>>op;

switch(op){

case 1:

sh[0]->get_data();

sh[0]->display_area();

break;

case 2:

sh[1]->get_data();

sh[1]->display_area();

break;

case 3:

sh[2]->get_data();

sh[2]->display_area();

break;

case 4:

sh[3]->get_data();

sh[3]->display_area();

break;

default:

cout<<"Invalid Number You Enter "<<endl;

   }

   cout<<"Do You Want to Calculate Another Area [Yes / No] :";

   cin>>choice;

}while(choice =='Y'||choice == 'y');

}


Related Video




Related Program 

No comments

Powered by Blogger.