In: Computer Science
Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following:
1. Add an overloaded constructor that has a parameter for radius. Negative values should result in radius set to 1.0. (see example 7-6 page 427)
2. Add a member function calcCircumference() to compute and return the circle circumference (2 * 3.14 * radius).
3. In main() "circle1" should be instantiated with a value for radius.
// This program uses a constructor to initialize a member variable.
#include <iostream>
#include <cmath>
using namespace std;
// Circle class declaration
class Circle
{ private:
double radius;
public: // Member function prototypes
Circle();
void setRadius(double);
double calcArea();
};
// Circle member function implementation section
/********************************************
* Circle::Circle *
* This is the constructor. It initializes *
* the radius class member variable. *
********************************************/
Circle::Circle()
{ radius = 1.0;
}
/********************************************
* Circle::setRadius *
* This function validates the value passed *
* to it before assigning it to the radius *
* member variable. *
********************************************/
void Circle::setRadius(double r)
{ if (r >= 0.0)
radius = r;
// else leave it set to its previous value
}
/**********************************************
* Circle::calcArea *
* This function calculates and returns the *
* Circle object's area. It does not need any *
* parameters because it can directly access *
* the member variable radius. *
**********************************************/
double Circle::calcArea()
{ return 3.14 * pow(radius, 2);
}
/***************************************
* main *
* The main function creates and uses *
* 2 Circle objects. *
***************************************/
int main()
{
// Define a Circle object. Because the setRadius function
// is never called for it, it will keep the value set
// by the constructor.
Circle circle1;
// Define a second Circle object and set its radius to 2.5
Circle circle2;
circle2.setRadius(2.5);
// Get and display each circle's area
cout << "The area of circle1 is " << circle1.calcArea() << endl;
cout << "The area of circle2 is " << circle2.calcArea() << endl;
return 0;
}
4. In main() add two more output statements for showing the circumference of each of the circles (circle1 and circle2).
Example 7-6 on page 427 shows how to overload constructors.
Save and submit your file as pr7-5_Lab.cpp.
CODE FOR THE FOLLOWING PROGRAM:-
#include <iostream>
#include <cmath>
using namespace std;
// Circle class declaration
class Circle{
private:
double radius;
public: // Member function prototypes
Circle();
Circle(double);
void setRadius(double);
double calcArea();
double calcCircumference();
};
// Circle member function implementation section
/********************************************
* Circle::Circle *
* This is the constructor. It initializes *
* the radius class member variable. *
********************************************/
Circle::Circle()
{
radius = 1.0;
}
/********************************************
* Circle::Circle(double) *
* This is a parametrized constructor *
* that has a parameter for radius *
********************************************/
Circle::Circle(double r)
{
if(r<0){
radius=1.0;
}else{
radius=r;
}
}
/********************************************
* Circle::setRadius *
* This function validates the value passed *
* to it before assigning it to the radius *
* member variable. *
********************************************/
void Circle::setRadius(double r)
{
if (r >= 0.0)
radius = r;
// else leave it set to its previous value
}
/**********************************************
* Circle::calcArea *
* This function calculates and returns the *
* Circle object's area. It does not need any *
* parameters because it can directly access *
* the member variable radius. *
**********************************************/
double Circle::calcArea()
{
return 3.14 * pow(radius, 2);
}
/**********************************************
* Circle::calcCircumference*
* This function calculates and returns the *
* Circle object's circumference. It does not need any *
* parameters because it can directly access *
* the member variable radius. *
**********************************************/
double Circle::calcCircumference()
{
return 2*3.14*radius;
}
/***************************************
* main *
* The main function creates and uses *
* 2 Circle objects. *
***************************************/
int main()
{
// Define a Circle object. Because the setRadius function
// is never called for it, it will keep the value set
// by the constructor.
Circle circle1;
// Define a second Circle object and set its radius to 2.5
Circle circle2(2.5);
// Get and display each circle's area
cout << "The area of circle1 is " << circle1.calcArea() << endl;
cout << "The area of circle2 is " << circle2.calcArea() << endl;
// Get and display each circle's circumference
cout << "The circumference of circle1 is " << circle1.calcCircumference() << endl;
cout << "The circumference of circle2 is " << circle2.calcCircumference() << endl;
return 0;
}
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING