Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write a C++ program to build and test the Circle class. The class uses th

ID: 654390 • Letter: P

Question

Please write a C++ program to build and test the Circle class. The class uses the Point class from the handout 3 and contains the following specifications.

1. Data members: radius and center as the Point object.

2. Default constructor: Initialize the center as the origin and radius to the unit radius.

3. Customized constructor: Initialize center and the radius based on the user preference.

4. Getter function returns the center.

5. Getter function returns the radius.

6. Setter function sets the center.

7. Setter function sets the radius.

8. A function computes and returns the area of circle. Assume the ? is the constant of 3.14159.

9. A function determines if the target circle is bigger than this circle object. The function prototype is given below.

bool i s B i g g e r ( C i r c l e t a r g e t_c i r c l e ) ;

Here is the point class from handout 3. Program needs to incorporate this point class in it.

Point Class

Please write a C++ program to build and test Point class. The Point class contains the following.

1. Data members: x and y coordinate.

2. Default constructor: initialize the x and y to the origin.

3. Customized constructor: initialize the data members.

4. Getter function for the x-coordiante.

5. Getter function for the y-coordiante.

6. A function compute and returns the distance from this point to another point at (a, b). The function prototype is given below.

double computeDistance ( double a , double b ) ;

7. A function compute and returns the distance from this point to another point. The function prototype is given below.

double computeDistance ( Poin t e n d_poi n t ) ;

Explanation / Answer

/* The Circle class (All source codes in one file) (CircleAIO.cpp) */
#include <iostream> // using IO functions
#include <string> // using string
using namespace std;

class Circle {
private:
double radius; // Data member (Variable)
string color; // Data member (Variable)

public:
// Constructor with default values for data members
Circle(double r = 1.0, string c = "red") {
radius = r;
color = c;
}

double getRadius() { // Member function (Getter)
return radius;
}

string getColor() { // Member function (Getter)
return color;
}

double getArea() { // Member function
return radius*radius*3.1416;
}
}; // need to end the class declaration with a semi-colon

// Test driver function
int main() {
// Construct a Circle instance
Circle c1(1.2, "blue");
cout << "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" << c1.getColor() << endl;

// Construct another Circle instance
Circle c2(3.4); // default color
cout << "Radius=" << c2.getRadius() << " Area=" << c2.getArea()
<< " Color=" << c2.getColor() << endl;

// Construct a Circle instance using default no-arg constructor
Circle c3; // default radius and color
cout << "Radius=" << c3.getRadius() << " Area=" << c3.getArea()
<< " Color=" << c3.getColor() << endl;
return 0;
}

--------------------------------------------------------------
--------------------------------------------------------------
/* Test function default arguments (TestFnDefault.cpp) */
#include <iostream>
using namespace std;

// Function prototype
int sum(int n1, int n2, int n3 = 0, int n4 = 0, int n5 = 0);

int main() {
cout << sum(1, 1, 1, 1, 1) << endl; // 5
cout << sum(1, 1, 1, 1) << endl; // 4
cout << sum(1, 1, 1) << endl; // 3
cout << sum(1, 1) << endl; // 2
// cout << sum(1) << endl; // error: too few arguments
}

// Function definition
// The default values shall be specified in function prototype,
// not the function implementation
int sum(int n1, int n2, int n3, int n4, int n5) {
return n1 + n2 + n3 + n4 + n5;
}


-----------------------------------------
------------------------------------------
private:
// Private boolean variable
bool xxx;
public:
// Getter
bool isXxx() const { return xxx; }

---------------------------------------------------
----------------------------------------------------
* The Circle class Implementation (Circle.cpp) */
#include "Circle.h" // user-defined header in the same directory

// Constructor
// default values shall only be specified in the declaration,
// cannot be repeated in definition
Circle::Circle(double r, string c) {
radius = r;
color = c;
}

// Public getter for private data member radius
double Circle::getRadius() const {
return radius;
}

// Public setter for private data member radius
void Circle::setRadius(double r) {
radius = r;
}

// Public getter for private data member color
string Circle::getColor() const {
return color;
}

// Public setter for private data member color
void Circle::setColor(string c) {
color = c;
}

// A public member function
double Circle::getArea() const {
return radius*radius*3.14159265;
}