
July 3rd, 2013, 08:11 PM
#1
calculate dimensions of a circle in c++
Code:
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle {
public:
//constructors
Circle();
Circle(double r);
//setter
void setRadius(double r);
//getter
double getRadius();
//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
double m_Radius;
};
#endif // CIRCLE_H
#include "circle.h"
#include "math.h"
#include <iostream>
#include <QString>
#include <sstream>
using namespace std;
Circle::Circle()
{
m_Radius = 0;
}
Circle::Circle(double r)
{
m_Radius = r;
}
void Circle::setRadius(double r){
m_Radius = r ;
}
double Circle::getRadius(){
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);
}
#include <iostream>
#include <QTextStream>
#include "circle.h"
using namespace std;
int main(){
QTextStream cout(stdout);
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;
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 04:48 AM.

July 4th, 2013, 03:46 AM
#2
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...eincheaders
What is your problem with the isBigger function? It is quite forward to implement.

July 4th, 2013, 06:07 AM
#3
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 {
return m_Radius > other.m_Radius;
}[\code]

July 4th, 2013, 06:09 AM
#4
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 {
return m_Radius > other.m_Radius;
}

July 4th, 2013, 07:16 AM
#5
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, 07:22 AM
#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
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only  not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums  and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.5.5)
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
