LAB 3 – Introduction to Classes (INDIVIDUAL
WORK)
A circle can be
described by its radius and the coordinates of its center. The circle can be changed by modifying any
or all of these three values. We can
also calculate the area of the circle.
The Circle class
will contain three data items: the radius of the circle and
the x and y coordinates of the center.
The Circle class
will also define the functions (or methods) for altering a circle, for
displaying its values,and
for calculating its area.
Complete the Circle
class and test program shown below.
Homework: add
a function to return the circumference. Add a test to the function in main.
#include <iostream> using namespace std; class Circle { public: // Default Constructor Circle(); // Constructor Circle( const int centerX, const int centerY, const int radius ); // Get the radius int get_radius(); // Set the radius of the circle void set_radius( const int aRadius ); // Get the x-coordinate of the center int get_centerX(); // Set the x-coordinate of the center void set_centerX( const int aCoordinate ); // Get the y-coordinate of the center int get_centerY(); // Set the y-coordinate of the center void set_centerY( const int aCoordinate ); // Calculate the area of the circle double area(); // Print the circle void print(); private: int myCenterX; int myCenterY; int myRadius; }; // Default Constructor Circle::Circle() { // add code here } // Constructor Circle::Circle( const int centerX, const int centerY, const int radius ) { // add code here } // Get the radius of the circle int Circle::get_radius() { // Add Code Here } // Set the radius of the circle void Circle::set_radius( const int aRadius ) { // Add Code Here } // Get the x-coordinate of the center int Circle::get_centerX() { // Add Code Here } // Set the x-coordinate of the center void Circle::set_centerX( const int aCoordinate ) { // Add Code Here } // Get the y-coordinate of the center int Circle::get_centerY() { // Add Code Here } // Set the y-coordinate of the center void Circle::set_centerY( const int aCoordinate ) { // Add Code Here } // Calculate the area of the circle double Circle::area() { // Add Code Here } // Print the circle void Circle::print() { cout << "Center: (" << myCenterX << ", " << myCenterY << ") " << "Radius: " << myRadius << endl; } int main() { // Test program for the Circle class // First test the constructors cout << "Testing Constructors" << endl; Circle firstCircle; Circle secondCircle(1, 2, 3); cout << " Using Default Constructor: " << endl; cout << " "; firstCircle.print(); cout << " Using Constructor with Parameters 1, 2, 3: " << endl; cout << " "; secondCircle.print(); cout << endl; cout << "Accessing contents of first circle using get functions: " << endl; cout << " First circle radius is " << firstCircle.get_radius() << endl; cout << " First circle center is (" << firstCircle.get_centerX() << ", " << firstCircle.get_centerY() << ")" << endl; cout << endl; cout << "Testing set functions: " << endl; cout << " Moving the first circle to the position (5, 6): " << endl; firstCircle.set_centerX(5); firstCircle.set_centerY(6); cout << " First Circle is: "; firstCircle.print(); cout << endl; cout << " Changing the radius of second circle to 10: " << endl; secondCircle.set_radius( 10 ); cout << " Second Circle is: "; secondCircle.print(); cout << endl; cout << "Testing the area function" << endl; cout << " First circle area is: " << firstCircle.area() << endl; cout << " Second circle area is: " << secondCircle.area() << endl; cout << endl; }