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