calculate dimensions of a circle in c++
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

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

1. Junior Member
Join Date
Jul 2011
Posts
5

## 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?
Last edited by annitaz; July 4th, 2013 at 03:48 AM.

2. Senior Member
Join Date
May 2001
Location
Germany
Posts
1,144

## 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.

3. Junior Member
Join Date
Jul 2011
Posts
5

## 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]

4. Junior Member
Join Date
Jul 2011
Posts
5

## 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 {
}```

5. Senior Member
Join Date
May 2001
Location
Germany
Posts
1,144

## 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.

6. ## 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;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•