ITPub博客

首页 > 应用开发 > > 复制构造函数无法正确输出

复制构造函数无法正确输出

作者:kalt 时间:2019-10-29 11:08:36 0 删除 编辑
//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不同呢?

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1805/viewspace-3126791/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    121
  • 访问量
    893