编译器:vs2022
main.cpp
#include <iostream> #include <iomanip> #include "person.h" const int Sum = 2; class Group { protected: PostGra st[Sum]; int sum; public: Group(); void Input(); void SortByID(); void Output(); }; Group::Group() { sum = Sum; } void Group::Input() { int i; for (i = 0; i < sum; i++) st[i].Input(); } void Group::SortByID() { int index, i, k; PostGra temp; for (k = 0; k < sum - 1; k++) { index = k; for (i = k + 1; i < sum; i++) if (st[i].GetID() < st[index].GetID()) index = i; if (index != k) { temp = st[index]; st[index] = st[k]; st[k] = temp; } } } void Group::Output() { int i; std::cout << std::endl << "The group information:" << std::endl; std::cout << "ID" << std::setw(10) << "Name" << std::setw(10) << "Age" << std::setw(10) << "Gender" << std::setw(10) << "Spec" << std::setw(10) << "research"<< std::endl; for (i = 0; i < sum; i++) { std::cout << st[i].GetID() << std::setw(10) << st[i].GetName() << std::setw(10) << st[i].GetGender() << std::setw(10) << st[i].GetSpeciality() << std::setw(10) << st[i].GetResearchTopic() << std::setw(10) << st[i].GetAcademicTitle() << std::endl; } } int main() { Group group; group.Input(); group.SortByID(); group.Output(); return 0; }
person.h
#pragma once #ifndef _PERSON #define _PERSON #include <string> class Person { protected: std::string ID; std::string name; std::string gender; public: Person(std::string = "000", std::string = "unknown", std::string = "unknown"); void Input(); std::string GetID(); std::string GetName(); std::string GetGender(); }; class Student : virtual public Person { protected: std::string speciality; public: Student(std::string, std::string, std::string = "000", std::string = "unknown"); void Input(); std::string GetSpeciality(); }; class Graduate :virtual public Student { protected: std::string researchTopic; public: Graduate(std::string=" ", std::string=" ", std::string=" ", std::string = "000", std::string = "unknown"); void Input(); std::string GetResearchTopic(); }; class Teacher :virtual public Person { protected: std::string academicTitle; public: Teacher(std::string, std::string, std::string, std::string = "unknown"); void Input(); std::string GetAcademicTitle(); }; class PostGra :public Graduate, public Teacher { public: PostGra(std::string=" ", std::string=" ", std::string=" ", std::string=" ", std::string = "000", std::string = "unknown"); void Input(); }; #endif
person.cpp
#include "person.h" #include <iostream> Person::Person(std::string id, std::string name, std::string gender) { ID = id; name = name; gender = gender; } void Person::Input() { std::cout << "Enter ID: "; std::cin >> ID; std::cout << "Enter name: "; std::cin >> name; std::cout << "Enter gender: "; std::cin >> gender; } std::string Person::GetID() { return ID; } std::string Person::GetName() { return name; } std::string Person::GetGender() { return gender; } Student::Student(std::string id, std::string name, std::string gender, std::string speciality) : Person(id, name, gender) { speciality = speciality; } void Student::Input() { Person::Input(); std::cout << "Enter speciality: "; std::cin >> speciality; } std::string Student::GetSpeciality() { return speciality; } Graduate::Graduate(std::string id, std::string name, std::string gender, std::string speciality, std::string rese) :Person(id, name, gender), Student(id, name, gender, speciality) { researchTopic = rese; } void Graduate::Input() { Student::Input(); std::cout << "Enter research topic: "; std::cin >> researchTopic; } std::string Graduate::GetResearchTopic() { return researchTopic; } Teacher::Teacher(std::string id, std::string name, std::string gender, std::string title) : Person(id, name, gender) { academicTitle = title; } void Teacher::Input() { Person::Input(); std::cout << "Enter academic title: "; std::cin >> academicTitle; } std::string Teacher::GetAcademicTitle() { return academicTitle; } PostGra::PostGra(std::string id, std::string name, std::string gender, std::string speciality, std::string rese, std::string title) : Person(id, name, gender), Student(id, name, gender, speciality), Teacher(id, name, gender, title) { ; } void PostGra::Input() { Graduate::Input(); std::cout << "Enter academic title: "; std::cin >> academicTitle; }