类继承练习题四

类继承练习题四

4、定义一个Person类,它包含数据成员age、name和gender。从Person中派生一个类Employee,在新类中添加一个数据成员,存储个人的number。再从Employee中派生一个类Executive,每个派生类都应定义一个函数,来显示相关的信息(名称和类型,如"Fred Smith is an Employee")。编写一个main()函数,生成两个数组,一个数组包含5个Executive对象,另一个数组包含5个一般的Employee对象,然后显示它们的信息。另外,调用从Employee类继承的成员函数,显示Executive的信息。

Person.h
// Person.h
// Person class and class derived from Person
#ifndef PERSON_H
#define PERSON_H

#include 
<string>
using std::string;

class Person {
public:
    Person(): age(
0), name(""), gender('f') {};            // Default constructor
    Person(int theAge, string theName, char theGender);

    
void who() const;        // Display details

protected:
    
int age;        // Age in years
    string name;
    
char gender;    // 'm' or 'f'
};

class Employee: public Person {
public:
    Employee() {}; 
// Default constructor - necessary to declare arrays

    Employee(
int theAge, string theName, char theGender, long persNum):
    Person(theAge, theName, theGender), personnelNumber(persNum) {}

    
void who() const;    // Display details
protected:
    
long personnelNumber;
};

class Executive: public Employee {
public:
    Executive() {};    
// Default constructor - necessary to declare arrays

    Executive(
int theAge, string theName, char theGender, long persNum): 
    Employee(theAge, theName, theGender, persNum) {}

    
void who() const;    // Display details
};

#endif


Person.cpp
// Person.cpp
// Person class implementation
#include "Person.h"
#include 
<iostream>
#include 
<string>
using std::string;
using std::cout;
using std::endl;

Person::Person(
int theAge, string theName, char theGender):
                age(theAge), name(theName), gender(theGender) {
}

void Person::who() const {
    cout 
<< "\nThis is " << name << " who is " << age << " years old.";
}

void Employee::who() const {
    cout 
<< endl
        
<< name << " is a " << (gender == 'f' ? "female" : "male")
        
<< " employee aged " << age << ".";
}

void Executive::who() const {
    cout 
<< endl
        
<< name << " is a " << (gender == 'f' ? "female" : "male"<< " executive.";
}


main.cpp
// main.cpp 
// Working with Employee and Executive objects

#include 
<iostream>
#include 
"Person.h"
using std::cout;
using std::endl;

void main() {
    Employee employees[
5];
    employees[
0= Employee(21"Randy Marathon"'m'34567);
    employees[
1= Employee(32"Anna Pothecary"'f'34578);
    employees[
2= Employee(46"Peter Out"'m'34589);
    employees[
3= Employee(37"Sheila Rangeit"'f'34598);
    employees[
4= Employee(65"Jack Ittin"'m'34667);

    
int i = 0;

    
for (i=0; i<sizeof employees/sizeof(Employee); i++)
        employees[i].who();

    Executive executives[
5];
    executives[
0= Executive(44"Irwin Pootlemeyer"'m'35567);
    executives[
1= Executive(32"Alexa Workwell"'f'35578);
    executives[
2= Executive(42"Steve Stove"'m'35589);
    executives[
3= Executive(33"Sue Neenuf"'f'35598);
    executives[
4= Executive(29"Melanie Clair"'f'35667);

    
for(i = 0 ; i<sizeof executives/sizeof(Executive) ; i++) {
        executives[i].who();
        executives[i].Employee::who();
    }
    cout 
<< endl;
}



你可能感兴趣的:(类继承练习题四)