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:
int getArea( void ){
return 3.14 * *ptr * *ptr;
}
Circle( int r ){
cout << "Normal Constructor Called" << endl;
ptr = new int;
*ptr = r;
}
Circle( const Circle &obj){
cout << "Copy Constructor Called." << endl;
ptr = new int;
*ptr = *obj.ptr;
}
~Circle(){
cout << "Destructor Called" << endl;
delete ptr;
}
private:
int *ptr;
};
void display(Circle obj) {
cout << "Area of Circle : " << obj.getArea() <<endl;
}
int main() {
Circle circle(7);
display(circle);
return 0;
}
Comments
Leave a comment