Task
Implement a Circle class. Each object of this class will represent a circle, storing its radius and the x and y coordinates of its center as floats. Include a default constructor, access functions, an area() function, and a circumference() function.
So far this is what I have done, I would greatly appreciate any help, Thank you
private:int r; //Data Members, declare r as an integer
float area, cir, x, y; //declare area, cir, x and y as floats
public:
Circle(); // Constructor
void getdata (void); // Methods
void putdata (void);
void calarea (void);
void calcir (void);
};
// Function Definitions
Circle :: Circle() // Constructor function
{
r = 0;
area = 0.0;
cir = 0.0;
x = 0.0;
y = 0.0;
}
void Circle :: getdata(void)
{
cout << "Please enter the radius of the circle ";
cin >> r;
cout << "Please enter the x co-ordinate of the center ";
cin >> x;
cout << "Please enter the y co-ordinate of the center ";
cin >> y;
}
void Circle :: putdata(void)
{
cout << endl;
cout << "Radius of the circle is : " << r << endl; //print with users input for r
cout << "The x-cordinate of the circle is : " << x << endl; //print with user input for x
cout << "The y-cordinate of the circle is : " << y << endl; //print with user input for y
cout << "Area of the circle is : " << PI * r * r << endl; //print with calculation answer for area
cout << "Circumference of the circle is : " << 2 * PI * r << endl; //print with calculation answer for circumference
}
main()
{
Circle c; // c is an object of class Circle
/* Here is where we clear the screen */
/*clrscr();*/
("cls");
/*system ("cls");*/
cout << " \t\t * TO IMPLEMENT A CLASS CIRCLE * \t\t " << endl;
c.getdata();
c.calarea();
c.calcir();
c.putdata();
#include <iostream>
#include <iostream>
#include <cmath>
#define PI 3.142
usingnamespace std;
class Circle
{
private:
int r; //Data Members, declare r as an integer
float area, cir, x, y; //declare area, cir, x and y as floats
public:
Circle(); // Constructor
Circle(float, float, int);
float getCirc();
float getArea();
};
// Function Definitions
Circle :: Circle() // Constructor function
{
r = 0;
area = 0.0;
cir = 0.0;
x = 0.0;
y = 0.0;
}
Circle::Circle(float x_, float y_, int r_) {
x=x_;
y=y_;
r=r_;
}
float Circle::getArea() {
return (float)M_PI*r*r;
}
float Circle::getCirc() {
return (float)(2*M_PI*r);
}
int main()
{
Circle c(1, 2, 3); // c is an object of class Circle
cout << "The area is " << c.getArea() << endl;
cout << "The circumference is " << c.getCirc() << endl;
return 0;
}
// End of the Program
Yes, M_PI is a known problem: some IDEs don't recognize it. You can substitute M_PI with a constant value 3.14...
For the x and y I don't understand the problem. I showed you a very simple example where I was passing 1, 2, 3 as parameters to the constructor: if you want to change them, you can ask the user to insert x, y and radius, then pass these values to the constructor.
#include <iostream>
#include <cmath>
usingnamespace std;
class Circle
{
private:
int r; //Data Members, declare r as an integer
float x, y; //declare area, cir, x and y as floats
public:
Circle(); // Constructor
Circle(float, float, int);
float getCirc();
float getArea();
};
// Function Definitions
Circle :: Circle() // Constructor function
{
r = 0;
x = 0.0;
y = 0.0;
}
Circle::Circle(float x_, float y_, int r_) {
x=x_;
y=y_;
r=r_;
}
float Circle::getArea() {
return (float)M_PI*r*r;
}
float Circle::getCirc() {
return (float)(2*M_PI*r);
}
int main()
{
float x, y;
int radius;
cout << "Insert centre x: ";
cin >> x;
cout << "Insert centre y: ";
cin >> y;
cout << "Insert radius: ";
cin >> radius;
Circle c(x, y, radius); // c is an object of class Circle
cout << "The area is " << c.getArea() << endl;
cout << "The circumference is " << c.getCirc() << endl;
return 0;
}
// End of the Program