Results 1 to 3 of 3
  1. #1
    bmwboi765's Avatar
    Join Date
    Jan 2006
    Gender
    male
    Posts
    129
    Reputation
    10
    Thanks
    57
    My Mood
    Twisted

    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!

  2. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,321
    My Mood
    Cheeky
    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.
    Ah we-a blaze the fyah, make it bun dem!

  3. The Following User Says Thank You to Hell_Demon For This Useful Post:

    Hassan (05-10-2012)

  4. #3
    sparkart's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    1
    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.

  5. The Following User Says Thank You to sparkart For This Useful Post:

    Hell_Demon (05-18-2012)

Similar Threads

  1. To All GunZ Down 02-07-06 PROBLEM
    By WertyRO in forum Gunz General
    Replies: 18
    Last Post: 02-09-2006, 07:41 PM
  2. Problem
    By lambda in forum Gunz General
    Replies: 3
    Last Post: 02-08-2006, 11:36 AM
  3. hacking problems
    By iwillkillyou in forum WarRock - International Hacks
    Replies: 11
    Last Post: 02-04-2006, 04:37 PM
  4. WPE problem...
    By styx23 in forum General Game Hacking
    Replies: 8
    Last Post: 01-18-2006, 07:51 PM
  5. Problem Wit Hacking Programs
    By f5awp in forum General Gaming
    Replies: 5
    Last Post: 01-10-2006, 05:44 AM