17

Person

  1. #pragma once
  2.  
  3. #include "../String/String.h"
  4. #include "../Date/Date.h"
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class Person
  11. {
  12. public:
  13.     Person( char *n="", int s=0, char *nat="U.S.A." ) : name( n ), sex( s ), nationality( nat ) {}
  14.     void setDOB( int d, int m, int y ) { dob.setDate( d, m, y ); }
  15.     void setDOD( int d, int m, int y ) { dod.setDate( d, m, y ); }
  16.     void printName( ) { std::cout << name; }
  17.     void printNationality( ) { std::cout << nationality; }
  18.     void printDOB( ) { std::cout << dob; }
  19.     void printDOD( ) { std::cout << dod; }
  20. #if 0
  21.     void print( ) { cout << "My name is " << name << ".\n"; }
  22. #else
  23.     virtual void print( ) { cout << "My name is " << name << ".\n"; }
  24. #endif
  25. protected:
  26.     String name, nationality;
  27.     Date dob, dod;              // date of birth, date of death
  28.     int sex;                    // 0=female, 1=male
  29. };
  1. #include "Person.h"
  2.  
  3. #if defined( STANDALONE )
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     Person author("Thomas Jefferson", 1 );
  11.  
  12.     author.setDOB( 13, 4, 1743 );
  13.     author.setDOD( 4, 7, 1826 );
  14.  
  15.     cout << "The author of the Declaration of Independence was ";
  16.     author.printName();
  17.     cout << ".\nHe was born ";
  18.     author.printDOB();
  19.     cout << " and died ";
  20.     author.printDOD();
  21.     cout << ".\n";
  22. }
  23.  
  24. #endif
  1. #pragma once
  2.  
  3. #include "Person.h"
  4.  
  5. #include <iostream>
  6.  
  7. class Student : public Person
  8. {
  9. public:
  10.     Student( char *n, int s=1, char *nat="U.S.A.", char *i="" ) : Person( n, s, nat ), id( i ), credits( 0 ) {}
  11.     void setDOM( int d, int m, int y ) { dom.setDate( d, m, y ); }
  12.     void printDOM( ) { std::cout << dom; }
  13.     void print( ) { Person::print(); cout << "I entered the school " << dom << ".\n"; }
  14. protected:
  15.     String id;
  16.     Date dom;       // date of membership
  17.     int credits;
  18.     float gpa;      // grade point average
  19. };
  1. #include "Student.h"
  2.  
  3. #if defined( STANDALONE )
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     Student x( "John Smith", 1, "UK", "A450034" );
  11.     x.setDOB( 13, 5, 1977 );
  12.     x.setDOM( 29, 8, 1995 );
  13.     x.printName();
  14.     cout << "\n\t       born: ", x.printDOB();
  15.     cout << "\n\t registered: ", x.printDOM();
  16.     cout << "\n\tnationality: ", x.printNationality();
  17.     cout << endl;
  18.  
  19.     cout << endl;
  20.     Person *p1 = &x;
  21.     p1->print();
  22.     cout << endl;
  23.     Person p2 = x;
  24.     p2.print();
  25.     cout << endl;
  26.     Student *p3 = &x;
  27.     p3->print();
  28.     cout << endl;
  29.     Person y( "Ann Jones", 0 );
  30.     Student *p4 = (Student *)&y;    // cast needed
  31.     p4->print();
  32. }
  33.  
  34. #endif

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].