diff --git a/schooling_v1.cpp b/schooling_v1.cpp new file mode 100644 index 0000000..4412b73 --- /dev/null +++ b/schooling_v1.cpp @@ -0,0 +1,75 @@ +#include +#include + +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,为什么? +} \ No newline at end of file