c++
* Be sure to add your name as a cout in the first lines of each program – else 0 credit.
* Add constructors – a default and parameterized constructor to each.
* Write an .h interface and a .cpp implementation for each class
* Write an Drive/Test file that tests the constructors and functions
* Write a UML class diagram for each class
cout << “Name: YOURNAME – Program Name: PROGRAM NAME – Date: MMDDYY” << endl;
Example: cout << “Name: Jon Smith – Program Name: Prog9Box – Date: 6/28/17” << end;
Program 7 – Circle
You write ALL the code,
then run it – Produce the correct output. Turn in code and screen print of successful run, for credit
* Write a class for a Circle
* Input only the radius.
* Write functions that Calculate the circles Circumference, Area and Diameter, and print out the value of the radius
* Include error checking for radius, must be greater than zero
Expert Answer
/* The Circle class Header (Circle.h) */
#include <iostream>
using namespace std;
// Circle class declaration
class Circle {
private: // Accessible by members of this class only
// private data members (variables)
double radius;
public:
// Declare prototype of member functions
// Constructor with default values
Circle(double radius);
// Public getters & setters for private data members
double getRadius() ;
void setRadius(double radius);
// Public member Function
double getDiameter();
double getArea();
double getCircumference();
};
/* The Circle class Implementation (Circle.cpp) */
#include “Circle.h”
// Constructor
Circle::Circle(double r)
{
if(r < 0)
{
cout << “Radius cannot be negativen”;
radius = 0;
}
else
{
radius = r;
}
}
// Public getter for private data member radius
double Circle::getRadius()
{
return radius;
}
// Public setter for private data member radius
void Circle::setRadius(double r)
{
if(r < 0)
{
cout << “Radius cannot be negativen”;
radius = 0;
}
else
{
radius = r;
}
}
// A public member function
double Circle::getDiameter()
{
return 2*radius;
}
// A public member function
double Circle::getArea()
{
return radius*radius*3.14159265;
}
// A public member function
double Circle::getCircumference()
{
return 2*radius*3.14159265;
}
/* A test driver for the Circle class (TestCircle.cpp) */
#include <iostream>
#include “Circle.h” // using Circle class
using namespace std;
int main()
{
// Construct an instance of Circle c1
Circle c1(4.2);
cout << “nRadius =” << c1.getRadius() << ” nDiameter =” << c1.getDiameter() << ” nArea =” << c1.getArea() << “nCircumference = ” << c1.getCircumference() << endl;
double r;
cout << “nnEnter Radius: “;
cin >> r;
Circle c2(r);
cout << “Radius =” << c2.getRadius() << ” nDiameter =” << c2.getDiameter() << ” nArea =” << c2.getArea() << “nCircumference = ” << c2.getCircumference() << endl;
return 0;
}
/*
output:
Radius =4.2
Diameter =8.4
Area =55.4177
Circumference = 26.3894
Enter Radius: -2
Radius cannot be negative
Radius =0
Diameter =0
Area =0
Circumference = 0
*/