Data type methods

Data type methods make a class act like a primitive data type by giving it properties similar to those of a primitive data type.

These properties are usually implemented as overloaded operators in the class interface.

The program fragment in Listing 5 shows prototypes for methods that:

Listing 5. Data type methods.
class Date{
  int month,day,year;//private data members
  Date(int mo,int da,int yr);//public constructor
  void Display();//display the date
  void AdjustMonth(int m);// +/- m months
  int DayOfWeek();//return 0-6 = Sun-Sat
  // --- arithmetic operators
  Date operator+(int n);
  Date operator-(int n);
  int operator-(Date &dt);//note the reference operator
  // --- assignment operators
  Date& operator=(Date &dt);
  // --- relational operators
  int operator==(Date &dt);
  int operator!=(Date &dt);
  int operator<(Date &dt);
  int operator>(Date &dt);
};//note the required semicolon