+2 votes
102 views

Assalam-o-Alaikum! I am a student of software engineering and learning c++. We are studying about OOP in C++ and I have to make a program to calculate Area of different shapes. The main problem in this program is How to create multiple Constructors in a Class?. The use of multi constructor is to calculate area in costructor and different shapes have different values i.e width, height and radius. So, how can I use a constructor get arguments according to the shape values. I have no idea about it. If any body have an idea, please help me. The problem statement is given bellow:

Q. Create a program which will get user's input to calculate area of a square, rectangle, circle or a triangle based on user's requirement (whichever area the user chose to calculate). You can only use constructors to calculate area of each shape. Then show output.

by (1.4k points)   | 102 views

1 Answer

0 votes

Wa_Alaikum Assalam! Hello dear! I understand your statement and created a program for you and your friends. The main solution is that you can create multiple constrcutors in any class like this:

class ClassName
{
    private: int a,b;
    public:
            //Constructor no. 1
            ClassName(int x, int y){
             //Do something
            }
            //Constructor no. 2
            ClassName(int z){
             //Do something
            }
};

The complete solution for your problem is given bellow. You have to just copy the following code and paste into Dev C++ or any I.D.E you like.

The code is:

#include<iostream>
using namespace std;
//Define a class
class FindArea{
    private: int height;
             int width;
             int radius;
             float area;

    public:
             FindArea(int r){
                area = 3.14*(r*r);
             }
             FindArea(int x, int y){
                area = x*y;
             }
             void display(){
                cout<<"The Area of given data is: "<<area;
             }

};

main(){
    int option;
    int height,width,radius;
    cout<<"1. Area of Square"<<endl;
    cout<<"2. Area Rectangle"<<endl;
    cout<<"3. Area of Circle"<<endl;
    cout<<"Select operation:  ";
    cin>>option;
    cout<<"--------------------\n";
    if(option==1 || option==2){

        cout<<"Enter Height: ";
        cin>>height;
        cout<<"Enter Width: ";
        cin>>width;

        FindArea obj1(height,width);
        obj1.display();

    }
    else if(option==3){
        cout<<"Enter Radius: ";
        cin>>radius;
        cout<<"--------------------\n";
        FindArea obj2(radius);
        obj2.display();
    }
    else{

        cout<<endl<<"Invalid Choise";

    }
}

A working screenshot is: image.png

Please keep visiting this site for your problem solutions!

Regards, M.Athar

by (3.3k points)  

Related questions

+2 votes
1 answer
asked Jun 5 by toheed (1.4k points) | 18 views
+1 vote
1 answer
+2 votes
1 answer
+1 vote
2 answers
+2 votes
3 answers
+1 vote
1 answer
asked Jun 27 by fatyma Nauman (555 points) | 50 views
+2 votes
2 answers
asked Jun 4 by toheed (1.4k points) | 101 views
+1 vote
1 answer
asked May 26 by toheed (1.4k points) | 14 views




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
118 questions
90 answers
43 comments
113 users