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(){
radius = new double;
*radius = 1;
}
//
Circle(double r){
radius = new double;
*radius = r;
}
//copy constructor
Circle(const Circle& other) {
radius = new double;
*radius = *other.radius;
}
// destructor
~Circle() {
delete radius;
}
double area();
private:
double* radius;
};
double Circle::area() {
return 3.14 * (*radius) * (*radius);
}
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