#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)。:我不知道如何实现这个功能。有人能用简单的英语解释一下吗?
答案 0 :(得分: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;
}
答案 1 :(得分:1)
如果你知道自己的半径(m_Radius
或getRadius()
)和另一个圆的半径(other.getRadius()
),那么你可以简单地比较它们以查看哪一个更大的半径,像这样:
bool Circle::isBigger(const Circle &other) const
{
return m_Radius > other.getRadius();
}
答案 2 :(得分:0)
举个例子,如果你有:
Circle c1;
Circle c2;
当且且仅当c1.isBigger(c2)
的半径大于c1
时,c2
应该为真。
(我可能会调用函数isBiggerThan
以便更容易大声说出来。)
答案 3 :(得分:0)
你的意思是:
bool Circle::isBigger(const Circle& other) const
{
return m_Radius > other.getRadius();
}
这只会根据给定圆的半径检查当前圆的半径(m_Radius
)。