1. 程式人生 > >C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 6

C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 6

為方便起見:

  1. 需要用友元過載<<;
  2. 未考慮使用者非法輸入的情況;

既然是bool值,用法肯定是用於條件判斷,可以是if或for/while中expression部分。

Stonewt.h

// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_

class Stonewt
{
private:
    enum {Lbs_per_stn = 14};      // pounds per stone
    int stone;
// whole stones double pds_left; // fractional pounds double pounds; // entire weight in pounds public: Stonewt(double lbs); // constructor for double pounds Stonewt(int stn, double lbs); // constructor for stone, lbs Stonewt(); // default constructor
~Stonewt(); void show_lbs() const; // show weight in pounds format void show_stn() const; // show weight in stone format // Newly added: reload 6 relational operators bool operator>(Stonewt& obj); bool operator>=(Stonewt& obj); bool operator<(Stonewt&
obj); bool operator<=(Stonewt& obj); bool operator==(Stonewt& obj); bool operator!=(Stonewt& obj); // support << in main() friend std::ostream& operator<<(std::ostream& os,Stonewt& obj); }; #endif /* Two ways to reload relational operators: 1. member functions 2. friend functions Here I use member funtions */

Stonewt.cpp

// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"

// construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
    stone = int (lbs) / Lbs_per_stn;    // integer division
    pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
    pounds = lbs;
}

// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
    stone = stn;
    pds_left = lbs;
    pounds =  stn * Lbs_per_stn +lbs;
}

Stonewt::Stonewt()          // default constructor, wt = 0
{
    stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt()         // destructor
{
}

// show weight in stones
void Stonewt::show_stn() const
{
    cout << stone << " stone, " << pds_left << " pounds\n";
}

// show weight in pounds
void Stonewt::show_lbs() const
{
    cout << pounds << " pounds\n";
}

// Newly added: reload 6 relational Stonewt::operators
bool Stonewt::operator>(Stonewt& obj)
{
    if (pounds > obj.pounds)
        return true;
    else
        return false;
}

bool Stonewt::operator>=(Stonewt& obj)
{
    if (pounds >= obj.pounds)
        return true;
    else
        return false;
}

bool Stonewt::operator<(Stonewt& obj)
{
    if (pounds < obj.pounds)
        return true;
    else
        return false;
}

bool Stonewt::operator<=(Stonewt& obj)
{
    if (pounds <= obj.pounds)
        return true;
    else
        return false;
}

bool Stonewt::operator==(Stonewt& obj)
{
    if (pounds == obj.pounds)
        return true;
    else
        return false;
}

bool Stonewt::operator!=(Stonewt& obj)
{
    if (pounds != obj.pounds)
        return true;
    else
        return false;
}

std::ostream& operator<<(std::ostream& os, Stonewt& obj)
{
    os << obj.pounds;
    return os;
}

main

// main()
#include<iostream>
#include"stonewt.h"

using std::cin;
using std::cout;

int main()
{
    Stonewt stone[6] = { Stonewt(14.0),Stonewt(15.0), Stonewt(1600.0) };
    for (int i = 3; i < 6; i++)
    {
        cout << "Assign a double value for pounds of #" << i + 1 << " element: ";
        double n;
        cin >> n;
        stone[i] = Stonewt(n);
    }

    // calculate min and max
    Stonewt max = stone[0];
    Stonewt min = stone[0];


    int count = 0;// total objectives >= 11 stones
    Stonewt temp(11.0, 0.0);// a reference
    for (int i = 0; i < 6; i++)
    {
        if (stone[i] >= temp)
            count++;

        max = max > stone[i] ? max : stone[i];
        min = min < stone[i] ? min : stone[i];
    }

    cout << "\nMax element is: " << max << "\n"
         << "Min element is: " << min << "\n"
         << "There are " << count << " elements larger than " << " 11 stones.";

    cin.get();
    cin.get();
}
/*
can't declare this way:
Stonewt stone[6] = { {1,0,0},...};
that only applies to structure,but not class,because class also contains member functions.
if delcare that way,information about member function and private/public all lose.
*/