//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;
}
//?構(gòu)造函數(shù)的一般初始化
//?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;
}為什么對(duì)象t4的輸出與對(duì)象t3不同呢?
復(fù)制構(gòu)造函數(shù)無(wú)法正確輸出
帝國(guó)大學(xué)
2017-06-11 13:26:02