Problem with code on oper. overload.

This is a discussion on Problem with code on oper. overload. within the C++ Programming forums, part of the General Programming Boards category; Homework problem. The #1 requirement by the teacher is we can't adjust the main (driver) function. Must build our class ...

  1. #1
    Unregistered
    Guest

    Unhappy Problem with code on oper. overload.

    Homework problem. The #1 requirement by the teacher is we can't adjust the main (driver) function. Must build our class around it. Having a hard time with handling the NumDays Four Instantiated object. Also not sure if I did the ostream and istream functions correctly. Please help before I loose my hair.




    #include <iostream.h>
    #include <stdlib.h>

    class NumDays
    {
    private:
    float hours;
    float days;
    void calcDays(void) { days = hours / 8; }

    public:

    NumDays(float h) { hours = h; }
    NumDays();
    float GetDays();
    NumDays operator+(const NumDays &);
    NumDays operator-(const NumDays &);
    NumDays operator++(void);
    NumDays operator++(int);
    NumDays operator--(void);
    NumDays operator--(int);
    friend ostream &operator<<(ostream &, NumDays &);
    friend istream &operator>>(istream &, NumDays &);

    };


    float NumDays::GetDays(void)
    {
    calcDays();
    return days;

    }

    NumDays NumDays:perator+(const NumDays &right)
    {
    NumDays temp;
    temp.hours = hours + right.hours;
    temp.calcDays();
    return temp;
    }

    NumDays NumDays:perator-(const NumDays &right)
    {
    NumDays temp;
    temp.hours = hours - right.hours;
    temp.calcDays();
    return temp;
    }

    NumDays NumDays:perator++(void)
    {
    ++hours;
    calcDays();
    return *this;
    }

    NumDays NumDays:perator++(int)
    {
    NumDays temp(hours);
    hours++;
    calcDays();
    return temp;
    }

    NumDays NumDays:perator--(void)
    {
    --hours;
    calcDays();
    return *this;
    }

    NumDays NumDays:perator--(int)
    {
    NumDays temp(hours);
    hours--;
    calcDays();
    return temp;
    }

    ostream &operator<<(ostream &strm, NumDays &obj)
    {
    strm << obj.hours << " hour(s), " << obj.days << " day(s) ";
    return strm;
    }

    istream &operator>>(istream &strm, NumDays &obj)
    {
    cout << "One: ";
    strm >> obj.hours;
    cout << " Two: ";
    strm >> obj.hours;
    cout << "One + Two: ";
    strm >> obj.days;
    return strm;
    }



    //Either copy your source code here or
    // #include "Your header file here"
    // 14.1 Driver Program

    //Do not alter anything in the main function.
    // Instead, alter your class so it compiles using this main function.

    void main(void)
    {
    float Hours1, Hours2;

    //Prompt for the data for the first 2 objects
    cout << "Enter the number of hours for the object called One: ";
    cin >> Hours1;
    cout << "Enter the number of hours for the object called Two: ";
    cin >> Hours2;

    //Instantiate One and Two
    NumDays One(Hours1), Two(Hours2);

    cout << "One: " << One.GetDays() << " day(s)" << endl;
    cout << "Two: " << Two.GetDays() << " day(s)" << endl << endl;

    //Addition and subtraction operators
    cout << "One + Two: " << One + Two << " hour(s)" << endl;
    cout << "One - Two: " << One - Two << " hour(s)" << endl << endl;

    //Instantiate Three to the sum of One and Two
    NumDays Three(One + Two);
    //Instantiate Four
    NumDays Four;

    //Increment and decrement operators
    Four = Three++;
    cout << "Four = Three++ " << endl;
    cout << " Three: " << Three.GetDays() << " day(s)" << endl;
    cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;

    Four = ++Three;
    cout << "Four = ++Three: " << endl;
    cout << " Three: " << Three.GetDays() << " day(s)" << endl;
    cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;

    Four = Three--;
    cout << "Four = Three--: " << endl;
    cout << " Three: " << Three.GetDays() << " day(s)" << endl;
    cout << " Four: " << Four.GetDays() << " day(s)" << endl << endl;

    Four = --Three;
    cout << "Four = --Three: " << endl;
    cout << " Three: " << Three.GetDays() << " day(s)" << endl;
    cout << " Four: " << Four.GetDays() << " day(s)" << endl;

    system("PAUSE");
    }

  2. #2
    Unregistered
    Guest
    A typical overloaded insertion fucntion:

    Code:
    istream& operator >> ( istream &strm, NumDays &obj )
    {
          strm >> obj.hours >> obj.days;
          return strm;
    }

    we use strm instead of cout because it represents a generic istream object, it can read from a file or from user input. A similar method is employed for insertion.

    I havent got time to take an longer look at the rest, I'll get back to it later though

  3. #3
    Unregistered
    Guest
    Here's a reference to a pretty good discussion of overloading the various operators.

    http://www.stud.fim.ntnu.no/~oystesk/CPP/htm/ch10.htm

    I think the reason you are having trouble with

    NumDays Four;

    is that you haven't defined the default constructor:

    NumDays ();

    Declaring Four like that uses the default constructor. If the default constuctor isn't defined then neither are the variables declared in the object when the object is declared. Then when you try to use the undefined variables in the object you start getting undefined results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21