Answer to Question #320118 in C++ for khan

Question #320118

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.


1
Expert's answer
2022-03-29T05:19:01-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog