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:
Circle(){ // default constructor
radius = new double; // Dynamically allocate of memory.
*radius = 1;
}
Circle(double r){ // parameterized constructor
radius = new double; // Dynamically allocate t of memory.
*radius = r;
}
Circle(const Circle& other) { // copy constructor
radius = new double;
*radius = *other.radius;
}
~Circle() { // destructor
delete radius; // free resources of data objects
}
double area(){ // Member function
return 3.14 * (*radius) * (*radius);
}
private:
double* radius;
};
// Main function for the program
int main() {
double radius;
cout<<"Enter radius : " ;
cin >> radius;
Circle myCicle(radius);
Circle newCicle(myCicle); //copy constructor
cout<<endl;
cout << "Area myCicle = " << myCicle.area() << endl<<endl;
cout<<"Copy constructor: "<<endl;
cout << "Area newCicle = " << newCicle.area() << endl;
return 0;
}
Comments
Leave a comment