In: Computer Science
C++ - When working on this assignment, focus on memorizing the syntax for writing classes.
Write a simple class named Circle, with three private variables: doubles named x, y and radius. The center of the circle is denoted by coordinates (x,y), and the radius of the circle is denoted by radius. It should have public member functions with the following signatures:
void setRadius(double r) void setX(double value) void setY(double value) double getRadius() double getX() double getY() double getArea() bool containsPoint(double xValue, double yValue)
The first six member functions are straightforward functions to set and get the private member variables.
double getArea()
This member function should return the area of the circle. When you are calculating the area you can use 3.14 for pi. The formula is radius * radius * pi.
bool containsPoint(double xValue, double yValue)
This member function should return true if the point at (xValue, yValue) is contained in the circle, and false if not. You can determine whether or not a point is in a circle by calculating the distance from the center of the circle (its x and y coordinates) to the point (parameters xValue, yValue). If this distance is less than or equal to the radius then the point is inside the circle. For your reference, here is how to calculate distance between two points (Links to an external site.).
For example, let's say we have instantiated a Circle object named myCircle which has x=2.0, y=3.0, and radius=2.0.
Write a main function that tests your class. It should instantiate a number of circle objects with different radius values and centers. You should test all your member functions until you are confident that they work. At minimum, make sure you try each of the following and provide sample output for each of the below test cases:
Implement the Circle class in separate .h and .cpp files. Circle.h contains your class declaration and Circle.cpp contains your class implementation, i.e. the member function implementations. Don't forget the #ifndef and #define preprocessor directives as necessary. Put your main program into a separate file as well called main.cpp, and put your sample output at the end of main.cpp.
To summarize, at the end of the assignment you should have three files with the following names:
When you are finished, create a zip file containing these three files, and submit the zip file via Canvas. Do not submit the three files separately.
Here is C++ Code:
Note: Please put all files in same directory.
circle.h
// File circle.h
// Circle class definition
#ifndef CIRCLE_H
#define CIRCLE_H
class circle
{
public:
// Member Functions
// constructor
circle();
// Set center coordinates
void setCoord(double, double);
// Set radius
void setRadius(double);
// Compute the area
void computeArea();
// Compute the perimeter
void computePerimeter();
// Display attributes
void displayCircle() const;
// accessor functions
double getX() const;
double getY() const;
double getRadius() const;
double getArea() const;
double getPerimeter() const;
// setters
void setX(double);
void setY(double);
bool containsPoint(double, double);
private:
// Data members (attributes)
double x;
double y;
double radius;
double area;
double perimeter;
};
#endif // CIRCLE_H
circle.cpp
// File circle.cpp
// Circle class implementation
#include "circle.h"
#include <iostream>
using namespace std;
const double pi = 3.14159;
// Member Functions...
// constructor
circle::circle()
{
x = 0;
y = 0;
radius = 0;
area = 0.0;
perimeter = 0.0;
}
// Set center position
void circle::setCoord(double xArg, double yArg)
{
x = xArg;
y = yArg;
}
// Set radius
void circle::setRadius(double r)
{
radius = r;
computeArea();
computePerimeter();
}
// Compute the area
void circle::computeArea()
{
area = pi * radius * radius;
}
// Compute the perimeter
void circle::computePerimeter()
{
perimeter = 2 * pi * radius;
}
// Setters
void circle::setX(double value)
{
x = value;
}
void circle::setY(double value)
{
y = value;
}
// check if circle have point
bool circle::containsPoint(double xValue, double yValue)
{
if ((xValue - getX()) * (xValue - getX()) +
(yValue - getY()) * (yValue - getY()) <=
getRadius() * getRadius())
return true;
else
return false;
}
// Display attributes
void circle::displayCircle() const
{
cout << "x-coordinate is " << x << endl;
cout << "y-coordinate is " << y << endl;
cout << "area is " << area << endl;
cout << "perimeter is " << perimeter << endl;
}
// accessor functions
double circle::getX() const
{
return x;
}
double circle::getY() const
{
return y;
}
double circle::getRadius() const
{
return radius;
}
double circle::getArea() const
{
return area;
}
double circle::getPerimeter() const
{
return perimeter;
}
main.cpp
// File circletest.cpp
// Tests the Circle class
#include "circle.h"
#include "circle.cpp"
#include <iostream>
using namespace std;
int main()
{
circle myCircle;
// Set circle attributes.
myCircle.setCoord(2.0, 3.0);
myCircle.setRadius(2.0);
// Compute area and perimeter
myCircle.computeArea();
myCircle.computePerimeter();
// Display the circle attributes.
cout << "The circle attributes follow:" << endl;
myCircle.displayCircle();
// Check points
if (myCircle.containsPoint(2.0, 4.0))
{
cout << "Circle contains this point.";
}
else
{
cout << "Circle does not contain this point";
}
if (myCircle.containsPoint(2.0, 10.0))
{
cout << "Circle contains this point.";
}
else
{
cout << "Circle does not contain this point";
}
return 0;
}
To compile:
g++ main.cpp
./a
Output: