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

如果调用函数的 Circle 实例的半径大于(或小于)传递给函数的 Circle 实例的半径,则函数 isBigger() 返回 true(或 false)。: 我不知道如何实现这个功能。有人可以用简单的英语解释一下吗?

4

4 回答 4

2

实现看起来像这样:

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

但是,使用运算符更习惯用法:

class Circle
{
    // ...
    friend bool operator >(Circle const &, Circle const &) const;
};

bool operator >(Circle const& lhs, Circle const& rhs) const
{
    return lhs.m_Radius > rhs.m_Radius;
}
于 2013-07-03T22:18:00.637 回答
1

如果您知道自己的半径 (m_RadiusgetRadius()) 和另一个圆的半径 ( other.getRadius()),那么您可以简单地比较它们以查看哪个半径更大,如下所示:

bool Circle::isBigger(const Circle &other) const
{
    return m_Radius > other.getRadius();
}
于 2013-07-03T22:15:55.527 回答
0

举个例子,如果你有:

Circle c1;
Circle c2;

当且仅当半径大于时thenc1.isBigger(c2)应该为真。c1c2

(我可能会调用该函数isBiggerThan以使其更容易大声说出来。)

于 2013-07-03T22:16:37.083 回答
0

你的意思是这样的:

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

这将简单地检查当前圆的半径 ( m_Radius) 与给定圆的半径。

于 2013-07-03T22:16:54.593 回答