Question
Exercise 2 : There can be several constructors as long as they differ in number of parameters or data type. Alter the program so that
Exercise 2: There can be several constructors as long as they differ in number of parameters or data type. Alter the program so that the user can enter just the radius, the radius and the center, or nothing at the time the object is defined. Whatever the user does NOT include (radius or center) must be initialized somewhere. There is no setRadius function and there will no longer be a setCenter function. You can continue to assume that the default radius is 1 and the default center is (0, 0). Alter the client portion (main) of the program by defining an object sphere1, giving just the radius of 2 and the default center, and sphere2 by giving neither the radius nor the center (it uses all the default values). Be sure to print out the vital statistics for these new objects (area and circumference).
Code:
#include
using namespace std;
class Circles
{
private:
float radius;
int center_x;
int center_y;
public:
void setCenter(int x, int y);
double findArea();
double findCircumference();
void printCircleStats(); // This outputs the radius and center of the circle.
Circles(float r); // Constructor
Circles(); // Default constructor
};
const double PI = 3.14;
//you can remove this constrctor
//Client section
//Circles::Circles()
//{
// radius = 1;
//}
Circles::Circles()
{
center_x = 9;
center_y = 10;
//initialize the radius with 8
radius = 8;
}
double Circles::findArea()
{
return 3.14*radius*radius;
}
double Circles::findCircumference()
{
return 2 * 3.14*radius;
}
void Circles::printCircleStats()
{
cout << "The radius of the circle is " << radius << endl;
cout << "The center of the circle is (" << center_x << "," << center_y << ")" << endl;
}
void Circles::setCenter(int x, int y)
{
center_x = x;
center_y = y;
}
int main()
{
//Circles sphere(8);
//create an object to Circles and default constructor will call with pre assigned
//initial values
Circles sphere;
// sphere.setCenter(9,10);
sphere.printCircleStats();
cout << "The area of the circle is " << sphere.findArea() << endl;
cout << "The circumference of the circle is " << sphere.findCircumference() << endl;
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started