HelpProblem In My C++ Book

Posts 1–3 of 3 · Page 1 of 1
Problem In My C++ Book
Alright, so I just went through a chapter of my book, and started to do the practice problems, however, there are two that I just can't seem to figure out. Here are the questions:


5.

Rewrite the Stonewt class (Listings 11.16 and 11.17) so that it has a state member that governs whether the object is interpreted in stone form, integer pounds form, or floating-point pounds form. Overload the << operator to replace the show_stn() and show_lbs() methods. Overload the addition, subtraction, and multiplication operators so that one can add, subtract, and multiply Stonewt values. Test your class with a short program that uses all the class methods and friends.



6.

Rewrite the Stonewt class (Listings 11.16 and 11.17) so that it overloads all six relational operators. The operators should compare the pounds members and return a type bool value. Write a program that declares an array of six Stonewt objects and initializes the first three objects in the array declaration. Then it should use a loop to read in values used to set the remaining three array elements. Then it should report the smallest element, the largest element, and how many elements are greater or equal to 11 stone. (The simplest approach is to create a Stonewt object initialized to 11 stone and to compare the other objects with that object.)

Listing 11.16
Code:
// 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
};
#endif
Listing 11.17
Code:
// 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";
}
On top of that, could someone explain operator overloading please.... I'm still not understanding it very well after reading through my chapter. On top of that, I don't even know when to use it or how to use it. Thanks in advanced!
Quote Originally Posted by Wikipedia
In object-oriented programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments.
I assume they want you to overload the << operator like this:
Code:
friend ostream& operator<<(ostream& out, const Stonewt& Obj) // output
{
    switch(Obj.m_nFlag)
    {
    case Stonewt::WEIGHT_STONE:
        out << Obj.m_nStone << " stones";
        break;
    ... etc
    };
    return out;
}
As for overloading the other operators they want:
Code:
Stonewt Stonewt::operator+(Stonewt& lhs, Stonewt& rhs)
{
    Stonewt tmp = *lhs;
    tmp.stones += rhs->stones;
    tmp.pounds += rhs->pounds;
    ...etc
    return tmp;
}
Wrote this in my browser so I have no idea if it's correct.
Operator overloading allows you to give functionality to whatever operator you overload.

A very common example is overloading the math operators for a vector class, making it easy to add/subtract/multiply two vector objects.

In programming when you are stuck, you must first understand what you don't understand. If you come to the simple conclusion of "I just don't understand it all", you will be lost forever.

there are two that I just can't seem to figure out.
What can't you figure out about them? What don't you understand?


Read each thing sentence by sentence. If you don't understand the sentence, don't read the next one, because its no use. Start with the first sentence:
Rewrite the Stonewt class (Listings 11.16 and 11.17) so that it has a state member that governs whether the object is interpreted in stone form, integer pounds form, or floating-point pounds form.
Do you understand it? I'll assume you don't. It wants you to modify the "Stonewt" class so that it has a member that specifies whether the object is represented in stone, integer pounds, or floating point pounds. One thing to do is to create an integer:
Code:
int m_form;
0 means stone, 1 means integer pounds, and 2 means floating point pounds.

Then you can create a method that ensures that this field member only holds either of those three values.
Code:
void setForm(int form)
{
    m_form = form;
    if (form < 0)
        form = 0;
    if (form > 2)
        form = 2;
}

Let's read the next sentence: Overload the << operator to replace the show_stn() and show_lbs() methods.

I'll assume you don't understand it. Basically it wants you to overload '<<' so that instead of calling "show_stn()" and "show_lbs()" you would call '<<'.

So first step to getting help is understanding what you need help with. "I just don't understand it" is way to vague.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us