Ask Question
30 May, 05:45

Write a program to calculate the area of circle, rectangle using a class. The program would prompt a user to select (1) circle or (2) rectangle followed by asking the necessary dimensions. Once the user enters the valid input, the program then calculates the area and prints the final value. The set () / get () methods must include data validation.

+1
Answers (1)
  1. 30 May, 06:13
    0
    The following program written in c++:

    #include

    using namespace std;

    class Circle / /define class

    {

    private : / /access modifier

    double pi, radius, area; / / set double type variables

    public:

    Circle (double radius = 0.0) / /define constructor

    {

    pi = 3.1415;

    this->radius = radius;

    }

    void setRadius (double radius) / / define void type method

    {

    if (radius < 0) { / / if the radius is negative don't take it

    cout << "Radius can't be negative." << endl;

    return;

    }

    this->radius = radius;

    }

    double getArea () / / define double type method

    {

    area = pi * radius * radius;

    return area;

    }

    };

    class Rectangle / /define class

    {

    private: / /access modifier

    double length, breadth, area; / / set double type private data types

    public:

    Rectangle (double length = 0.0, double breadth = 0.0) / / define constructor

    {

    this->length = length;

    this->breadth = breadth;

    }

    void setDimension (double length, double breadth) / / define void type method

    {

    if (length < 0 || breadth < 0) { / /set if condition, if the dimensions is negative than don't take it

    cout << "Dimensions can't be negative." << endl;

    return;

    }

    this->length = length;

    this->breadth = breadth;

    }

    double getArea () / / define double type method

    {

    area = length * breadth;

    return area;

    }

    };

    int main () / /main function

    {

    int choice; / /integer type variable

    double radius, length, breadth;

    Circle c;

    Rectangle r;

    cout << "Select (1) Circle or select (2) Rectangle : "; / / get the choice of user

    cin >> choice;

    switch (choice)

    {

    case 1 : cout << "Enter radius of circle : ";

    cin >> radius;

    c. setRadius (radius);

    cout << "Area of circle is : " << c. getArea ();

    break;

    case 2 : cout << "Enter dimensions of rectangle : ";

    cin >> length >> breadth;

    r. setDimension (length, breadth);

    cout << "Area of rectangle is : " << r. getArea ();

    break;

    default : cout << "Invalid Selection ... ";

    }

    return 0;

    }

    Explanation:

    Here, we define two classes "Circle" and "Rectangle"

    Then, we create first two function which get the value from the user.

    Then, we create the second two functions which return the calculation of the following formulas.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program to calculate the area of circle, rectangle using a class. The program would prompt a user to select (1) circle or (2) ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers