Question

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

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*************************************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:\Users\lmali\Desktop\Chegg>g++ circle.cpp

C:\Users\lmali\Desktop\Chegg>a.exe
Area is: 113.097
Number of active object:1

C:\Users\lmali\Desktop\Chegg>

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

  

Add a comment
Know the answer?
Add Answer to:
A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Write a program using the class to find out the area of a circle and volume...

    Write a program using the class to find out the area of a circle and volume of a cylinder. [* area of a circle=πr2, the volume of a cylinder=πr2*h where h is the height of the cylinder and r is the radius of the circular end of the cylinder] 1. The member variable should be private 2. Use public member functions to execute your program. 3. Use set ( ) function to access the private members 4. Use another approach...

  • Note- can you rewrite the code in C++ if there is any existing code. Can you...

    Note- can you rewrite the code in C++ if there is any existing code. Can you not use arrow -> operator. Write a circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument . • setRadius. A mutator function...

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT