Programming Assignment #2: Operator Overloading

Purpose: Implement new relational operators and arithmetic-like operators to enhance the DateType class and provide console or file input/output capability through the global operators >> and  <<

 

 

*********************************************************

Step 0: Download a sample executable, a revised header file, and a new .cpp file for testing

Download this zip file containing (i) a sample executable (DateType_Program2_Demo.exe) showing what your enhanced DateType class should be able to do by the end of this assignment, (ii) a Visual C++ 2013 project folder containing in the program folder a new updated header file (DateType.h) for the enhanced DateType class we are going to implement, an empty source code file (DateType.cpp), and another source code file (DateTest.cpp) for testing the implementation of the new things in DateType class and checking the behavior of DateType objects.

 

 

*********************************************************

Step 1: Create a new project

Replace the empty DateType.cpp in the program folder you have downloaded in Step 0 above with the DateType.cpp from your project for programming #1B. You need to add C++ code into DateType.cpp to implement the enhanced version of the DateType class as described in DateType.h. So your C++ project still has the following three files:

DateType.h, which specifies all the member functions (both the old ones and the newly added) and other information in the enhanced DateType class,

DateType.cpp, which provides the implementation of the enhanced DateType class, and

DateTest.cpp, which contains the main function for testing the implementation of the enhanced DateType class.

 

 

Step 2: Explore the new things added into the DateType class

 

In DateType.h, examine the new declaration added into the DateType class such as (i) new member functions and operators and (ii) declaration of global functions as friends of the DateType class to allow these global functions to access the private data members of DateType objects. More specifically, see the following:

 

Class DateType

{

...

//**********************************************************************

  // The following are new public member functions added into DateType

  //**********************************************************************

 

  void SetRandomDate();

      //set the contents to a valid random date

 

  bool operator> (const DateType & aDate);

      //Define the relational operator >

 

  bool operator< (const DateType & aDate);

      //Define the relational operator <

 

  bool operator== (const DateType & aDate);

      //Define the relational operator ==

 

  bool operator!= (const DateType & aDate);

      //Define the relational operator !=

 

  bool operator>= (const DateType & aDate);

      //Define the relational operator >=

 

  bool operator<= (const DateType & aDate);

      //Define the relational operator <=

 

  DateType operator+ (int numDays);

      //Define the arithmetic operator + such that it will

// return a separate DateType object recording a date

// that is numDays into the future from the DateType object

// performing this operation.

//Note that the date recorded in the DateType object performing

// this operation should remain unchanged.

//For example, in the following the output should be

// 1/1/2010 and 1/3/2010 respectively.

// DateType d1(1,1, 2010)), d2;

// d2 = d1+2;

// d1.PrintDate();

// d2.PrintDate();

  DateType operator- (int numDays);

      //Define the arithmetic operator - such that it will

// return a separate DateType object recording a date

// that is numDays into the past from the DateType object

// performing this operation.

//Note that the date recorded in the DateType object performing

// this operation should remain unchanged.

//For example, in the following the output should be

// 1/3/2010 and 1/1/2010 respectively.

// DateType d1(1,3, 2010)), d2;

// d2 = d1-2;

// d1.PrintDate();

// d2.PrintDate();

 

 

  int operator- (const DateType & aDate);

      //Define the arithmetic operator - such that

// it will calculate and return the number days to go

// from the DateType object aDate passed as the argument

// to this DateType object now performing this operation.

//The number returned should be positive if aDate records

// a date earlier than that recorded in this date,

//The number returned should be negative if aDate records

// a date later than that recorded in this date,

//The number returned should be 0 if aDate records records

// the same date as that recorded in this date.

//For example, in the following, 2 should be the output generated.

// DateType d1(2,16, 2010), d2(2, 14, 2010);

// cout << d1-d2;

 

 

 

 //***********************************************************************

 // The following two new global functions as I/O operators for DateType.

 // They are NOT member functions in DateType, but are declared as friends

 //   so that they can access the private data members of DateType objects

 //   for input and output.

 //***********************************************************************

 

  friend ostream & operator<< (ostream & output, const DateType & aDate);

//For example, in the following 2/16/2010 should be the output:

// DateType d1(2,16, 2010);

// cout << d1;

//For example, in the following

// 2/16/2010 2/16/2010 should be the output:

// DateType d1(2,16, 2010);

// cout << d1 << " " << d1;

//

//For example, in the following 2 14 2010 should be written

// into the file someFile.txt.

// DateType d2(2, 14, 2010); ofstream fout;

// fout.open("someFile.txt");

// fout << d2;

//************************************************************

// Correspondingly in DateType.cpp, you should implement it as

// ostream & operator<< (ostream & output, const DateType & aDate)

// { // Your code

// // ...

// //Remember to return output in the end:

// return output;

// }

// Note that in your code you want to check

// whether output is a reference to cout or not by

// if (&output == &cout)

// If output is indeed a reference to cout,

// your code should print out day, month, year separated by '/'.

// output << month << '/' << day << '/' << year;

// If output is NOT a reference to cout,

// it is a reference to an ostream object for file output and

// your code should simply do

// output << month << ' ' << day << ' ' << year;

// write out day, month, year separated by ' '.

//************************************************************

 

 

friend istream & operator>> (istream & input, DateType & aDate);

//For example, in the following the user will be prompted

// to enter the day, month, and year as a date stored in d3

// just like how the ReadDate method should work.

// DateType d3;

// cin >> d3;

//For example, in the following the user will be prompted to

// enter two dates, first one to be stored in d1

// while the second one to be stored in d3.

// cin>> d1 >> d3;

//

//For example,

// given the contents of 2 14 2010 in the file someFile.txt,

// in the following 2 14 2010 should be read

// into the data members month, day, year of d4

// DateType d4; ifstream fin;

// fin.open("someFile.txt");

// fin >> d4;

//************************************************************

// Correspondingly in DateType.cpp, you should implement it as

// istream & operator>> (istream & input, DateType & aDate)

// { // Your code

// // ...

// //Remember to return input in the end:

// return input;

// }

// Note that in your code you want to check

// whether input is a reference to cin or not by

// if (&input == &cin)

// If input is indeed a reference to cin,

// you can simply call the ReadDate method

// to prompt the user with messages on the screen

// to enter day, month, year separately from the keyboard,

// which will check and ensure the validity of date.

// If input is NOT a reference to cin,

// (i) it is a reference to an istream object for file input and

// your code should read the information of

// day, month, and year into some local variables you declared

// through input without prompting the user such as:

// input >> tempM >> tempD >> tempYear;

// (ii) you should then check whether they compose a valid date:

// if yes, update the day, month, and year in aDate;

// if no, keep the day, month, and year in aDate unchanged.

//************************************************************

}

 

 

 

 

*********************************************************

 

Step 3: Implementation and testing

 

 

Implementation: Implement all the new member functions (and operators) in your own DateType.cpp :

 

 

Testing: The code in the main function in DateTest.cpp provide a test menu to extensively test your implementation. You should do so to make sure it works fine for each new feature mentioned above. Find another person in the class to play with your code for further testing before you submit the source code files and the self-evaluation report.