Create the class Circle with attribute radius to find the area of circle. Assign the values for data member using copy constructor and display the result. Finally free the resources of data objects using destructor member function. (Note: Area of circle = 3.14 * radius * radius)
#include <iostream>
using namespace std;
class Circle {
public:
// default constructor
Circle(){
// Dynamically allocate t of memory.
radius = new double; //attribute
*radius = 1;
}
// parameterized constructor
Circle(double r){
// Dynamically allocate t of memory.
radius = new double;
*radius = r;
}
//copy constructor
Circle(const Circle& other) {
radius = new double;
*radius = *other.radius;
}
// destructor
// free resources of data objects
~Circle() {
delete radius;
}
// Member function
double area();
private:
double* radius;
};
double Circle::area() {
return 3.14 * (*radius) * (*radius);
}
// Main function for the program
int main()
{
Circle firstCicle(10);
Circle secondCicle(firstCicle); //copy constructor
Circle thirdCicle(secondCicle); //copy constructor
cout << "Area firstCicle = " << firstCicle.area() << endl;
cout << "Area secondCicle = " << secondCicle.area() << endl;
cout << "Area thirdCicle = " << thirdCicle.area() << endl;
return 0;
}
Comments
Leave a comment