13

Date

  1. #pragma once
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Date {
  8.     friend ostream &operator<<( ostream &, const Date & );
  9.     friend istream &operator>>( istream &, Date & );
  10.  
  11. public:
  12.     Date( int d=0, int m=0, int y=0 ) : day( d ), month( m ), year( y ) { };
  13.     void setDate( int d, int m, int y ) { day=d; month=m; year=y; };
  14.  
  15. private:
  16.     int day;
  17.     int month;
  18.     int year;
  19. };
  1.  
  2. #include "Date.h"
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. ostream &operator<<( ostream &out, const Date &d )
  9. {
  10.     static const char *monthnames[] = {
  11.         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  12.     };
  13.     return out << monthnames[ d.month - 1 ] << " " << d.year << ", " << d.day;
  14. }
  15.  
  16. istream &operator>>( istream &in, Date &d )
  17. {
  18.     return in >> d.day >> d.month >> d.year;
  19. }
  20.  
  21. #if defined( STANDALONE )
  22.  
  23. int main() {
  24.     Date peace( 11, 11, 1918 );
  25.     cout << "World War I ended " << peace << ".\n";
  26.     peace.setDate( 14, 8 , 1945);
  27.     cout << "World War II ended " << peace << ".\n";
  28.  
  29.     Date date;
  30.     cout << "Enter today's date (day month year): ";
  31.     cin >> date;
  32.     cout << "Today is " << date << ".\n";
  33. }
  34.  
  35. #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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].