2 回答
TA贡献1891条经验 获得超3个赞
//#include "stdafx.h"
#include "iostream.h"
class CAT
{
int *itsAge;
public:
CAT():itsAge(new int(5)){}
//增加拷贝构造函数
CAT(CAT &c)
{
itsAge = new int(0);
*itsAge=*c.itsAge;//把c中的itsAge指针指向的内容赋给当前对象的*itsAge
}
~CAT()
{
delete itsAge;
}
int GetAge() const {return *itsAge;}
void setAge(int age)
{
*itsAge=age;
}
};
void main()
{
CAT fri;
cout<<"1111111 "<<fri.GetAge()<<endl;
fri.setAge(6);
CAT boot(fri);
cout<<"1111112 "<<fri.GetAge()<<endl;
cout<<"2222221 "<<boot.GetAge()<<endl;
fri.setAge(7);
cout<<"1111113 "<<fri.GetAge()<<endl;
cout<<"2222222 "<<boot.GetAge()<<endl;
}
TA贡献1786条经验 获得超11个赞
//#include
"stdafx.h"
#include
"iostream.h"
class
CAT
{
int
*itsAge;
public:
CAT():itsAge(new
int(5)){}
//增加拷贝构造函数
CAT(CAT
&c)
{
itsAge
=
new
int(0);
*itsAge=*c.itsAge;//把c中的itsAge指针指向的内容赋给当前对象的*itsAge
}
~CAT()
{
delete
itsAge;
}
int
GetAge()
const
{return
*itsAge;}
void
setAge(int
age)
{
*itsAge=age;
}
};
void
main()
{
CAT
fri;
cout<<"1111111
"<<fri.GetAge()<<endl;
fri.setAge(6);
CAT
boot(fri);
cout<<"1111112
"<<fri.GetAge()<<endl;
cout<<"2222221
"<<boot.GetAge()<<endl;
fri.setAge(7);
cout<<"1111113
"<<fri.GetAge()<<endl;
cout<<"2222222
"<<boot.GetAge()<<endl;
}
添加回答
举报