#include <iostream>
using namespace std;
#include <string>
#include <time.h>
#include<stdio.h>
#include<stdlib.h>
//编译器处理做浅拷贝 我们需要深拷贝
class Person
{
public:
Person()
{
cout<<"Person的默认构造函数"<<endl;
}
Person(int age,int height)
{
cout<<"Person的有参构造函数"<<endl;
m_age=age;
m_height=new int(height);//在堆区申请一块新的内存
}
Person(const Person&p)
{
m_age=p.m_age;
cout<<"Person拷贝函数调用"<<endl;
//m_height=p.m_age编译器默认实现就是这行代码
m_height= new int(*p.m_height);//深拷贝操作
}
~Person()
{
cout<<"Person的析构函数"<<endl;//析构代码 将堆区的开辟的数据释放操作
if(m_height!=NULL)
{
delete m_height;
}
}
int m_age;
int*m_height;
};
void test01()
{
Person p1(18,160);
Person p2(p1);
cout<<"p1的年龄"<<p1.m_age<<"身高为"<<*p1.m_height<<endl;
cout<<"p2的年龄"<<p2.m_age<<"身高为"<<*p2.m_height<<endl;
}
/*void test02()
{
Person p;
Person p2(p);
cout<<"p2的年龄"<<p2.m_age<<endl;
}*/
int main()
{
//test01();
test01();
system("pause");
return 0;
}