# calculate dimensions of a circle in c++

• July 3rd, 2013, 07:11 PM
annitaz
calculate dimensions of a circle in c++
Code:

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle {
public:
//constructors
Circle();
Circle(double r);
//setter
//getter
//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;**

private:
//private data members
};

#endif // CIRCLE_H

#include "circle.h"
#include "math.h"
#include <iostream>
#include <QString>
#include <sstream>
using namespace std;

Circle::Circle()
{
}

Circle::Circle(double r)
{
}

}

}

double Circle::computeDiameter()const{
}

double Circle::computeArea()const {
}

double Circle::computeCircumference()const {
}

#include <iostream>
#include <QTextStream>
#include "circle.h"
using namespace std;

int main(){
QTextStream cout(stdout);

Circle c1,c2;    // input

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;

return 0;
}

The function isBigger() returns true (or false) if the radius of the Circle instance on which the function is invoked is bigger (or smaller) than the radius of the Circle instance passed to the function.: Can someone help me to implement this function?
• July 4th, 2013, 02:46 AM
Richard.J
Re: calculate dimensions of a circle in c++
First of all, please use code tags when posting source code.

Then, never place a "using" directive in a header file. For a discussion, see this link: http://stackoverflow.com/questions/5...e-in-c-headers

What is your problem with the isBigger function? It is quite forward to implement.
• July 4th, 2013, 05:07 AM
annitaz
Re: calculate dimensions of a circle in c++
I added this code in main

[code]if(c1.isBigger(c2))
cout << "circle 1 is bigger" << endl;
else
cout << "circle 2 is bigger" << endl;[\code]

and it works. What do u think? The function is:

[code]bool Circle::isBigger(const Circle& other) const {
}[\code]
• July 4th, 2013, 05:09 AM
annitaz
Re: calculate dimensions of a circle in c++
I added this code in main

Code:

if(c1.isBigger(c2))
cout << "circle 1 is bigger"  << endl;
else
cout << "circle 2 is bigger"  << endl;

and it works. What do u think? The function is:

Code:

bool Circle::isBigger(const Circle& other) const {
}

• July 4th, 2013, 06:16 AM
Richard.J
Re: calculate dimensions of a circle in c++
Yes, that's what I was thinking of, but didn't want to post because it is so simple.
• July 4th, 2013, 06:22 AM
2kaud
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

//getter

//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;

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
};

Circle::Circle(double r)
{
}

{
}

{
}

double Circle::computeDiameter() const
{
}

double Circle::computeArea() const
{
}

double Circle::computeCircumference() const
{
return 2 * m_Radius * M_PI;
}

bool Circle::isBigger(const Circle& other) const
{
}

bool Circle::operator >(const Circle& other) const
{
}

bool Circle::operator <(const Circle& other) const
{
}

bool Circle::operator ==(const Circle& other) const
{
}

bool Circle::operator !=(const Circle& other) const
{
}

int main(){
Circle c1,c2;    // input

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;
}