5-1
阅读下面的程序,完成其中复制构造函数的代码。
#include <iostream>
using namespace std;
class CAT
{ public:
CAT();
CAT(const CAT&);
~CAT();
int GetAge() const { return *itsAge; }
void SetAge(int age){ *itsAge=age; }
protected:
int* itsAge;
};
CAT::CAT()
{ itsAge=new int;
*itsAge =5;
}
CAT::CAT(const CAT& c)
{
itsAge=new int(5分);
*itsAge=*c.itsAge(5分);
}
CAT::~CAT()
{ delete itsAge; }
5-2
using namespace std;
class A{
int i;
public:
A(int k=0){
i=k;
}
void display(){
cout<<"i="<<i<<endl;
}
};
int main()
{
A*p=new A;(2分)//动态创建对象p,创建的是对象,不是int * p =new int(不是传统的了,可以那么理解叭?)
p->display();
delete p;(2分)//删除对象p,送分题
p=new A(8);
p->display();
delete p;
p=new A[3];(2