C 习题与解析(引用-04)

文章作者 100test 发表时间 2007:03:10 16:59:24
来源 100Test.Com百考试题网


题6.阅读下面的程序与输出结果,添加一个拷贝构造函数来完善整个程序
#include
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()
{
0delete itsage.
itsage=0.
}
void main()
{
Cat frisky.
cout<<"frisky’s age:"<cout<<"setting frisky to 6...\n".
frisky.setage(6).
cout<<"creating boots from frisky\n".
Cat boots(frisky).
cout<<"frisky’s age:"<cout<<"boots’age:"<cout<<"setting frisky to 7...\n".
frisky.setage(7).
cout<<"frisky’s age:"<cout<<"boots’age:"<}

当添加了拷贝构造函数后,程序的运行结果为:
frisky’s age:5
setting frisky to 6...
creating boots from frisky
frisky’s age:6
boots’age:6
setting frisky to 7...
frisky’s age:7
boots’age:6

解:
添加的拷贝构造函数如下:
Cat::Cat(const Cat&. c)
{
itsage=new int.
*itsage=*c.itsage.
}


相关文章


C 习题与解析(友元-02)
C 习题与解析(友元-01)
C 习题与解析(引用-04)
[二级C试题天天练]C语言考试试题10
C 习题与解析(引用-03)
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛