stonewt.h
// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>
class Stonewt
{
public:
enum Mode {stnFormat, pdsFormat};
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode; // stnFormat or pdsFormat
public:
Stonewt(double pds, Mode m = stnFormat); // constructor for double pounds
Stonewt(int stn, double lbs, Mode m = stnFormat); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void set_stnFormat() {mode = stnFormat;}
void set_pdsFormat() {mode = pdsFormat;}
Stonewt operator+(Stonewt s) const;
Stonewt operator-(Stonewt s) const;
Stonewt operator*(double n) const;
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
stonewt.cpp
// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"
// construct Stonewt object from double value
Stonewt::Stonewt(double pds, Mode m)
{
stone = int (pds) / Lbs_per_stn; // integer division
pds_left = int (pds) % Lbs_per_stn + pds - int(pds);
pounds = pds;
mode = m;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs, Mode m)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
mode = m;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
stone = pounds = pds_left = 0;
mode = stnFormat;
}
Stonewt::~Stonewt() // destructor
{
}
Stonewt Stonewt::operator+(Stonewt s) const
{
double pds = pounds + s.pounds;
Stonewt temp(pds);
return temp;
}
Stonewt Stonewt::operator-(Stonewt s) const
{
double pds = pounds - s.pounds;
Stonewt temp(pds);
return temp;
}
Stonewt Stonewt::operator*(double n) const
{
double pds = pounds * n;
Stonewt temp(pds);
return temp;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if(s.mode==Stonewt::stnFormat)
os << s.stone << " stone, " << s.pds_left << " pounds";
else
os << s.pounds << " pounds";
return os;
}
usestonewt.cpp
//usetime3.cpp -- using the fourth draft of the Time class
//compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "stonewt.h"
int main()
{
using std::cout;
using std::endl;
Stonewt s1;
cout<<"s1 weighs "<<s1;
s1.set_pdsFormat();
cout<<",\nwhich is "<<s1<<" in whole pounds.\n";
Stonewt s2(275);
cout<<"s2 weighs "<<s2;
s2.set_pdsFormat();
cout<<",\nwhich is "<<s2<<" in whole pounds.\n";
Stonewt s3(285.7);
cout<<"s3 weighs "<<s3;
s3.set_pdsFormat();
cout<<",\nwhich is "<<s3<<" in whole pounds.\n";
Stonewt s4(21, 8);
cout<<"s4 weighs "<<s4;
s4.set_pdsFormat();
cout<<",\nwhich is "<<s4<<" in whole pounds.\n";
cout<<"\ncacluator test\n";
Stonewt temp;
temp = s2 * 4;
cout<<"s2 * 4 = "<<temp;
temp.set_pdsFormat();
cout<<",\nwhich is "<<temp<<" in whole pounds.\n";
temp = s3 + 526.34;
temp.set_stnFormat();
cout<<"s3 + 526.34 = "<<temp;
temp.set_pdsFormat();
cout<<",\nwhich is "<<temp<<" in whole pounds.\n";
temp = s4 - 287;
temp.set_stnFormat();
cout<<"s4 - 287 = "<<temp;
temp.set_pdsFormat();
cout<<",\nwhich is "<<temp<<" in whole pounds.\n";
std::cin.get();
return 0;
}