//main.c #include <iostream> #include "Teacher.h" #include "stdlib.h" using namespace std; int main(void) { Teacher t1; cout<< t1.getName()<<" "<<t1.getAge()<< endl; Teacher t2("Marry",12); cout << t2.getName() << " " << t2.getAge() << endl; Teacher t3("King", 35, 120 ); cout << t3.getName() << " " << t3.getAge() <<" "<<t3.getMax()<< endl; Teacher t4=t3; cout << t4.getName() << " " << t4.getAge() << " " << t4.getMax() << endl; system("pause"); return 0; } //Teacher.h #include "string" #include <iostream> using namespace std; class Teacher { public: //Teacher(string name = "Jim", int age = 30); //Teacher(); Teacher(string name="Jim", int age=30, int m=120); Teacher(const Teacher &); //Teacher(Teacher &t); void setName(string name); string getName(); void setAge(int age); int getAge(); int getMax(); private: string m_strName; int m_iAge; int m_iMax; }; //Teacher.c #include "Teacher.h" //using namespace std; //使用初始化列表 Teacher::Teacher(string name, int age, int m) : m_strName(name), m_iAge(age), m_iMax(m) { //m_iMax = m; cout << "Teacher(string name, int age, int m)"<< endl; // m_strName = name; // m_iAge = age; } // 构造函数的一般初始化 // Teacher::Teacher(string name, int age, int m) // { // cout << "Teacher(string name, int age)" << endl; // m_iMax = m; // m_strName = name; // m_iAge = age; // } Teacher::Teacher(const Teacher &) { cout <<"Teacher(const Teacher &)" << endl; } int Teacher::getMax() { return m_iMax; } void Teacher::setName(string name) { m_strName = name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int age) { m_iAge = age; } int Teacher::getAge() { return m_iAge; }
为什么对象t4的输出与对象t3不同呢?
Xyino_Snake
慕的地6079101
Xyino_Snake
相关分类