C++读书笔记之 关系运算符重载 大于号> 小于号< 等于号== 重载 overload
public:
static const double absError=0.0000001;
static bool IsEqual(double a, double b)
{
if (a==b)
return true;
if (fabs(a-b)<absError)
return true;
return false;
}
/**重载赋值运算符后 交换两个类的数据**/
void swap(Distance& d)
{
Distance temp;
temp=*this;
*this=d;
d=temp;
}
/*** overloaded < operator 重载小于号 ***/
bool operator <(const Distance& d)
{
if((feet - d.feet<absError)||(feet == d.feet && inches - d.inches<absError))
{
return true;
}
return false;
}
/*** overloaded > operator 重载大于号***/
bool operator > (const Distance& d)
{
if((feet -d.feet>absError)||(feet == d.feet && inches - d.inches>absError))
{
return true;
}
return false;
}
/*** overloaded == operator 重载等于号 ***/
bool operator ==(const Distance& d)
{
if(IsEqual(feet,d.feet) && IsEqual(inches,d.inches))
{
return true;
}
return false;
}
[cpp]
#include <iostream>
#include <math.h>
using namespace std;
class Distance
{
private:
double feet; // 0 to infinite
double inches;
public:
static const double absError=0.0000001;
static bool IsEqual(double a, double b)
{
if (a==b)
return true;
if (fabs(a-b)<absError)
return true;
return false;
}
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(double f, double i)
{
feet = f;
inches = i;
}
/**重新设置数据**/
void set(double f, double i)
{
feet = f;
inches = i;
}
/**重载赋值运算符后 交换两个类的数据**/
void swap(Distance& d)
{
Distance temp;
temp=*this;
*this=d;
d=temp;
}
// method to display distance
void displayDistance()
{
cout <<" Feet: " << feet << " Inches:" << inches <<endl;
}
/*** overloaded minus (-) operator ***/
Distance operator- ()
{
feet = -feet;
&nb
补充:软件开发 , C++ ,