Question

Please use C++. Write a class named Circle that has a double data member named radius...

Please use C++.

Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius. It should also contain the definition of an exception class named IllegalRadius, which will contain a double data member called badRadius and a constructor that takes a double and uses it to initialize badRadius. The static maxRadius data member should be initialized to 10.0.  The Circle constructor that takes a parameter should throw an IllegalRadiusException if the parameter exceeds the maxRadius.

Write a main function in a separate file. It should ask the user to input a value for the maximum radius and sets maxRadius to that value. It should then ask the user for a radius, and then create a Circle with that radius. Next it should print out the area of that Circle. Your function should use try/catch to handle the possible exception. If an IllegalRadius exception is thrown, the program should print out an error message as shown below:

Enter the maxRadius for Circles.
7.0
Enter the radius for a new Circle object.
6.0
The Circle object was successfully created.
The area of the circle is 113.097.
Enter the maxRadius for Circles.
6.0
Enter the radius for a new Circle object.
7.0
The Circle object could not be created.
A radius of 7 exceeds the maximum allowed radius.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle {
public:
    double radius;
    static double maxRadius;
    Circle();
    void setRadius(double);
    void setMaxRadius(double);
    double calcArea();
};

#endif

Circle.cpp

#include <iostream>
#include "Circle.h"

double Circle::maxRadius = 10.0;

Circle::Circle() {
    this->radius = 1.0;
}

void Circle::setRadius(double r) {
    this->radius = r;
    if (r > maxRadius) {
        throw r;
    }
}

void Circle::setMaxRadius(double mr) {
    maxRadius = mr;
}

double Circle::calcArea() {
    return (3.14159 * radius * radius);
}

таin.cpp

#include <iostream>
#include "Circle.h"

using namespace std;

int main() {
    Circle c;
    double mr, r;

    cout << "Enter the maxRadius for Circles: ";
    cin >> mr;
    c.setMaxRadius(mr);

    cout << "\nEnter the radius for a new Circle object: ";
    try {
        cin >> r;
        c.setRadius(r);
    } catch (double badRadius) {
        cerr << "The Circle object could not be created." << "\n";
        cout << "A radius of " << badRadius << " exceeds the maximum allowed radius." << endl;
        return 0;
    }

    cout << "\nThe area of the circle is " << c.calcArea() << "\n";
}
Add a comment
Know the answer?
Add Answer to:
Please use C++. Write a class named Circle that has a double data member named radius...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set...

    A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set and get functions to access the radius variable, and a function named get getArea which returns the area of the circle. The area is calculated as 3.14 * radius * radius. B. Write a constructor to the circle that accepts an argument and assign its value to the radius member variable. C. Add a static member variable that accounts for the currently active circle...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...

    Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have a default constructor that initializes the height to 10 and the radius to 2. It should have a method named surfaceArea that returns the the surface area (in square inches) of a cylinder with that...

  • U8ing separate files, write th definition for a clas called Poit and a class called Cirele...

    U8ing separate files, write th definition for a clas called Poit and a class called Cirele 1. The class point has the data members (double) and y (double) for the x and y coordinates The class has the following ember functions a) void set x (double) to set the x data member b) void set y(double) to set the y data member c) double get-x() to return th the data member d) double get yO to zeturn the the y...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT