#include
using namespace std;
#include
class Person{
public:
string name,sex;
int age;
Person(string name,string sex,int age){
cout<<"Person Constructed!"<<endl;
}
Person(const Person & p){
cout<<"Person Copied!"<<endl;
}
virtual void PrintInfo(){
cout<<"class"<<"name"<<name<<"sex"<<sex<<"age"<<age
}
~Person(){cout<<"Person Destructed!"<<endl;}
};
class Student:public Person{
public:
Student(string name, string sex, int age, string major, int grade, int gpa) {
cout<<"Student Constructed!"<<endl;
}
Student(const Student &p): Person(p) {
cout<<"Student Copied!"<<endl;
}
~Student(){cout<<"Student Destructed!"<<endl;}
string major;
int grade,gpa;
};
class Worker:public Person{
Worker(string name,string sex,int age,string nationality,float salary){
cout<<"Worker Constructed!"<<endl;
}
Worker(const Worker &p): Person(p) {
cout<<"Worker Copied!"<<endl;
}
~Worker(){cout<<"Worker Destructed!"<<endl;}
string nationality,salary;
};
int main() {
Person person("Allen", "male", 29);
Student student("Frank", "male", 15, "Robotics", 17, 3.5);
Worker worker("Ben", "male", 45, "China", 3500.0f);
// 调用多态函数,按要求输出信息
person.PrintInfo();
student.PrintInfo();
worker.PrintInfo();
// 验证运算符重载的正确性
worker("Ben", "male", 45, "China", 3500.0f);
std::cout << student << std::endl;
// 对三个实例进行拷贝
Person person2 = person;
Student student2 = student;
Worker worker2 = worker;
// 对其中一个拷贝得到的实例调用多态函数,输出信息
worker2.PrintInfo();
return 0;
}
ostream &append(ostream & append,Student & stu){
cout<<"I'm a student named "<<stu.name<<endl;
}