Answer to Question #173445 in C++ for JOTHIBASS S

Question #173445

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


1
Expert's answer
2021-03-22T02:22:27-0400
#include <iostream>
using namespace std;

class Circle {
	double* radius;
public:
	Circle(double r){
		radius = new double;
		*radius = r;
	};
	//copy constructor
	Circle(const Circle& circle)
	{
		radius = new double;
		*radius = *circle.radius;
	};

	~Circle() {
		delete radius;
	};


	double area() { return 3.14 * (*radius) * (*radius); }
};


int main()
{
	Circle c1(5.0);
	Circle c2(c1);  	//copy constructor

	cout << "area (c1) = " << c1.area() << endl;
	cout << "area (c2) = " << c2.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
APPROVED BY CLIENTS