9 Replies - 894 Views - Last Post: 04 July 2013 - 03:42 AM Rate Topic: -----

#1 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

calculate dimensions of a circle in c++

Posted 03 July 2013 - 04:08 PM

#ifndef CIRCLE_H
#define CIRCLE_H
#include <QString>
#include <string>
using namespace std;


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.: I have no idea how to implement this function. Can someone explain this in simple english?

Is This A Good Question/Topic? 0
  • +

Replies To: calculate dimensions of a circle in c++

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: calculate dimensions of a circle in c++

Posted 03 July 2013 - 04:15 PM

Quote

Can someone explain this in simple english?

You've already explained what you need in plain English? What exactly don't you understand?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

Re: calculate dimensions of a circle in c++

Posted 03 July 2013 - 04:57 PM

my question is: which is the radius of the Circle instance on which the function is invoked and which is the the radius of the Circle instance passed to the function?
Was This Post Helpful? 0
  • +
  • -

#4 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

Re: calculate dimensions of a circle in c++

Posted 03 July 2013 - 05:06 PM

I don't understand how to implement the isBigger function
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: calculate dimensions of a circle in c++

Posted 03 July 2013 - 06:22 PM

Quote

my question is: which is the radius of the Circle instance on which the function is invoked and which is the the radius of the Circle instance passed to the function?

Since this function is a class member function one instance will be the current instance and the other will be passed as the parameter other.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

Re: calculate dimensions of a circle in c++

Posted 03 July 2013 - 10:51 PM

do u mean circle1 and circle2? something like

if c1.m_Radius>c2.m_radius then c1.isBigger(c2) else c2.isBigger(c1)?

Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,606
  • Joined: 19-February 09

Re: calculate dimensions of a circle in c++

Posted 04 July 2013 - 12:15 AM

View Postannitaz, on 04 July 2013 - 12:57 AM, said:

my question is: which is the radius of the Circle instance on which the function is invoked and which is the the radius of the Circle instance passed to the function?


If you have :

int main()
{
  Circle circle_A(1);
  Circle circle_B(2);

  if( circle_A.isBigger( circle_B ) )
    cout << "circle A is bigger"  << endl;
  else
    cout << "circle B is bigger"  << endl;

  return(0);
}




Relating it to the above code the function looks like

bool Circle::isBigger(const Circle& other) const
{
  // the object that the function belongs to is circle A
  // the object parameter called other, is circle B
}


Was This Post Helpful? 0
  • +
  • -

#8 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

Re: calculate dimensions of a circle in c++

Posted 04 July 2013 - 02:23 AM

Is this correct?

bool Circle::isBigger(const Circle& other) const
{
    return m_Radius > other.m_Radius;
}


Was This Post Helpful? 0
  • +
  • -

#9 annitaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 10

Re: calculate dimensions of a circle in c++

Posted 04 July 2013 - 02:42 AM

I added this code in main

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:

bool Circle::isBigger(const Circle& other) const {
    return m_Radius > other.m_Radius;
}

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: calculate dimensions of a circle in c++

Posted 04 July 2013 - 03:42 AM

You should use the other's getRadius() method to get the radius.

The other way to do this is to overload the comparison operators.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1