欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 【C++ Primer Plus习题】11.5

【C++ Primer Plus习题】11.5

2024/10/25 18:30:58 来源:https://blog.csdn.net/qq_74047911/article/details/141938591  浏览:    关键词:【C++ Primer Plus习题】11.5

问题:

这里是引用
在这里插入图片描述
在这里插入图片描述

解答:
main.cpp

#include <iostream>
#include "Stonewt.h"
using namespace std;int main()
{Stonewt incognito = 275;cout << "incognito: " << incognito << endl;Stonewt wolfe(285.7);cout << "wolfe: " << wolfe <<endl;Stonewt taft(21, 8);cout << "taft: " << taft << endl;incognito = 276.8;cout << "incognito: " << incognito << endl;cout << "wolfe: " << wolfe*2.3 << endl;taft = incognito + wolfe + 200;cout << "taft: " << taft << endl;wolfe.setMode(Stonewt::FLOAT);wolfe = wolfe * 2.3;cout << "wolfe: " << wolfe * 2.3 << endl;return 0;
}

Stonewt.h

#pragma once
#include <iostream>
using namespace std;
class Stonewt
{
public:enum Mode { YIN, INT, FLOAT };
private:enum{Lbs_per_stn=14};int stone;double pds_left;double pounds;Mode mode;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();/*void show_lbs()const;void show_stn()const;*/void setMode(Mode m);Stonewt operator+(const Stonewt& s);Stonewt operator-(const Stonewt& s);Stonewt operator*(double n);friend ostream& operator<<(ostream& os, const Stonewt& s);friend Stonewt operator*(double m, Stonewt& s) { return s * m; }
};

Stonewt.cpp

#include "Stonewt.h"
#include <iostream>
using namespace std;Stonewt::Stonewt(double lbs)
{stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;mode = INT;
}
Stonewt::Stonewt(int stn, double lbs)
{stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn + lbs;mode = FLOAT;
}
Stonewt::Stonewt()
{stone = pounds = pds_left = 0;mode = YIN;
}
Stonewt::~Stonewt()
{}//void Stonewt::show_lbs()const
//{
//	cout << stone << " stone, " << pds_left << " pounds\n";
//}
//void Stonewt::show_stn()const
//{
//	cout << pounds << " pounds\n";
//}void Stonewt::setMode(Mode m)
{this->mode = m;
}
Stonewt Stonewt::operator+(const Stonewt& s)
{Stonewt temp;temp.pounds = pounds + s.pounds;temp.stone = temp.pounds / Lbs_per_stn;temp.pds_left = int(temp.pounds) % Lbs_per_stn + temp.pounds - int(temp.pounds);temp.mode = this->mode;return temp;
}
Stonewt Stonewt::operator-(const Stonewt& s)
{Stonewt temp;temp.pounds = pounds - s.pounds;temp.stone = temp.pounds / Lbs_per_stn;temp.pds_left = int(temp.pounds) % Lbs_per_stn + temp.pounds - int(temp.pounds);temp.mode = this->mode;return temp;
}
Stonewt Stonewt::operator*(double n)
{Stonewt temp;temp.pounds = pounds *n;temp.stone = temp.pounds / Lbs_per_stn;temp.pds_left = int(temp.pounds) % Lbs_per_stn + temp.pounds - int(temp.pounds);temp.mode = this->mode;return temp;
}ostream& operator<<(ostream& os, const Stonewt& s)
{if (s.mode == Stonewt::YIN){double st = s.stone + s.pds_left / Stonewt::Lbs_per_stn;os << st << " stone\n";}if (s.mode == Stonewt::INT){os << s.pounds << " pounds\n";}if (s.mode == Stonewt::FLOAT){os << s.stone << " stone, " << s.pds_left << " pounds\n";}return os;
}

运行结果:
在这里插入图片描述

考查点:

  • 枚举
  • 运算符重载
  • 友元

2024年9月5日19:57:57

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com