13
Date
- #pragma once
- #include <iostream>
- using namespace std;
- class Date {
- friend ostream &operator<<( ostream &, const Date & );
- friend istream &operator>>( istream &, Date & );
- public:
- Date( int d=0, int m=0, int y=0 ) : day( d ), month( m ), year( y ) { };
- void setDate( int d, int m, int y ) { day=d; month=m; year=y; };
- private:
- int day;
- int month;
- int year;
- };
- #include "Date.h"
- #include <iostream>
- using namespace std;
- ostream &operator<<( ostream &out, const Date &d )
- {
- static const char *monthnames[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- return out << monthnames[ d.month - 1 ] << " " << d.year << ", " << d.day;
- }
- istream &operator>>( istream &in, Date &d )
- {
- return in >> d.day >> d.month >> d.year;
- }
- #if defined( STANDALONE )
- int main() {
- Date peace( 11, 11, 1918 );
- cout << "World War I ended " << peace << ".\n";
- peace.setDate( 14, 8 , 1945);
- cout << "World War II ended " << peace << ".\n";
- Date date;
- cout << "Enter today's date (day month year): ";
- cin >> date;
- cout << "Today is " << date << ".\n";
- }
- #endif
$ g++ -g -DSTANDALONE Date.cpp -o tdate
$ ./tdate
World War I ended Nov 1918, 11.
World War II ended Aug 1945, 14.
Enter today's date (day month year): 31 12 1999
Today is Dec 1999, 31.
Comments