Create a 'Circle' class with having a static data member name 'radius' and two pointer
data members name 'circumference' and 'area' as private. Allocate memory to the
two pointers in both parameter-less and parameterized constructors - parameterized
constructor must take a single float argument for radius. Define a deep copy constructor for
the class. Note that the constructor should assign data as well as calculate 'area' and
'circumference' based on the formulae. Two other functions with return type float
must return the value 'circumference' and 'area' are holding. Get rid of the
allocated space in destructor.
#include<iostream>
using namespace std;
#define PI 3.14159
class Circle
{
static float radius;
float* circumference;
float* area;
public:
//default
Circle()
{
circumference = new float;
*circumference = 2 * PI*radius;
area = new float;
*area = PI*radius*radius;
}
//parametrized
Circle(float _radius)
{
SetRadius(_radius);
circumference = new float;
*circumference = 2 * PI*radius;
area = new float;
*area = PI*radius*radius;
}
//deep copy
Circle(Circle& c)
{
SetRadius(c.radius);
circumference = new float;
*circumference = *(c.circumference);
area = new float;
*area = *(c.area);
}
static void SetRadius(float _radius) {radius = _radius;}
float Circumference() {return *circumference;}
float Area() { return *area; }
~Circle()
{
cout << "Destructor "<<endl;
delete area;
delete circumference;
}
};
float Circle::radius = 0;
int main()
{
Circle a;
cout<<"Circumference: "<<a.Circumference()<<endl;
cout << "Area: " << a.Area() << endl;
Circle b(20);
cout << "Circumference: " << b.Circumference() << endl;
cout << "Area: " << b.Area() << endl;
Circle c(b);
cout << "Circumference: " << c.Circumference() << endl;
cout << "Area: " << c.Area() << endl;
}
Comments
Leave a comment