一定要注意格式!
// 类模板的继承.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
using namespace std;
template <typename Type1, typename Type2>
class Cperson
{
private:
Type1 name;
Type2 sex;
public:
Cperson(Type1 name, Type2 sex)
{
this->name = name;
this->sex = sex;
}
~Cperson()
{
cout << "调用Cperson的析构函数" << endl;
}
void ShowInf()
{
cout << "学生" << this->name << "的性别为" << this->sex << endl;
}
};
template <typename Type1, typename Type2, typename Type3, typename Type4>
class Cstudent :public Cperson<Type1, Type2>
{
private:
Type3 Mark;
Type4 Weight;
public:
Cstudent(Type1 name, Type2 sex, Type3 Mark, Type4 Weight) :Cperson<Type1, Type2>(name, sex)
{
this->Mark = Mark;
this->Weight = Weight;
}
~Cstudent()
{
cout << "调用Cstudent的析构函数" << endl;
}
void ShowInf()
{
Cperson<Type1, Type2>::ShowInf(); // 调用作用域为Cperson的ShowInf函数
cout << "学生的成绩为" << this->Mark << ";体重为" << this->Weight << endl;
}
};
int main()
{
Cstudent<string, char, float, float> stud("大明", 'm', 99.99, 65.3);
stud.ShowInf();
}