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 {
double* radius;
public:
Circle(double r){
radius = new double;
*radius = r;
};
//copy constructor
Circle(const Circle& circle)
{
radius = new double;
*radius = *circle.radius;
};
~Circle() {
delete radius;
};
double area() { return 3.14 * (*radius) * (*radius); }
};
int main()
{
Circle c1(5.0);
Circle c2(c1); //copy constructor
cout << "area (c1) = " << c1.area() << endl;
cout << "area (c2) = " << c2.area() << endl;
}
Comments
Leave a comment