Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A. (C++) Write a class declaration named Circle , with a private member variable

ID: 3589634 • Letter: A

Question

A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set and get functions to access the radius variable, and a function named get getArea which returns the area of the circle. The area is calculated as 3.14 * radius * radius.

B. Write a constructor to the circle that accepts an argument and assign its value to the radius member variable.

C. Add a static member variable that accounts for the currently active circle objects. Show your changes in the constructor and also, write a destructor function to do just that.

Please use basic C++ code without printf and scanf. Thank you in advance!

Explanation / Answer

/*************************************circle.cpp********************************/

#include <iostream>

using namespace std;

const double PI = 3.14159;

static int numberOfObjects=0;

class Circle

{

private:

double radius;

  

public:

//default constructor

//precondition: none

//postcondition: an object has been instantiated with a default radius of 1.

Circle();

//constructor

//precondition: none

//postcondition: an object has been instantiated with a default radius of r.

Circle(double r);

//Destructor

//precondition: an object exists with a valid radius.

//postcondition: the object is destroyed

~Circle();

//Sets Radius

//precondition: an object exists with a valid radius.

//postcondition: the radius is changed to r.

void setradius(double r);

//Gets Radius

//precondition: an object exists with a valid radius.

//postcondition: the radius is changed to r.

double getradius();

//Calculate the area

//precondition: an object exists with a valid radius.

//postcondition: calculates and returns the area of the circle

double getArea();

};

  

Circle::~Circle() {

numberOfObjects--;   

delete this;

cout<<"Number of active object:"<<numberOfObjects<<endl;

}

//constructor

//precondition: none

//postcondition: an object has been instantiated with a default radius of r.

Circle::Circle(double r)

{

if (r<=0)

{

cout << "An invalid radius has been detected. " <<endl;  

radius = 1.0;

} else {

radius =r;

}

numberOfObjects++;

}

//end of class definition

//Sets Radius

//precondition: an object exists with a valid radius.

//postcondition: the radius is changed to r.

void Circle::setradius(double r)

{

//invalid input (r <= 0) is checked for in the calling function.

radius =r;

}

//Gets Radius

//precondition: an object exists with a valid radius.

//postcondition: the radius is changed to r.

double Circle::getradius()

{

return radius;

}

//Calculate the area

//precondition: an object exists with a valid radius.

//postcondition: calculates and returns the area of the circle

double Circle::getArea()

{

return (PI * (radius*radius));

}

int main()

{

  

Circle *circle = new Circle(6);

cout<<"Area is: "<<circle->getArea()<<endl;

cout<<"Number of active object:"<<numberOfObjects<<endl;

return 0;

}

/*******************************output************************************/

C:UserslmaliDesktopChegg>g++ circle.cpp

C:UserslmaliDesktopChegg>a.exe
Area is: 113.097
Number of active object:1

C:UserslmaliDesktopChegg>

Thanks a lot. Please let me know if you have any doubt.

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote