程序代码:
#include<iostream> #include<string> using namespace std; class Student{ public: Student(); Student(string m_name, int m_age, int m_score_C, int m_score_Cpp, int m_score_math); Student(const Student& st); Student& operator=(const Student& st); ~Student(); void getInformation(); private: string name; int age; int score_C; int score_Cpp; int score_math; }; inline Student::Student() {} inline Student::Student(string m_name, int m_age, int m_score_C, int m_score_Cpp, int m_score_math) : name(m_name),age(m_age),score_C(m_score_C), score_Cpp(m_score_Cpp), score_math(m_score_math) {} inline Student::Student(const Student& st) { name = st.name; age = st.age; score_C = st.score_C; score_Cpp = st.score_Cpp; score_math = st.score_math; } inline Student& Student::operator=(const Student& st) { this->name = st.name; this->age = st.age; this->score_C = st.score_C; this->score_Cpp = st.score_Cpp; this->score_math = st.score_math; return *this; } inline Student::~Student() {} inline void Student:: getInformation() { cout << "Name:" << this->name << endl; cout << "Age:" << this->age << endl; cout << "Score of C:" << this->score_C << endl; cout << "Score of Cpp:" << this->score_Cpp << endl; cout << "Score of math:" << this->score_math << endl; } int main(void) { Student s1("Lmw", 25, 90, 90, 90); s1.getInformation(); Student s2; s2 = s1; s2.getInformation(); return 0; }