Can someone help, having trouble with this exercise. Thank you. Exercise #1 from
ID: 3772096 • Letter: C
Question
Can someone help, having trouble with this exercise. Thank you.
Exercise #1 from Chapter 11, page 507.
from Programming Logic and Design, Comprehensive, Joyce Farrell, 8th Edition
1. Complete the following tasks:
a. Design a class named Circle with fields named radius, area, anddiameter. Include a constructor that sets the radius to 1. Include get methods for each field, but include a set method only for the radius. When the radius is set, do not allow it to be zero or a negative number. When the radius is set, calculate the diameter (twice the radius) and the area (the radius squared times pi, which is approximately 3.14). Create the class diagram and write the pseudocode that defines the class.
b. Design an application that declares two Circles. Set the radius of one manually, but allow the other to use the default value supplied by the constructor. Then, display each Circle's values.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Circle
{
public:
double radius, area, diameter;
Circle()
{
radius=1;
}
Circle(int r)
{
radius=r;
}
double getrad()
{
return radius;
}
double getarea()
{
return (pow(radius,2)*3.14);
}
double getdia()
{
return 2*radius;
}
void setrad(double r)
{
radius=r;
}
};
void main()
{
Circle o(5),o1;
double area1=o.getarea();
double dia1=o.getdia();
cout<<"Area of first circle "<<area1<<endl;
cout<<"Diameter of first circle "<<dia1<<endl;
o1.setrad(7);
double area2=o1.getarea();
double dia2=o1.getdia();
cout<<"Area of second circle "<<area1<<endl;
cout<<"Diameter of second circle "<<dia2<<endl;
getch();
}