从这个程序的题目和注释来看,这个type就是标识person到底是干部(首字符为g)还是教师(首字符为t)
整个程序是输入所有的N个职工的数据(person结构体数组元素的所有内部成员),并输出所有的数据
#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
}
classperson{
private
string
name
private
char
sex
private
int
age
public
string
getname()
{
return
name
}public
void
setname(string
name){
this.name=name}
public
char
getsex(){return
sex}public
void
setsex(char
sex){this.sex=sex}public
int
getage(){return
age}public
void
setname(int
age){this.age=age}
public
person(string
s)
{
this.setname(s)
}
public
person(string
s,char
c)
{
person(s)
this.
setsex(c)}
public
person(string
s,char
c,int
i){
person(string
s,char
c)this.setage(i)}
public
string
tostring(){return
this.getname()+"
"+this.getsex()+"
"+this.getage()}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)