设计一个名为Person的类,并设计两个Person的派生的类Student和Employee。Employee的Faculty和Staff的派生类。

题:
设计一个名为Person的类,并设计两个Person的派生的类Student和Employee。Employee的Faculty和Staff的派生类。Person类具有name,address,phone number和e-mail。Student具有freshmen, sophonore, junior, or senior四个状态。Employee有office,salary和datehired。定义一个名为MyDate的类,其中包含year,month和day字段。Faculty有officehour和rank。Staff有title。在Person类中定义一个常量虚拟toString函数,并在每个类中覆盖它以显示类名和人名。
实现类。编写一个测试程序,创建一个Person,Student,Employee,Faculty和Staff,并调用它们的toString()函数。

题解:通过继承从基类中派生获取基类的函数等,在派生类中可以设计属于自己的函数可来区别于父类,达到自己的特色,用虚函数和动态绑定来实现tostring()函数的功能。

#include<iostream>
using namespace std;

class Person
{
public:
	Person()
	{
		name = "0";
		address = "0";
		number = 0;
		mail = "0";
	}//无参构造函数
	/*Person(string name, string address, int number, string mail)
	{
		this->name = name;
		this->address = address;
		this->number = number;
		this->mail = mail;
	}*/

	Person(string name)
	{
		this->name = name;
	}

	void setAddress(string address)
	{
		this->address = address;
	}//设置地址

	void setNumber(int number)
	{
		this->number = number;
	}//设置电话号码

	void setMail(string mail)
	{
		this->mail = mail;
	}//设置邮箱地址

	string name;
	string address;
	int number;
	string mail;
	virtual string toString() const
	{
		return "Person Object";
	}//返回类名
};

class Student :public Person
{
public:
	string status;
	Student()
	{
		status = "freshman";
	}

	Student(string name)
	{
		this->name = name;
	}

	void setStatus(string status)
	{
		this->status = status;
	}//设置学生的年级

	string toString() const
	{
		return "Student Object";
	}
};

class Employee:public Person
{
public:
	Employee()
	{
		name = "0";
	}
	Employee(string name)
	{
		this->name = name;
	}
	string office;
	int salary;
	string datehired;

	void setOffice(string office)
	{
		this->office = office;
	}//设置办公室

	void setSalary(int salary)
	{
		this->salary = salary;
	}//设置薪水

	void setDatehired(string datehired)
	{
		this->datehired = datehired;
	}//设置工作时间

	string toString() const
	{
		return "Employee Object";
	}
};

class Faculty :public Employee
{
public:
	int officehour;
	int rank;
	Faculty(string name)
	{
		this->name = name;
	}

	void setOfficehour(int officehour)
	{
		this->officehour = officehour;
	}//设置工作时长

	void setRank(int rank)
	{
		this->rank = rank;
	}//设置排名

	string toString() const
	{
		return "Faculty Object";
	}
};

class Staff :public Employee
{
public:
	string title;
	Staff()
	{
		title = "0";
		name = "0";
	}
	Staff(string name)
	{
		this->name = name;
	}
	void setTitle(string title)
	{
		this->title = title;
	}//设置头衔

	string getTitle()
	{
		return title;
	}//获取头衔

	string toString() const
	{
		return "Staff Object";
	}
};

class MyDate//MyDate类
{
public:
	int year;
	int month;
	int day;
	MyDate()
	{
		year = 0;
		month = 0;
		day = 0;
	}
	MyDate(int year, int month, int day)
	{
		this->year = year;
		this->month = month;
		this->day = day;
	}
};

void display(const Person* g)
{
	cout << (*g).toString() << "  " << (*g).name << endl;
}

int main()
{
	display(&Person("Mike"));
	display(&Student("Dan"));
	display(&Employee("Joe"));
	display(&Faculty("John"));
	display(&Staff("Joney"));

	return 0;
}