Date class: year, month and day : Date « Data Type « C++






Date class: year, month and day

Date class: year, month and day


#include <iostream>
#include <ctime>
using namespace std;

class Date
{
  private:                       
    short month, day, year;
  public:                        
    void init(void);
    void init( int month, int day, int year);
    void print(void);
};

void Date::init(void){                        
   struct tm *ptr;     
   time_t sec;         
   time(&sec);         
   ptr = localtime(&sec); 
   
   month = (short) ptr->tm_mon + 1;
   day   = (short) ptr->tm_mday;
   year  = (short) ptr->tm_year + 1900;
} 

void Date::init( int m, int d, int y)
{
   month = (short) m;
   day   = (short) d;
   year  = (short) y;
}

void Date::print(void)
{
   cout << month << '-' << day << '-' << year
        << endl;
}
int main()
{
   Date today, birthday, aDate;
   today.init();
   birthday.init( 12, 11, 1997);
   cout << "Today's date: ";
   today.print();
   cout << "\n Felix' birthday: ";
   birthday.print();
   cout << "Some testing outputs:" << endl;
   aDate = today;                 
   aDate.print();
   Date *pDate = &birthday;       
   pDate->print();
   Date &holiday = aDate;         
   holiday.init( 1, 5, 2000);     
   aDate.print();                 
   return 0;
}

           
       








Related examples in the same category