CIRCLE CLASS
Implement a Circle class in C++. Each object of this class will represent a circle, storing its radius and the x and y co-ordinates of its centre as floats. Include a default constructor, access functions, an area() function and a circumference() function. The program must print the co-ordinates of the centre with radius, area and circumference of the circle. Theory: This program uses the concept of class in C++. A class binds the data and its related functions together. In the following class declaration, the data members r, area, cir, x, y are private member functions. class Circle { private: int r; //Data Members float area, cir, x, y; public: Circle(); // Constructor void getdata (void); // Methods void putdata (void); void calarea (void); void calcir (void); }; Thus, data and the function are hidden, i.e. not visible to outside functions. In the main() function, we have declared an object c of class Circle. The components of this object are x, y, r, area and cir. The constructor function Circle() is used to initialize the object.
CODE: // Author : // Purpose: To implement a class Circle #include #include #define PI 3.142 class Circle { private: int r; //Data Members float area, cir, x, y; 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; } Hiddencomputertricks.blogspot.com
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; cout << "The x-cordinate of the circle is : " << x << endl; cout << "The y-cordinate of the circle is : " << y << endl; cout << "Area of the circle is : " << area << endl; cout << "Circumference of the circle is : " << cir << endl; } void Circle :: calarea(void) { area = PI * r * r; } void Circle :: calcir(void) { cir = 2 * PI * r; } main() { Circle c; // c is an object of class Circle clrscr(); cout << " \t\t * TO IMPLEMENT A CLASS CIRCLE * \t\t " << endl; c.getdata(); c.calarea(); c.calcir(); c.putdata(); getch(); return 0; } // End of the Program
Hiddencomputertricks.blogspot.com