认识C++继承派生时基类构造函数的是怎样调用的,以及模板的相关知识?

本文通过一个详细的C++代码示例,展示了Person、Student、Teacher和Graduate类之间的继承关系,以及如何使用模板简化代码。每个类的构造函数都包含提示语句,清晰地显示了对象创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面以和之前那篇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


如果觉得从博主这学到了些东西,记得关注下博主哦!博主将不定期推送新知识的呢!谢谢大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值