2010 Lab-13

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

Getting started

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

Copy it to 2010/d/dates13.cpp

cd
cd 2010/b
cp dates.cpp ../d/dates13.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 data member.

  2. -- Prefix and postfix decrement operators.
        These operators will decrement the object's day data 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, 2014 is subtracted from April 18, 2014,
       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, 2014
        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 21 2023 now enter another date: 11 29 2023 Get days between: November 21, 2023 and November 29, 2023 Calculating days between dates... Got it. Number of days is: 8 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.