|
|
|
@ -0,0 +1,74 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using std::string, std::cout, std::cin, std::endl;
|
|
|
|
|
|
|
|
|
|
class Student
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
string name;
|
|
|
|
|
int age;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Student(string _name, int _age) : name{_name}, age{_age} {}
|
|
|
|
|
virtual int schooling()
|
|
|
|
|
{
|
|
|
|
|
return 0; // 空泛的学生类,缺乏收费依据,直接返回0
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MiddleStudent : public Student
|
|
|
|
|
{
|
|
|
|
|
int books;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
MiddleStudent(string _name, int _age, int _books)
|
|
|
|
|
: Student(_name, _age), books{_books} {}
|
|
|
|
|
int schooling()
|
|
|
|
|
{
|
|
|
|
|
return books * 20; // 中学生按20元单价收书本费
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CollegeStudent : public Student
|
|
|
|
|
{
|
|
|
|
|
int credit;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
CollegeStudent(string _name, int _age, int _credit)
|
|
|
|
|
: Student(_name, _age), credit{_credit} {}
|
|
|
|
|
int schooling()
|
|
|
|
|
{
|
|
|
|
|
return credit * 120; // 大学生按120单价收学分费
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class GraduateStudent : public Student
|
|
|
|
|
{
|
|
|
|
|
int credit;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
GraduateStudent(string _name, int _age, int _credit)
|
|
|
|
|
: Student(_name, _age), credit{_credit} {}
|
|
|
|
|
int schooling()
|
|
|
|
|
{
|
|
|
|
|
return credit * 200; // 研究生按200单价收学分费
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int get_total(Student *s[], int len)
|
|
|
|
|
{
|
|
|
|
|
int total{};
|
|
|
|
|
for (int i{0}; i < len; ++i)
|
|
|
|
|
total += s[i]->schooling();
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
MiddleStudent ms{"张三", 15, 20};
|
|
|
|
|
CollegeStudent cs{"张二", 18, 150};
|
|
|
|
|
GraduateStudent gs{"张大", 22, 40};
|
|
|
|
|
Student *s[]{&ms, &cs, &gs}; // 不存在引用的数组,只能定义指针数组
|
|
|
|
|
std::cout << get_total(s, 3) << '\n';
|
|
|
|
|
}
|