2010 Lab-13

Lab elements include:
 • Classes
 • Overloaded operators such as ++, --
 • Friend functions
 • Overload your own << operator to display a class.

Getting started

First do this...

    Log on to Odin server
    Run the following command...
      /home/fac/gordon/p/2010/semester-start.sh

Now start the lab...

Start with your 2010/b/save-dates.cpp program.

Copy it to 2010/d/lab13.cpp

    $ cd
    $ cd 2010/d
    $ cp ../b/save-dates.cpp lab13.cpp


Date class changes

Add the following elements to your class...

  1. ++ Prefix and postfix increment operators.
		These operators will increment the object's day member.

  2. -- Prefix and postfix decrement operators.
		These operators will decrement the object's day member.

  3. - Subtraction operator.
       This operator will subtract one date from another date. The result will
       be the number of days between two dates.
       For example, if April 10, 2025 is subtracted from April 18, 2025,
       the result will be 8.

       We will discuss the method for counting days between dates.

  4. << cout's stream insertion operator. This operator should
        cause the date to be displayed in the form April 18, 2025
        Example: cout << date1 << endl;

  5. >> cin's stream extraction operator. This operator should
        prompt the user for a date to be stored in a Date object.
        Example: cin >> date1;
                 then the user will enter the 3 date components.

  6. You may also overload the...
        < less than operator.
        > greater than operator.
        Use them to determining which date comes before the other.

Sample output will look like this...

Lab-13 Days between dates program --------------------------------- Please enter month, day, year as numbers: 11 18 2025 now enter another date: 11 21 2025 Get days between: November 18, 2025 and November 21, 2025 Calculating days between dates... Number of days is: 3 end of program.

References:

Chapter-14 pg. 838  -  Overloading Math Operators

Chapter-14 pg. 843  -  Overloading the Prefix ++ Operator
                       Overloading the Postfix ++ Operator

Chapter-14 pg. 848  -  Overloading the << and >> Operators


Your main function will look something like this:

main() { Date mydate1, mydate2; cout << "Please enter a date: "; cin >> mydate1; cout << "enter another date: "; cin >> mydate2; cout << "Get days between: " << mydate1; cout << " and " << mydate2 << endl; cout << "Calculating now..." << endl; //Days between dates was not done this way during lab. //cout << "Number of days is: " << mydate2 - mydate1 << endl; //It was done this way... int count = 0; while (!(mydate1 == mydate2)) { ++mydate1; ++count; } cout << "Number of days is: " << count << endl; //============================================================= //Extra credit for using the postfix ++ operator. //Replace the code just above. int count = 0; while (!(mydate1 == mydate2)) { mydate1++; count++; } cout << "Number of days is: " << count << endl; //============================================================= return 0; }
There are many correct ways to write the program logic. Try to use all your overloaded operators. Program to be collected: lab13.cpp