#ifndef A_H_
#define A_H_
class Stonewt
{
private:
enum { Lbs_per_stn = 14 };
enum Mode { Stone, IntPounds, FloatPounds };
Mode m_mode;
int mm_stone;
double m_left_pbs;
double m_pounds;
public:
Stonewt();
Stonewt(double weight, Mode mode);
~Stonewt();
friend Stonewt operator +(Stonewt& temp1, Stonewt& temp2);
friend ostream& operator <<(ostream& os, Stonewt& temp);
};
#endif
//以上为头文件
#include
#include"标头.h"
using namespace std;
Stonewt::Stonewt()
{
mm_stone = 0;
m_left_pbs = 0;
m_pounds = 0;
}
Stonewt::Stonewt(double weight, Mode mode)
{
m_mode = mode;
if (m_mode == Stone)
{
mm_stone = weight;
m_left_pbs = 0;
m_pounds = weight*Lbs_per_stn;
}
else if (m_mode == IntPounds)
{
mm_stone = (int)weight / Lbs_per_stn;
m_left_pbs = (int)weight%Lbs_per_stn;
m_pounds = (int)weight*Lbs_per_stn;
}
else if (m_mode == FloatPounds)
{
mm_stone = weight / Lbs_per_stn;
m_left_pbs = (int)weight%Lbs_per_stn + weight - int(weight);
m_pounds = weight*Lbs_per_stn;
}
else
{
cout << "模式错误\n";
mm_stone = 0;
m_left_pbs = 0;
m_pounds = 0;
}
}
Stonewt::~Stonewt()
{
}
Stonewt operator +(Stonewt& temp1, Stonewt& temp2)
{
Stonewt temp;
temp.mm_stone = temp1.mm_stone + temp2.mm_stone;
return temp;
}
ostream& operator<<(ostream& os, Stonewt& temp)
{
os << "英石:" << temp.mm_stone << " 磅:" << temp.m_left_pbs << endl
<< "总英石:" << temp.m_pounds << endl;
return os;
}
//以上为类实现文件
在最后一个operator函数中无法访问 temp.mm_stone;temp.m_left_pbs; temp.m_pounds 。但是把类的定义和类方法写在同一个文件上却不抱错不知道为什么,我用的VA2015