人生的许多大困难,只要活着,没什么是解决不了的,时间和智慧而已
——三毛
ps:以后不能说自闭了,之前说了一次,然后一语成谶,以后要说rank1。
手慢无场
Problem A: Person类与Student类的关系
main函数:
int main() { string n; int a, g; cin>>n>>a>>g; Student student(n, a, g); student.Person::show(); student.show(); return 0; }
没什么考点吧应该,怎么说怎么写,本来以为是考虚函数的,结果发现想多了。。。。
AC代码:
#include <bits/stdc++.h> using namespace std; class Person { string name; int age; public: Person(string _name,int _age):name(_name),age(_age){cout << "A person " << name << " whose age is " << age << " is created." << endl;} ~Person() { cout << "A person " << name << " whose age is " << age << " is erased." << endl; } void show() { cout << "Name is " << name << " and age is " << age << "." << endl; } }; class Student :public Person { int grade; public: Student(string _name,int _age,int _grade):Person(_name,_age),grade(_grade){cout << "A student whose grade is " << grade << " is created." << endl;} ~Student() { cout << "A student whose grade is " << grade << " is erased." << endl; } void show() { cout << "Grade is " << grade << "." << endl; } };
Problem B: 家教课程
main函数:
int main() { string s1, s2, s3, s4; int i; cin>>s1>>s2>>s3>>s4>>i; Person person1(s1), person2(s2); Teacher teacher(s1, s3); Student student(s2, i); Course course(s1, s2, s3, s4, i); return 0; }
细节题,我看到了hint,但是我居然认为它在框我,想想我脑回路也是清奇。
类的继承加上类的组合吧,但不算难。
AC代码:
#include <bits/stdc++.h> using namespace std; class Person { friend class Student; friend class Teacher; string name; public: Person(string _name):name(_name){ cout << "Person " << name << " is created." << endl; } ~Person() { cout << "Person " << name << " is erased." << endl; } }; class Student :public Person { int id; public: Student(string _name,int _id):Person(_name),id(_id){ cout << "Student " << name << " with id " << id << " is created." << endl; } ~Student() { cout << "Student " << name << " with id " << id << " is erased." << endl; } }; class Teacher:public Person { string work; public: Teacher(string _name,string _work):Person(_name),work(_work){ cout << "Teacher " << name << " with position " << work << " is created." << endl; } ~Teacher() { cout << "Teacher " << name << " with position " << work << " is erased." << endl; } }; class Course { Teacher t; Student s; string OPP; public: Course(string _name1,string _name2,string _work,string _OPP,int _id):t(_name2,_work),s(_name2,_id),OPP(_OPP){ cout << "Course " << OPP << " is created." << endl; } ~Course() { cout << "Course " << OPP << " is erased." << endl; } };
Problem C: 一帮学生
main函数:
int main() { int year, month, day, id, i; string name; int num; cin>>num; Student **students = new Student*[num]; for (i = 0; i < num; i++) { cin>>year>>month>>day>>id>>name; students[i] = new Student(year, month, day, name, id); } for (i = 0;i <num; i++) delete students[i]; delete[] students; return 0; }
考点:静态数据成员+继承
AC代码:
#include <bits/stdc++.h> using namespace std; class Date { friend class Person; friend class Student; int yy,mm,dd; public: Date(int _y,int _m,int _d):yy(_y),mm(_m),dd(_d){ cout << "Date " << yy << "-" << mm << "-" << dd << " is created." << endl; } ~Date() { cout << "Date " << yy << "-" << mm << "-" << dd << " is erased." << endl; } }; class Person { friend class Student; Date birth; string name; static int numOfPersons; public: Person(int _y,int _m,int _d,string _name):birth(_y,_m,_d),name(_name){numOfPersons++; cout << "The " << numOfPersons << "th person " << name << " whose birthday is " << birth.yy << "-" << birth.mm << "-" << birth.dd << " is created." << endl; } ~Person() { numOfPersons--; cout << "Person " << name << " whose birthday is " << birth.yy << "-" << birth.mm << "-" << birth.dd <<" is erased." << endl; } }; class Student :public Person { int id; static int numOfStudents; public: Student(int _y,int _m,int _d,string _name,int _id):Person(_y,_m,_d,_name),id(_id){numOfStudents++; cout << "The " << numOfStudents << "th student " << name << " whose birthday is " << birth.yy << "-" << birth.mm << "-" << birth.dd << " and id is " << id << " is created." << endl; } ~Student() { cout << "Student " << name << " whose birthday is " << birth.yy << "-" << birth.mm <<"-"<< birth.dd <<" and id is " << id << " is erased." << endl; numOfStudents--; } }; int Person::numOfPersons = 0; int Student::numOfStudents = 0;