首先清单11.16
#ifndef STONEWTY_H_
#define STONEWTY_H_
class Stonewt
{
private:
enum{Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
public:Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void show_lbs()const;
void show_stn()const;
};
#endif // !1
其次11.17
#include “stdafx.h”
#include “vector.h”
#include
using std::cout;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_lbs() const
{
cout << stone << “stone” <<pds_left<<“pounds \n”;
}
void Stonewt::show_stn() const
{
cout << pounds << “pounds\n”;
}
最后11.18
// 11月21.cpp: 定义控制台应用程序的入口点。
//
#include “stdafx.h”
#include
#include
#include
#include"vector.h"
void display(const Stonewt&st, int n);
int main()
{
using namespace std;
Stonewt incognito = 275;
Stonewt wolfe(285.7);
Stonewt taft(21, 8);
cout << "The celebrity weighted";
incognito.show_stn();
cout << "The detective weighted";
wolfe.show_stn();
cout << "The President weighted";
taft.show_lbs();
incognito = 276.8;
taft = 325;
cout << "After dinner ,the celebrity weighted";
taft.show_stn();
cout << "After dinner ,the President weighted";
taft.show_lbs();
display(taft,2);
cout << "The wrestler weighted even more .\n";
display(422, 2);
cout << "No stone left unearned\n";
cin.get();
return 0;
}
void display(const Stonewt & st, int n)
{
for (int i = 0;i<n;++i)
{
std::cout << “Wow!”;
st.show_lbs();
}
}