Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ##This is a program for calculating "Area" of Circle, Square, Rectangle and Triangle.
- ##This program uses:
- #Condition
- #Loop
- #Array
- #Inheritance
- #Polymorphism
- #Exception
- */
- #include <iostream>
- #include <cstdlib> //Use for "Press any key to continue..."
- using namespace std;
- //Super Class
- class AreaCalculation{
- protected:
- float base, height, width, total_area;
- public:
- AreaCalculation(float base){
- this->base = base;
- }
- AreaCalculation(float high, float wide){
- height=high;
- width=wide;
- }
- virtual void calculation(){
- cout<<"Calculation going on"<<endl;
- }
- };
- //Sub Classes
- class Circle: public AreaCalculation{
- public:
- Circle(float base):AreaCalculation(base){}
- void calculation()
- {
- total_area= 3.1416*base*base;
- cout<<"Area of the circle is: "<<total_area<<"square meter<s>"<<endl;
- }
- };
- class Rectangle : public AreaCalculation{
- public:
- Rectangle(float high, float wide): AreaCalculation(high, wide){}
- void calculation()
- {
- total_area = height*width;
- cout<<"Area of the rectangle is: "<<total_area<<"square meter<s>"<<endl;
- }
- };
- class Triangle : public AreaCalculation{
- public:
- Triangle(float high, float wide): AreaCalculation(high,wide){}
- void calculation()
- {
- total_area = 0.5*height*width;
- cout<<"Area of triangle is: "<<total_area<<"square meter<s>"<<endl;
- }
- };
- class Square: public AreaCalculation{
- public:
- Square(float base): AreaCalculation(base){}
- void calculation()
- {
- total_area = base*base;
- cout<<"Area of the square is: "<<total_area<<"square meter<s>"<<endl;
- }
- };
- int main()
- {
- //For restarting the program if user wants.
- char in;
- do{
- int opt;
- cout<< "\n\n================================================================="<<endl;
- cout<< " Find the Areas of Different Geometrical Figures"<<endl;
- cout<< " ========= Select Your Desired Option =========="<<endl;
- cout<< "================================================================="<<endl;
- cout<<"\n\n1. Area of Circle\n2. Area of Rectangle\n3. Area of Triangle\n4. Area of Square\nChoose your number : ";
- cin>>opt;
- cout<<"" <<endl;
- AreaCalculation *ac;//object of super class
- int no;
- float hi, wi;
- try
- {
- if(opt == 1){
- try{ //If there is any problem found
- cout<<"How many circle<s>: ";
- cin>>no;
- cout<<"" <<endl;
- float ba[no];
- for(int i=0; i<no; i++)//including array for more than 1 conversion
- {
- cout<<"Enter the radius of circle "<<i+1<<" :";
- cin>>ba[i];
- }
- for(int i=0; i<no; i++)
- {
- if(ba>0)
- {
- Circle c1(ba[i]);//object of sub class
- ac = &c1; // referencing object of sub class to super class
- ac->calculation();
- }
- else
- {
- throw "Radius can not be less than or equal to 0(zero).";
- }
- }
- }catch(const char *msg){
- cout<<"A problem occurred. "<<msg<<endl;
- }
- }
- else if(opt == 2){
- try{
- cout<<"How many rectangle<s>: ";
- cin>>no;
- float hi[no], wi[no];
- for(int i=0; i<no;i++)
- {
- cout<<"Enter the length of the rectangle "<<i+1<<" : ";
- cin>>hi[i];
- cout<<"Enter the width of the rectangle "<<i+1<<" : ";
- cin>>wi[i];
- }
- for(int i=0; i<no;i++)
- {
- if(hi>0 && wi>0)
- {
- Rectangle rec(hi[i],wi[i]);
- ac = &rec;
- ac->calculation();
- }
- else
- {
- throw "Length and width can not be less than or equal to 0(zero).";
- }
- }
- }catch(const char *msg){
- cout<<"A problem occurred. "<<msg<<endl;
- }
- }
- else if(opt == 3)
- {
- try
- {
- cout<<"Enter the height of the triangle: ";
- cin>>hi;
- cout<<"Enter the base of the triangle: ";
- cin>>wi;
- if(hi>0)
- {
- try
- {
- if(wi>0)
- {
- Triangle tri(hi,wi);
- ac = &tri;
- ac->calculation();
- }
- else{
- throw "Base cannot be less than or equal to 0(zero).";
- }
- }catch (const char *msg){
- cout<<"\nA problem occurred. "<<msg<<endl;
- }
- }
- else
- {
- throw "Height can not be less than or equal to 0(zero).";
- }
- }catch(const char *msg){
- cout<<"\nA problem occurred. "<<msg<<endl;
- }
- }
- else if(opt == 4){
- try{
- cout<<"Enter how many square<s>: ";
- cin>>no;
- float ba[no];
- for(int i=0; i<no; i++)//including array for more than 1 conversion
- {
- cout<<"Enter the base of square no. "<<i+1<<" :";
- cin>>ba[i];
- }
- for(int i=0; i<no; i++)
- {
- if(ba>0)
- {
- Square sq(ba[i]);
- ac = &sq; // referencing object of sub class to super class
- ac->calculation();
- }
- else
- {
- throw "Base can not be less than or equal to 0(zero).";
- }
- }
- }catch(const char *msg){
- cout<<"A problem occurred. "<<msg<<endl;
- }
- }
- else{
- throw "Invalid Option. Please try again.";
- }
- }catch(const char *msg)
- {
- cout<<msg<<endl<<"Thank you.\n"<<endl;
- }
- system("PAUSE");
- cout<<"Do you want to convert any other geometric figure?(Y/N): ";
- cin>>in;
- }while(in=='y'||in=='Y');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement