usingnamespace std;
Circle::Circle()
{
radius = 0.0;
pi = 3.14159;
}
Circle::Circle(double radius)
{
setRadius(radius);
}
void Circle::setRadius(double rad)
{
radius = rad;
}
double Circle::getRadius()
{
return radius;
}
double Circle::getArea()
{
double area = pi * (radius*radius);
return area;
}
double Circle::getDiameter()
{
return radius + radius;
}
double Circle::getCircumference()
{
return 2 * pi * radius;
}
// Demo program
int main()
{
// To hold a radius
double radius;
cout << "Enter a radius" << endl;
// Get the radius.
cin >> radius;
// Create a Circle object with the
// specified radius.
Circle circularObject(radius);
// Display the circle's data.
cout << "Radius: " << circularObject.getRadius() <<endl;
cout << "Area : " << circularObject.getArea() << endl;
cout << "Diameter: " << circularObject.getDiameter() <<endl;
cout << "Circumference: " << circularObject.getCircumference() << endl;
return 0;
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
// To hold a value for pi
double pi;
// To hold the radius
double radius;
public:
// The default constructor sets
// radius to 0.0 and pi to 3.14159.
Circle();
Circle(double radius);
// The overloaded constructor accepts
// the radius as an arguemnt.
// Mutator function for the radius
void setRadius(double rad);
// Accessor function for the radius
double getRadius();
// The getArea function returns the
// circle's area.
double getArea();
// The getDiameter function returns the
// circle's diameter.
double getDiameter();
// The getCircumference function returns
// the circle's circumference.
double getCircumference();
};
#endif
I have an instruction that says the default constructor sets the radius to 0.0 and pi to 3.14159, if i have to set pi in the overlaoded constructor then what is the point of the dafault.
yea it compiles. must have left taht off
there is two constructors, one that takes an argument and one that dosent
the one that dosent is the default. it should set the two values in the definition to what they should be. However, these values are not what they should be when the program runs, but some crazy numbers instead. c c c
#include <iostream>
usingnamespace std;
class Circle
{
private:
// To hold a value for pi
double pi;
// To hold the radius
double radius;
public:
// The default constructor sets
// radius to 0.0 and pi to 3.14159.
Circle();
Circle(double radius);
// The overloaded constructor accepts
// the radius as an arguemnt.
// Mutator function for the radius
void setRadius(double rad);
// Accessor function for the radius
double getRadius();
// The getArea function returns the
// circle's area.
double getArea();
// The getDiameter function returns the
// circle's diameter.
double getDiameter();
// The getCircumference function returns
// the circle's circumference.
double getCircumference();
};
Circle::Circle() : pi(3.14159)
{
radius = 0.0;
}
Circle::Circle(double radius) : pi(3.14159)
{
setRadius(radius);
}
void Circle::setRadius(double rad)
{
radius = rad;
}
double Circle::getRadius()
{
return radius;
}
double Circle::getArea()
{
double area = pi * (radius*radius);
return area;
}
double Circle::getDiameter()
{
return radius + radius;
}
double Circle::getCircumference()
{
return 2 * pi * radius;
}
// Demo program
int main()
{
// To hold a radius
double radius;
cout << "Enter a radius : ";
// Get the radius.
cin >> radius;
// Create a Circle object with the
// specified radius.
Circle circularObject(radius);
// Display the circle's data.
cout << "Radius: " << circularObject.getRadius() <<endl;
cout << "Area : " << circularObject.getArea() << endl;
cout << "Diameter: " << circularObject.getDiameter() <<endl;
cout << "Circumference: " << circularObject.getCircumference() << endl;
return 0;
}
Enter a radius : 25
Radius: 25
Area : 1963.49
Diameter: 50
Circumference: 157.08