6.重新编写 Stonewt类(程序清单 11.16和程序清单11.17),重载全部6个关系运算符。运算符对 pounds成员进行比较,并返回一个bool值。
编写一个程序,它声明一个包含6个Stonewt对象的数组,并在数组声明中初始化前3个对象。然后使用循环来读取用于设置剩余3个数
组元素的值。接着报告最小的元素、最大的元素以及大于或等于11英石的元素的数量(最简单的方法是创建一个 Stonewt对象,
并将其初始化为11英石,然后将其同其他对象进行比较)。
关系运算符,有6种关系,分别为小于、大于、 小于等于 、 大于等于 、 等于 、不等于
所以这里就重写这六种关系运算符,
头文件定义看一下
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum { Lbs_per_stn = 14 }; //pouns per stone 转换因子
int stone; //whole stone 英石
double pds_left; //fractional pounds
double pounds; //entire weight in pounds 磅
public:
Stonewt(double lbs); //constructor for double pounds
Stonewt(int stn, double lbs); //constructor for stone,lbs
Stonewt(); //default constructor
~Stonewt();
void show_lbs()const; //show weight in pounds format
void show_stn()const; //show weight in stone format
bool operator<(Stonewt& t);
bool operator<=(Stonewt& t);
bool operator>(Stonewt& t);
bool operator>=(Stonewt& t);
bool operator=(Stonewt& t);
bool operator!=(Stonewt& t);
};
#endif // !STONEWT_H_
实现的方法
//stonewt.cpp -- for pe11-6
#if 1
#include<iostream>
#include"stonewt.h"
using std::cout;
//construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;//integer division
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
//construct Stonewt object from stone,double values
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
//default construct Stonewt wt = 0
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
//destructor
Stonewt::~Stonewt()
{
}
//show weight in stones
void Stonewt::show_stn()const
{
cout << stone << " stone, " << pds_left << " pounds\n";
}
//show weight in pounds
void Stonewt::show_lbs()const
{
cout << pounds << " pounds\n";
}
//重载关系运算符
bool Stonewt::operator<(Stonewt& t)
{
return this->pounds < t.pounds;
}
bool Stonewt::operator<=(Stonewt& t)
{
return this->pounds <= t.pounds;
}
bool Stonewt::operator>(Stonewt& t)
{
return this->pounds > t.pounds;
}
bool Stonewt::operator>=(Stonewt& t)
{
return this->pounds >= t.pounds;
}
bool Stonewt::operator==(Stonewt& t)
{
return this->pounds == t.pounds;
}
bool Stonewt::operator!=(Stonewt& t)
{
return this->pounds != t.pounds;
}
#endif // 0
测试函数
#pragma region 第十一章练习6.cpp
程序清单
//xxx.cpp -- xxx
#if 1
#include <iostream>
#include"stonewt.h"
using namespace std;
int main()
{
Stonewt stonewtArr[6] = { 1,2,3,0,0,0 };
for (int i = 0; i < 3; i++)
{
stonewtArr[i + 3] = int(11 + i);
}
for (int i = 0; i < 6; i++)
{
stonewtArr[i].show_lbs();
}
Stonewt stTemp(11);
Stonewt stMax(-9999);
Stonewt stMin(9999);
int cntNum = 0;
stTemp.show_lbs();
stMax.show_lbs();
stMin.show_lbs();
for (int i = 0; i < 6; i++)
{
if (stTemp >= stonewtArr[i])
{
cntNum++;
}
if (stonewtArr[i]>stMax)
{
stMax = stonewtArr[i];
}
if (stonewtArr[i]<stMin)
{
stMin = stonewtArr[i];
}
}
cout << "最大值,最小值,和大于11石英(重载是的磅,所以这里暂时处理磅)的数量分别为";
stMax.show_lbs();
stMin.show_lbs();
cout << cntNum << endl;
}
#endif
#pragma endregion
标签:lbs,PrimerPlus,pounds,int,stn,show,C++,运算符,Stonewt
From: https://blog.csdn.net/zhyjhacker/article/details/139407564