下面以和之前那篇people类 类似的一个题为大家讲解!
如果看了本博客觉得有收获,别忘了点关注哦!
题目如下:
Person类包含私有成员数据姓名name(string),编号code(int)和出生年月日。Student类包含私有成员数据姓名name(string),编号code(int),出生年月日和分数score(int)。Teacher类包含私有成员数据姓名name(string),编号code(int),出生年月日和所在系department(string)。Graduate类包含私有成员数据姓名name(string),编号code(int),出生年月日,分数score(int)和所在系department(string)。提供main函数如下:
int main() {
string name, department;
int Cas = 0, code, year, month, day, score;
while(cin >> name >> code >> year >> month >> day >> score >> department) {
Cas++;
cout << "CASE #" << Cas << ":" << endl;
Person person(name, code, year, month, day);
Student student(name, code, year, month, day, score);
Teacher teacher(name, code, year, month, day, department);
Graduate graduate(name, code, year, month, day, score, department);
Show(&person);
Show(&student);
Show(&teacher);
Show(&graduate);
}
return 0;
}
程序代码如下,清楚的说明了4个类之间的继承与派生的关系!并且在每一个构造函数中加入提示语句,让运行过程更清晰!还简单的用到了模板的知识使代码不用那么累赘,就不用再写出非成员函数Show函数的重载!
#include<iostream>
#include<string>
using namespace std;
class Person {
protected:
string name;
int code;
int y;
int m;
int d;
public:
Person(string n, int c, int yy, int mm, int dd): name(n), code(c), y(yy), m(mm), d(dd) {
cout << "Person::Constructor Function is called." << endl;
}
Person(const Person &P): name(P.name), code(P.code), y(P.y), m(P.m), d(P.d) {}
~Person() {}
void Show()const {
cout << "Person::Show Function is called." << endl;
cout << "NAME:" << name << " Code:" << code << " BIRTHDAY:" << y << "-" << m << "-" << d << endl;
}
};
class Student: virtual public Person {
protected:
int score;
public:
Student(string n, int c, int yy, int mm, int dd, int s): Person(n, c, yy, mm, dd), score(s) {
cout << "Student::Constructor Function is called." << endl;
}
Student(const Student &S): Person(S), score(S.score) {}
~Student() {}
void Show()const {
cout << "Student::Show Function is called." << endl;
cout << "NAME:" << name << " Code:" << code << " SCORE:" << score << " BIRTHDAY:" << y << "-" << m << "-" << d << endl;
}
};
class Teacher: virtual public Person {
protected:
string department;
public:
Teacher(string n, int c, int yy, int mm, int dd, string d): Person(n, c, yy, mm, dd), department(d) {
cout << "Teacher::Constructor Function is called." << endl;
}
Teacher(const Teacher &T): Person(T), department(T.department) {}
~Teacher() {}
void Show()const {
cout << "Teacher::Show Function is called." << endl;
cout << "NAME:" << name << " Code:" << code << " DEPARTMENT:" << department << " BIRTHDAY:" << y << "-" << m << "-" << d << endl;
}
};
class Graduate: public Student, public Teacher {
public:
Graduate(string n, int c, int yy, int mm, int dd, int s, string d): Person(n, c, yy, mm, dd), Student(n, c, yy, mm, dd, s), Teacher(n, c, yy, mm, dd, d) {
cout << "Graduate::Constructor Function is called." << endl;
}
Graduate(const Graduate& G): Person(G), Student(G), Teacher(G) {}
~Graduate() {}
void Show()const {
cout << "Graduate::Show Function is called." << endl;
cout << "NAME:" << name << " Code:" << code << " SCORE:" << score << " DEPARTMENT:" << department << " BIRTHDAY:" << y << "-" << m << "-" << d << endl;
}
};
template <class T>
void Show(T *t) {
cout << "Show Function is called." << endl;
t->Show();
}
下面给个实例输入及输出结果!
张三 111111 2222 3 44 55 数学
李四 222222 5555 6 8 77 管理
王五 555555 2000 3 4 55 软件
输出结果如下:从这个结果可以清楚的看出构造的过程!以及模板的用法!
张三 111111 2222 3 44 55 数学
CASE #1:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:张三 Code:111111 BIRTHDAY:2222-3-44
Show Function is called.
Student::Show Function is called.
NAME:张三 Code:111111 SCORE:55 BIRTHDAY:2222-3-44
Show Function is called.
Teacher::Show Function is called.
NAME:张三 Code:111111 DEPARTMENT:数学 BIRTHDAY:2222-3-44
Show Function is called.
Graduate::Show Function is called.
NAME:张三 Code:111111 SCORE:55 DEPARTMENT:数学 BIRTHDAY:2222-3-44
李四 222222 5555 6 8 77 管理
CASE #2:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:李四 Code:222222 BIRTHDAY:5555-6-8
Show Function is called.
Student::Show Function is called.
NAME:李四 Code:222222 SCORE:77 BIRTHDAY:5555-6-8
Show Function is called.
Teacher::Show Function is called.
NAME:李四 Code:222222 DEPARTMENT:管理 BIRTHDAY:5555-6-8
Show Function is called.
Graduate::Show Function is called.
NAME:李四 Code:222222 SCORE:77 DEPARTMENT:管理 BIRTHDAY:5555-6-8
王五 555555 2000 3 4 55 软件
CASE #3:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:王五 Code:555555 BIRTHDAY:2000-3-4
Show Function is called.
Student::Show Function is called.
NAME:王五 Code:555555 SCORE:55 BIRTHDAY:2000-3-4
Show Function is called.
Teacher::Show Function is called.
NAME:王五 Code:555555 DEPARTMENT:软件 BIRTHDAY:2000-3-4
Show Function is called.
Graduate::Show Function is called.
NAME:王五 Code:555555 SCORE:55 DEPARTMENT:软件 BIRTHDAY:2000-3-4
如果觉得从博主这学到了些东西,记得关注下博主哦!博主将不定期推送新知识的呢!谢谢大家!