编写程序,定义一个基类Person,包含name和age两个数据成员
定义一个基类Person,包含name和age两个数据成员,再由它派生出学生类Student和教师类Teacher,其中学生类添加学号no,考试平均分数mean;教师类添...
定义一个基类Person,包含name和age两个数据成员,再由它派生出学生类Student和教师类Teacher,其中学生类添加学号no,考试平均分数mean;教师类添加部门department,职称title,和工资salary。由Student类派生出研究生类Graduate,添加数据项:专业subject,导师adviser。
要求每个类都有构造函数和数据输出功能,创建派生类对象,进行数据测试。 展开
要求每个类都有构造函数和数据输出功能,创建派生类对象,进行数据测试。 展开
1个回答
2019-01-02
展开全部
#include <iostream>
using namespace std;
class Person
{
public:
Person(char* name, int age);
virtual ~Person();
virtual void show();
protected:
char* name; // 姓名
int age; // 年龄
};
Person::Person(char* name, int age)
{
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
Person::~Person()
{
delete this->name;
}
void Person::show()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
}
class Student : public Person
{
public:
Student(char* name, int age, int no, float mean);
~Student();
virtual void show();
protected:
int no; // 学号
float mean; // 平均分数
};
Student::Student(char* name, int age, int no, float mean) : Person(name, age)
{
this->no = no;
this->mean = mean;
}
Student::~Student()
{
}
void Student::show()
{
Person::show();
cout << "学号:" << no << endl;
cout << "平均分数:"<< mean << endl;
}
class Teacher : public Person
{
public:
Teacher(char* name, int age, char* department, char* title, int salary);
~Teacher();
virtual void show();
protected:
char *department; // 部门
char *title; // 职称
int salary; // 工资
};
Teacher::Teacher(char* name, int age, char* department, char* title, int salary) : Person(name, age)
{
this->department = new char[strlen(department) + 1];
strcpy(this->department, department);
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
this->salary = salary;
}
Teacher::~Teacher()
{
delete department;
delete title;
}
void Teacher::show()
{
Person::show();
cout << "部门:" << department << endl;
cout << "职称:" << title << endl;
cout << "工资:" << salary << endl;
}
class Graduate : public Student
{
public:
Graduate(char* name, int age, int no, float mean, char* subject, char* adviser);
~Graduate();
virtual void show();
protected:
char* subject; // 专业
char* adviser; // 导师
};
Graduate::Graduate(char* name, int age, int no, float mean, char* subject, char* adviser) : Student(name, age, no, mean)
{
this->subject = new char[strlen(subject) + 1];
strcpy(this->subject, subject);
this->adviser = new char[strlen(adviser) + 1];
strcpy(this->adviser, adviser);
}
Graduate::~Graduate()
{
delete subject;
delete adviser;
}
void Graduate::show()
{
Student::show();
cout << "专业:" << subject << endl;
cout << "导师:" << adviser << endl;
}
int main()
{
Person* a = new Person("AA", 18);
Student* b = new Student("B", 18, 1, 80);
Teacher* c = new Teacher("C", 35, "XX", "XXX", 3500);
Graduate* d = new Graduate("D", 28, 2, 85, "YY", "YYY");
a->show();
cout << "---------------------------------------" << endl;
b->show();
cout << "---------------------------------------" << endl;
c->show();
cout << "---------------------------------------" << endl;
d->show();
cout << "---------------------------------------" << endl;
delete a;
delete b;
delete c;
delete d;
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询