// sample solution to lab8, problem 3
#include
/*
Define a class to represent a circle. The properties or variables defining
a circle should include the x and y coordinates of the center point, and
a value representing the radius (this is all that's necessary to define a
circle mathematically). All variables should be declared as doubles.
These variables should be declared in the private section of the class.
*/
class circle
{
private:
double x;
double y;
double radius;
// The public section of the class should contain the following:
public:
// a. the constructor. remember the rules for writing the constructor:
// it must have the same name as the class, no return value is specified,
// not even void. And it should initialize the variables of the class.
circle(double a, double b, double c)
{ x = a; y = b; radius = c; }
// b. a function move that takes two parameters and adds them to the
// x and y center coordinates respectively. This will shift the
// center of the circle. Now think: does this function need to
// return a value?
void move(double dx, double dy)
{ x = x + dx;
y = y + dy;
}
// c. a function diameter that returns the diameter of the circle
// (2*radius). Now think: does this function require a parameter?
double diameter()
{ return 2.0*radius; }
// d. a function that returns the circumference of the circle
// (diameter * 3.1415927). This function should call the diameter
// function you defined above to calculate the diameter.
double circumference()
{ return 3.1415927*diameter(); }
// e. a function stretchcircle that takes a double value representing the
// circumference of a new circle. This function is similar to resize
// expect you're given not the new radius but the new circumference.
// You have to figure out how to set the radius value of the class.
// A little bit of high school algebra is needed here.
void stretchcircle(double newcirc)
{
radius = newcirc / (2 * 3.1415927);
}
}; // end of class circle
/* Thing to remember: many of the functions defined inside of the class
will be accessing/manipulating the variables of the class (x,y,radius).
so these functions do not take in these values as parameters. They
only need to take in parameters representing additional information they
will need to complete what it needs to do.
*/
// After the definition of the class, write a main function that
// should declare at least two circle objects, then call the various
// functions you defined above on these objects. It should include cout
// and maybe cin statements to get and display test data.
int main()
{
circle mycircle(4.0, 5.0, 5.0);
circle yourcircle(2.0,3.0,8.0);
mycircle.stretchcircle(9.0);
yourcircle.move(4.5, 6.7);
cout << "my circumference is " << mycircle.circumference() << "\n";
cout << "your diameter is " << yourcircle.diameter() << "\n";
// etc ...
return 0;
}