Question

In: Computer Science

Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...

Note- can you rewrite the code in C++.

Circle Class c++ code 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.

Solutions

Expert Solution

Explanation:

Here is the code with a Circle class with appropriate attributes, constructors, and other functions to calculate area, diameter, circumference, etc.

also, in the main function , user is asked for the radius and the circle object is created and the details are printed.

Code:

#include <iostream>

using namespace std;

class Circle
{
double radius;
double pi = 3.14159;
  
public:
Circle()
{
radius = 0.0;
}
  
Circle(double radius)
{
this->radius = radius;
}
  
void setRadius(double r)
{
radius = r;
}
  
double getRadius()
{
return radius;
}
  
double getArea()
{
double area = pi*radius*radius;
return area;
}
  
double getDiameter()
{
double diameter = radius*2;
  
return diameter;
}
  
double getCircumference()
{
double circumference = 2*pi*radius;
return circumference;
}
  
  
  
  
  
};
int main()
{
double radius;
cout<<"Enter radius: ";
cin>>radius;
  
Circle c(radius);
  
cout<<"Area: "<<c.getArea()<<endl;
cout<<"Diameter: "<<c.getDiameter()<<endl;
cout<<"Circumference: "<<c.getCircumference()<<endl;

return 0;
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the 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
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
Can you please take this code and just rewrite it so it can be easily be...
Can you please take this code and just rewrite it so it can be easily be able to put into a complier. in java Implementation of above program in java: import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class 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 accessor function for the radius variable. • getArea. Returns the area of the...
Can you write code or class in java accepting from the user a + b as...
Can you write code or class in java accepting from the user a + b as a string and accepting their value from the user and the out but will be the sum of the variables.
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT