拷贝构造函数输出问题
定义好了拷贝构造函数,也“刷出了存在感”但是就是按理想的结果输出。比如t1=t2,t2可以输出jim 5 150,但是t1输出一堆看不懂的码是什么情况?求大神给解答一下
定义好了拷贝构造函数,也“刷出了存在感”但是就是按理想的结果输出。比如t1=t2,t2可以输出jim 5 150,但是t1输出一堆看不懂的码是什么情况?求大神给解答一下
2016-01-02
teacher.h头文件如下:
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class Teacher
{
public:
void setName(string name);
string getName();
void setAge(int age);
int getAge();
int getMax();
Teacher(string name, int age=20);
Teacher();
Teacher(const Teacher&);
private:
string m_strName;
int m_iAge;
const int m_iMax;
};teacher.cpp 文件如下:
#include "teacher.h"
string Teacher::getName()
{
return m_strName;
}
int Teacher::getAge()
{
return m_iAge;
}
int Teacher::getMax()
{
return m_iMax;
}
Teacher::Teacher():m_iMax(100)
{
m_strName="jack";
m_iAge=23;
cout<<"Teacher()"<<endl;
}
Teacher::Teacher(string name, int age):m_iMax(100)
{
m_strName=name;
m_iAge=age;
cout<<"Teacher(string name='jack', int age=23)"<<endl;
}
Teacher::Teacher(const Teacher&):m_iMax(100)
{
cout<<"拷贝"<<endl;
}demo文件如下:
#include <stdlib.h>
#include <iostream>
#include "teacher.h"
using namespace std;
int main(void)
{
Teacher t1;
Teacher t2("jack");
Teacher t3(t1);
Teacher t4=t2;
cout<<t1.getName()<<" "<<t1.getAge()<<" "<<t1.getMax()<<endl;
cout<<t2.getName()<<" "<<t2.getAge()<<" "<<t2.getMax()<<endl;
cout<<t3.getName()<<" "<<t3.getAge()<<" "<<t3.getMax()<<endl;
cout<<t4.getName()<<" "<<t4.getAge()<<" "<<t4.getMax()<<endl;
system("pause");
return 0;
}结果如下:

举报