#C++PrimerPlus# Chapter11_Exersice5_stonewtV2

改进Example16~17中对stonewt类的定义,并检验可用性。

改动内容有:1,声明状态成员记录对象所采用的单位模式;

      2,重载+,-,*,<<运算符。

程序清单如下:


// stonewt2.h
#ifndef STONEWT2_H_
#define STONEWT2_H_

#include <iostream>

class Stonewt
{
public:
  // 状态成员
  enum Mode {STN, LBS};
private:
  enum {LbsPerStn = 14};
  Mode mode;
  int stone;
  double pdsLeft;
  double pounds;
public:
  // 构造函数和析构函数
  Stonewt();
  Stonewt(int m_stone, double m_pdsLeft);
  Stonewt(double m_pounds);
  ~Stonewt();
  // 设置数据成员值得函数
  void reset(int m_stone, double m_pdsLeft);
  void reset(double m_pounds);
  // 设置单位模式的函数
  void setStn();
  void setLbs();
  // 重载运算符的函数
  Stonewt operator+(const Stonewt& b) const;
  Stonewt operator-(const Stonewt& b) const;
  Stonewt operator*(double n) const;
  // 友元函数
  friend Stonewt operator*(double n, const Stonewt& b);
  friend std::ostream& operator<<(std::ostream& os, const Stonewt& b);
};

#endif


// stonewt2.cpp
#include "stonewt2.h"

// 定义类方法

// 定义构造函数和析构函数
Stonewt::Stonewt()
{
  mode = STN;
  stone = pdsLeft = pounds = 0.0;
}
Stonewt::Stonewt(int m_stone, double m_pdsLeft)
{
  mode = STN;
  stone = m_stone;
  pdsLeft = m_pdsLeft;
  pounds = m_stone * LbsPerStn + pdsLeft;
}
Stonewt::Stonewt(double m_pounds)
{
  mode = LBS;
  pounds = m_pounds;
  stone = int (m_pounds) / LbsPerStn;
  pdsLeft = int (m_pounds) % LbsPerStn + m_pounds - int (m_pounds);
}
Stonewt::~Stonewt()
{
}

// 定义设置数据成员值得函数
void Stonewt::reset(int m_stone, double m_pdsLeft)
{
  mode = STN;
  stone = m_stone;
  pdsLeft = m_pdsLeft;
  pounds = m_stone * LbsPerStn + pdsLeft;
}
void Stonewt::reset(double m_pounds)
{
  mode = LBS;
  pounds = m_pounds;
  stone = int (m_pounds) / LbsPerStn;
  pdsLeft = int (m_pounds) % LbsPerStn + m_pounds - int (m_pounds);
}

// 设置单位模式的函数
void Stonewt::setStn()
{
  mode = STN;
}
void Stonewt::setLbs()
{
  mode = LBS;
}

// 重载运算符的函数
Stonewt Stonewt::operator+(const Stonewt& b) const
{
  return Stonewt(pounds + b.pounds);
}
Stonewt Stonewt::operator-(const Stonewt& b) const
{
  return Stonewt(pounds - b.pounds);
}
Stonewt Stonewt::operator*(double n) const
{
  return Stonewt(pounds * n);
}

// 友元函数
Stonewt operator*(double n, const Stonewt& b)
{
  return b * n;
}
std::ostream& operator<<(std::ostream& os, const Stonewt& b)
{
  if (b.mode == Stonewt::STN)
    os << "重量为:" << b.stone << "石," << b.pdsLeft << "磅。(英石制)";
  else if (b.mode == Stonewt::LBS)
    os << "重量为:" << b.pounds << "磅。(英镑制)";
  return os;
}


// useusestonewt.cpp
#include "stonewt2.h"

int main()
{
  using std::cout;

  Stonewt object1;
  cout << "object1的重量为:" << object1 << "\n";
  object1.reset(34.2);
  cout << "object1的重量为:" << object1 << "\n";
  cout << "object1 * 3 的重量为:" << object1 * 3 << "\n";

  Stonewt object2 (2, 5.7);
  Stonewt object3 (56.3);

  cout << "object2的重量为:" << object2 << "\n";
  object2.setLbs();
  cout << "object2的重量为:" << object2 << "\n";

  cout << "object3的重量为:" << object3 << "\n";
  cout << "object2 + object3 的重量为:" << object2 + object3 << "\n";
  cout << "object3 - object2 的重量为:" << object3 - object2 << "\n";

  system("pause>nul");
  return 0;
}


结束。

posted @ 2013-05-06 08:41  庄懂  阅读(129)  评论(0编辑  收藏  举报