Question

In: Computer Science

in c++ a Circle should be created with the default constructor, then its area, diameter, and...

in c++ a Circle should be created with the default constructor, then its area, diameter, and circumference printed to the screen for verification that it has the correct state and those respective methods work correctly. Then, output of the state should be repeated after calling each mutator to ensure that the object’s new state reflects the change

Create a Circle class with the class implementation in a file named circle.cpp and the class declaration in a file named circle.h (or circle.hpp) with

- private data member for double parameter: ‘radius’

- a default constructor which has no parameters

- an overloaded constructor which takes in a single double parameter for ‘radius’

-accessor and mutator for ‘radius’ named getRadius(…) and setRadius(…)

- member functions to return doubles for calculated parameters: area( ), diameter( ) and circumference( )  PI should be assumed to be exactly 3.141592. You may define this as a static const double at the class scope, as a const double inside any method that uses it, or as a literal in the expression in which it’s used

Solutions

Expert Solution

Circle.hpp

#include <iostream>

using namespace std;

class Circle {

private:

double radius;

static const double PI;

public:

Circle();

Circle(double);

double getRadius();

void setRadius(double);

double area();

double diameter();

double circumference();

};

Circle.cpp

#include <iostream>

#include "Circle.hpp"

using namespace std;

const double Circle::PI = 3.141592;

Circle::Circle(){

radius = 0;

}

Circle::Circle(double radius) {

this->radius = radius;

}

double Circle::getRadius() {

return radius;

}

void Circle::setRadius(double radius) {

this->radius = radius;

}

double Circle::area() {

return PI * radius * radius;

}

double Circle::diameter() {

return 2 * radius;

}

double Circle::circumference() {

return 2 * PI * radius;

}

int main() {

Circle c(14);

cout << "Diameter = " << c.diameter() << endl;

cout << "Circumference = " << c.circumference() << endl;

cout << "Area = " << c.area() << endl;

}


Related Solutions

What is a constructor ? What is a destructor? What is a default constructor? Is it...
What is a constructor ? What is a destructor? What is a default constructor? Is it possible to have more than one default constructor?
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list } // end default constructor template <typename ItemType> int ArrayBag<ItemType>::getCurrentSize() const { return item_count_; } template <typename ItemType> bool ArrayBag<ItemType>::isEmpty() const { return item_count_ == 0; } template <typename ItemType> bool ArrayBag<ItemType>::add(const ItemType &new_entry) {    bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY); if (has_room_to_add) { items_[item_count_] = new_entry; item_count_++; } // end if return has_room_to_add;...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c public void setA(double newA) public void setB(double newB) public void setC(double newC) (6) public static QuadraticExpression scale( double r, QuadraticExpression q) returns...
Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all...
Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all that apply) Group of answer choices super(); Clothing(); Clothing.Clothing() Make no explicit call Shirt inherits Clothing. In the Shirt constructor, how is a Clothing constructor with one int parameter called? For the answer, assume that an int variable called "myInt" is in scope. Group of answer choices Clothing.Clothing(myInt); Clothing(myInt); super(myInt); Make no explicit call Shirt inherits Clothing. Both classes have their own version of...
Write a program that calculates the area and circumference of a circle. It should ask the...
Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159 Sample Output: Enter the number...
Area of Circle
Luther has a circular garden of radius 1.6 cm . If she plans to put artificial grass on it, How much area does she need to cover?
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle,...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { int geoCalc; //number selection of user Scanner get = new Scanner(System.in); //display section. System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a Cirlce."); System.out.println("2. Calculate the Area of a Rectangle."); System.out.println("3. Calculate the Area of a Triangle."); System.out.println("4. QUIT"); System.out.println("Please make a selection:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT