Question

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 for the radius variable.

• getRadius. An acccssor function for the radius variable

. • getArea. Returns the area of the circle, which is calculated as area = pi * radius * radius

• getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2

• getcircumforence. Returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius

Write a program that demonstrates the circle class by asking the user for the circle's radius, creating a circle object, and then reporting the circle's area, diameter, and circumference.

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

CPP program :

//These are header files
#include <iostream>
using namespace std;
//CPP class
class circle{
//member variables
double radius;
double pi=3.14159;
public:
//Default Constructor
circle()
{
//A default constructor that sets radius to 0.0.
radius=0.0;
}
//Constructor. Accepts the radius of the circle as an argument .
circle(double r)
{
radius=r;
}
//setRadius. A mutator function for the radius variable.
void setRadius(double r)
{
radius=r;
}
//getRadius. An acccssor function for the radius variable
double getRadius()
{
return radius;
}
//getArea. Returns the area of the circle, which is calculated as area = pi * radius * radius
double getArea()
{
return pi*radius*radius;
}
//getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2
double getDiameter()
{
return radius*2;
}
// getcircumforence. Returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius
double getcircumforence()
{
return 2*pi*radius;
}
};
//main() method
int main()
{
//This variable will store radius
double radius;
//This line will ask user radius
cout<<"Enter radius : ";
cin>>radius;//reading radius
//This line will create object of circle class
circle c;
//This line will call method to set the radius
c.setRadius(radius);
//This line will call method and display area of circle
cout<<"Area of circle with radius "<<c.getRadius()<<" is "<<c.getArea()<<endl;
//This line will call method and display diameter of circle
cout<<"Diameter of circle with radius "<<c.getRadius()<<" is "<<c.getDiameter()<<endl;
//This line will call method and display circumference of circle
cout<<"Circumference of circle with radius "<<c.getRadius()<<" is "<<c.getcircumforence()<<endl;

return 0;
}
======================================

Output :

input Enter radius : 10 Area of circle with radius 10 is 314.159 Diameter of circle with radius 10 is 20 Circumference of cir

Add a comment
Know the answer?
Add Answer to:
Note- can you rewrite the code in C++ if there is any existing code. Can you...
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
  • 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)...

  • Given the below UML diagram, convert the diagram into working java code. Pay attention to the...

    Given the below UML diagram, convert the diagram into working java code. Pay attention to the constructors of the class "Circle". Circle radius: 2 pi Circle() circle (float) getRadius(): float getArea(): float getDiameter(): float 1. The no-argument constructor initiates "pi" to 3.14 2. The one argument constructor initiates variable "radius" to the value of parameter passed 3. getRadius (), returns the radius of the Circle. 4. get Area (), returns the area of the Circle. 5. getDiameter(), returns the diameter...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • U8ing separate files, write th definition for a clas called Poit and a class called Cirele...

    U8ing separate files, write th definition for a clas called Poit and a class called Cirele 1. The class point has the data members (double) and y (double) for the x and y coordinates The class has the following ember 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 th the data member d) double get yO to zeturn the the y...

  • 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...

  • #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;...

  • 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...

  • 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...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

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