C++primer plus第六版课后编程题答案11.5
2014-04-19 19:14
357 查看
Stonwth.h
main115.cpp
#ifndef STONEWT_h_ #define STONEWT_h_ #include <iostream> using namespace std; enum Mode{STONE,POUNDS};//方便main115,cpp class Stonewth { private: enum {Lbs_per_stn=14}; Mode mode; int stone; double pds_left; double pounds; public: Stonewth(double lbs) { mode=POUNDS;//用于控制输出什么格式 stone=int(lbs)/Lbs_per_stn; pds_left=int(lbs)%Lbs_per_stn+lbs-int(lbs); pounds=lbs; }; Stonewth(){ mode=POUNDS;//默认为镑 stone=pds_left=pounds=0; }; Stonewth(int stn,double lbs) { mode=POUNDS; stone=stn; pds_left=lbs; pounds=stn*Lbs_per_stn+lbs; }; void setMode(Mode form=STONE) { if(form==STONE) mode=STONE; else if(form==POUNDS) mode=POUNDS; else cout<<"Invail mode !"<<endl; } Stonewth operator+(const Stonewth &s)const//这里我就选最简单的了 { double newpounds=pounds+s.pounds; return Stonewth(newpounds); } Stonewth operator-(const Stonewth &s)const { double newpounds=pounds-s.pounds; return Stonewth(newpounds); } Stonewth operator*(double m)const//我这里选择重载常数* { double newpounds=pounds*m; return Stonewth(newpounds); } friend ostream& operator<<(ostream &os,const Stonewth &s) { if(s.mode==STONE) os<<s.stone<<" stone!"<<endl; else if(s.mode==POUNDS) os<<s.pounds+s.pds_left<<" pounds!"<<endl; else os<<"error!"<<endl; return os; } }; #endif
main115.cpp
#include <iostream> #include "stonewt.h" using namespace std; void main115() { Stonewth s1; Stonewth s2(10,5); Stonewth s3(1.5); //cout<<s2<<endl; s2.setMode(STONE); s3.setMode(STONE); //cout<<s2<<endl; //cout<<s1<<endl; //cout<<s2<<endl; //cout<<s3<<endl; Stonewth s4=s2-s3; cout<<s4<<endl; cin.get(); }
相关文章推荐
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- C++primer plus第六版课后编程题答案8.6
- C++primer plus第六版课后编程题答案8.7(补)
- C++primer plus第六版课后编程题答案10.7
- C++primer plus第六版课后编程题答案11.6
- C++primer plus第六版课后编程题答案12.6
- C++primer plus第六版课后编程练习答案11.5和11.6
- C++primer plus第六版课后编程题答案15.1
- C++primer plus第六版课后编程题答案7.2
- C++primer plus第六版课后编程题答案 7.8a
- C++primer plus第六版课后编程题答案8.2
- C++primer plus第六版课后编程题答案8.6
- C++primer plus第六版课后编程题答案9.2
- C++primer plus第六版课后编程题答案8.7(补)
- C++primer plus第六版课后编程题答案10.7
- C++primer plus第六版课后编程题答案12.3
- C++primer plus第六版课后编程题答案 7.8a
- C++primer plus第六版课后编程题答案8.2
- C++primer plus第六版课后编程题答案9.2