}
答案:
callingtheconstructorfunction.
callingtheconstructorfunction.
callingthecopy_initializationconstructorfunction.
callingthecopy_initializationconstructorfunction.
OK!
callingtheconstructorfunction.
callingthecopy_initializationconstructorfunction.
callingthedestructorfunction.
callingthedestructorfunction.
callingthedestructorfunction.
P=25,60
callingthedestructorfunction.
callingthedestructorfunction.
callingthedestructorfunction.
说明:
(1)构造函数执行三次,分别初始化主函数中的对象m,p和move函数中的对象r。
(2)拷贝构造函数共执行了三次。
第一次,初始化对象n;第二次在调用函数move()时,实参n给形参q进行初始化;第三次是执行函数move的returnr;语句时,系统用r初始化一个临时对象时使用了拷贝构造函数。
(3)析构函数执行了六次。
在退出函数move时释放对象r和q共调用二次;返回主函数后,临时对象赋值给对象p后,释放临时对象又调用一次析构函数;最后退出整个程序时释放对象m,n和p调用三次。
四、编程题
1.根据下面类中Count函数成员的原型和注释写出它的类外定义。
classAA{
int*a;
intn;
intMS;
public:
voidInitAA(intaa[],intnn,intms){
if(nn>ms){cout<<"Error!
"<(1);}
MS=ms;
n=nn;
a=newint[MS];
for(inti=0;i}
intCount(intx);根据下面类中Search函数成员的原型和注释写出它的类外定义。
classAA{
int*a;
intn;
intMS;
public:
voidInitAA(intaa[],intnn,intms){
if(nn>ms){cout<<"Error!
"<(1);}
MS=ms;
n=nn;
a=newint[MS];
for(inti=0;i}
intSearch(intx);根据下面类中MaxMin函数成员的原型和注释写出它的类外定义。
classAA{
int*a;
intn;
intMS;
public:
voidInitAA(intaa[],intnn,intms){
if(nn>ms){cout<<"Error!
"<(1);}
MS=ms;
n=nn;
a=newint[MS];
for(inti=0;i}
intMaxMin(int&x,int&y);根据下面类中Compare函数成员的原型和注释写出它的类外定义。
classAA{
int*a;
intn;
intMS;
public:
voidInitAA(intaa[],intnn,intms){
if(nn>ms){cout<<"Error!
"<(1);}
MS=ms;
n=nn;
a=newint[MS];
for(inti=0;i}
intCompare(AAb);根据下面类中CompareBig函数成员的原型和注释写出它的类外定义。
classAA{
int*a;
intn;
intMS;
public:
voidInitAA(intaa[],intnn,intms){
if(nn>ms){cout<<"Error!
"<(1);}
MS=ms;
n=nn;
a=newint[MS];
for(inti=0;i}
intCompareBig(AAb);//比较*this与b的大小,从前向后按两数组
//中的对应元素比较,若*this中元素值大则返回1,若b中
//元素值大则返回-1,若相等则继续比较下一个元素,直到
//一个数组中无元素比较,此时若两者的n值相同则返回0,
//否则若*this中的n值大则返回1,若b中的n值大则返回-1。
};
答案:
intAA:
:
CompareBig(AAb)
{
intk;
if(n>k=;elsek=n;
for(inti=0;iif(a[i]>[i])return1;
elseif(a[i]<[i])return-1;
if(k==n&&k==return0;
elseif(kelsereturn-1;
}