You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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} {}
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';
// 本程序失败了输出为0为什么
}