##
Re: calculate dimensions of a circle in c++

1) There is no need to have two constructors in this case. Just have one with a default argument of 0.

2) getRadius should be const as it only returns a value

3) isBigger would usually be implemented via overloading the > operator. Also the <, == and != operators would also be overloaded allowing statements like c1 > c2 and c2 != c1. A possible way of doing this is

Code:

#include <iostream>
using namespace std;
const double M_PI = 3.1416;
class Circle {
public:
//constructors
Circle(double r = 0);
//setter
void setRadius(double r);
//getter
double getRadius() const;
//calculate the diameter of a circle
double computeDiameter() const;
//calculate the area of a circle
double computeArea() const;
//calculate the Circumference of a circle
double computeCircumference() const;
//checks if radius of circle is bigger
bool isBigger(const Circle& other) const;
//operator overloads
bool operator> (const Circle& other) const;
bool operator< (const Circle& other) const;
bool operator== (const Circle& other) const;
bool operator!= (const Circle& other) const;
private:
//private data members
double m_Radius;
};
Circle::Circle(double r)
{
m_Radius = r;
}
void Circle::setRadius(double r)
{
m_Radius = r ;
}
double Circle::getRadius() const
{
return m_Radius;
}
double Circle::computeDiameter() const
{
return 2 * m_Radius;
}
double Circle::computeArea() const
{
return m_Radius * m_Radius * M_PI;
}
double Circle::computeCircumference() const
{
return 2 * m_Radius * M_PI;
}
bool Circle::isBigger(const Circle& other) const
{
return m_Radius > other.m_Radius;
}
bool Circle::operator >(const Circle& other) const
{
return m_Radius > other.m_Radius;
}
bool Circle::operator <(const Circle& other) const
{
return m_Radius < other.m_Radius;
}
bool Circle::operator ==(const Circle& other) const
{
return m_Radius == other.m_Radius;
}
bool Circle::operator !=(const Circle& other) const
{
return m_Radius != other.m_Radius;
}
int main(){
Circle c1,c2; // input
c1.setRadius(3);
c2.setRadius(7);
cout << "Circle with radius " << c1.getRadius() << " has: " << endl;
cout<< "Diameter " << c1.computeDiameter() << " cm " << endl;
cout<< "Area " << c1.computeArea() << " cm" << endl;
cout<< "Circumference " << c1.computeCircumference()<< " cm " << endl << endl;
cout << "Circle with radius " << c2.getRadius() << " has: " << endl;
cout << "Diameter " << c2.computeDiameter() << " cm " <<endl;
cout << "Area " << c2.computeArea() << " cm" <<endl;
cout << "Circumference " << c2.computeCircumference() << " cm " << endl << endl;
if (c1.isBigger(c2))
cout << "circle 1 is bigger" << endl;
else
cout << "circle 2 is bigger" << endl;
if (c1 < c2)
cout << "circle 2 is bigger" << endl;
else
cout << "circle 1 is bigger" << endl;
if (c1 != c2)
cout << "circle 1 is not equal to c2" << endl;
else
cout << "circle 1 is equal to c2" << endl;
return 0;
}

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.