8

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.

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[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]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].