Question

Exercise 2: There can be several constructors as long as they differ in number of parameters...

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<iostream>

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

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);       // parameterized Constructor

       Circles(float r, int centerX, int centerY); // parameterized constructor

       Circles();               // Default constructor

};

const double PI = 3.14;

//you can remove this constrctor

//Client section

//Circles::Circles()

//{

//   radius = 1;

//}

Circles::Circles()

{

               // initialize the Circle with default values

       center_x = 0;

       center_y = 0;

       radius = 1;

}

Circles::Circles(float r)       // parameterized Constructor with radius given

{

               radius = r;

               center_x = 0;

               center_y = 0;

}

Circles::Circles(float r, int centerX, int centerY) // parameterized constructor with radius and center given

{

               radius = r;

               center_x = centerX;

               center_y = centerY;

}

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;

*/

               Circles sphere1(2),sphere2;

               cout<<" Sphere 1 : "<<endl;

               sphere1.printCircleStats();

               cout << "The area of the circle is " << sphere1.findArea() << endl;

   cout << "The circumference of the circle is " << sphere1.findCircumference() << endl;

    cout<<" Sphere 2 : "<<endl;

    sphere2.printCircleStats();

    cout << "The area of the circle is " << sphere2.findArea() << endl;

    cout << "The circumference of the circle is " << sphere2.findCircumference() << endl;

system("pause");

               return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Exercise 2: There can be several constructors as long as they differ in number of parameters...
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
  • A) One of the problems that have been discussed in the class is to write a simple C++ program to ...

    C++ programming question will upvote A) One of the problems that have been discussed in the class is to write a simple C++ program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following int main //the radius of the...

  • A) One of the problems that have been discussed in the class is to write a...

    A) One of the problems that have been discussed in the class is to write a simple C++ program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following: int main() { double radius; double area; double circumference; //the radius...

  • #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;...

  • 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)...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • Note- can you rewrite the code in C++ if there is any existing code. Can you...

    Note- can you rewrite the code in C++ if there is any existing code. Can you not use arrow -> operator. Write a circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument . • setRadius. A mutator function...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  •     class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN,...

        class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN, RED};        Circle(int = 0, int = 0, double = 0.0, Color = UNDEFINED);        void setX(int);        void setY(int);        void setRadius(double);        void setColor(Color);        int getX() const;        int getY() const;        double getRadius() const;        Color getColor() const;           private:        int x;        int y;   ...

  • 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...

  • Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use...

    Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...

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