}
~A(){}
friendvoidadd(A&);
private:
intm;
};
voidadd(A&a)
{
a.m+=10;
}
intmain()
{
Aa(5);
add(a);
a.show();return0;
}
(3)
#include
classX
{
private:
inta,b,c;
public:
X(inti)
{
a=i;
}
X()
{
a=b=0;
}
X(inti,intj)
{
a=i;
b=j;
}
voidsetC(intk)
{
c=c+k;
}
voidsetA(inti)
{
a=i;
}
};
voidmain()
{
Xx1;Xx2
(2);
Xx3(1,2);x1.setA(3);
}
第三题
(1)
voidmain()
{
TPointp1(4,9);//调用构造函数初始化p1
TPointp2(p1);//显示调用拷贝构造函数初始化p2
TPointp3=p2;//对象之间的赋值,由于之前没有定义p3,因此用另一个同
类的对象给其赋值时,会调用拷贝构造函数。
cout<<"p3=("<和y
}
(2)
答:
调试程序会程序错误,原因是对于无输入的对象和只要一个输入值的对象没有相应的构造函数,要解决也挺简单,就是在说明部分加上相应的构造函数就可以了。
修改后的程序如下:
#include
classTPoint
{
private:
intX,Y;
public:
TPoint()
{
X=Y=0;
cout<<"AConstructoriscalled"<}
TPoint(inti)
{
X=i;
Y=0;
cout<<"BConstructoriscalled"<}
TPoint(intx,inty)
{
X=x;
Y=y;
cout<<"Constructoriscalled"<}
TPoint(TPoint&p);
~TPoint()
{
cout<<"Destructoriscalled"<intgetx()
{
returnX;
}
intgety()
{
returnY;
}
};
TPoint:
:
TPoint(TPoint&p)
{
X=p.X;
Y=p.Y;
cout<<"Copy-initializationConstructoriscalled"<}
voidmain()
{
TPointp1(4,9);
cout<<"p1=("<TPointp2(p1);
cout<<"p2=("<TPointp3=p2;
cout<<"p3=("<(2);
cout<<"p4=("<cout<<"p5=("<}
(3)
#include
classTPoint
{
private:
intX,Y;
public:
TPoint(intx,inty)
{
X=x;
Y=y;
cout<<"Constructoriscalled"<}
TPoint(TPoint&p);
~TPoint()
{
cout<<"Destructoriscalled"<}
intgetx()
{returnX;}
intgety()
{returnY;}
};
TPoint:
:
TPoint(TPoint&p)
{
X=p.X;
Y=p.Y;
cout<<"Copy-initializationConstructoriscalled"<}
voidmain()
{
TPoint*p1=newTPoint(4,9);cout<<"p1=("<getx()<<","<gety()<<")"<
}
(4)
#include
classTPoint
{
private:
intX,Y;
public:
TPoint(intx,inty)
{
X=x;
Y=y;
cout<<"Constructoriscalled"<}
TPoint(TPoint&p);
~TPoint()
{
cout<<"Destructoriscalled"<}
intgetx()
intgety()
{
{
returnX;returnY;
}
}
voidShowPoint()
{
cout<<"p1=("<}
};
TPoint:
:
TPoint(TPoint&p)
{
X=p.X;
Y=p.Y;
cout<<"Copy-initializationConstructoriscalled"<}
voidmain()
{
cout<<"Entermain"<TPoint*p1=newTPoint(4,9);
p1->ShowPoint();
deletep1;
}
第四题:
#include
classNUM
{//定义数据类NUMprivate:
intn;
int*p;
public:
NUM(intn1)
{
n=n1;
p=newint[n];
//动态分配存放连续合数的存储
空间
}
intyes(intx)
{
for(inti=2;i<=x/2;i++)
if(x%i==0)return1;
return0;
}
voidfun(void)
{
intj;
for(inti=3;1;i++)
{
j=0;
while(yes(i)&&j{
p[j]=i;
j++;
i++;
}
if(j==n)break;
}
}
voidprint(void)
{
cout<<"找到的"<"<for(inti=0;icout<
cout<}
~NUM()
{
if(p)delete[]p;//释放在构造函数中申请的存储空
间
}
};
voidmain()
{
cout<<"请输入要求的连续合数的个数(例如10):
";
intn;
cin>>n;
NUMnum(n);
num.fun();//寻找连续的合数,并存入动态申请的存储空间中num.print();//输出寻找到的合数
}
第五题:
#include
classRectangle
{
private:
intlen,wid;
public:
Rectangle()
{
len=wid=0;
cout<<"FirstConstructoriscalled"<}
Rectangle(inti,intw)
{
len=i;
wid=w;
cout<<"SecondConstructoriscalled"<}
voidSet_Rectangle()
{
cin>>len;
cin>>wid;
}
intGet_Perimeter()
{
return2*(len+wid);
}
intGet_Area()
{
returnlen*wid;
}
intget_len()
{
returnlen;
}
intget_wid()
{
returnwid;
}
~Rectangle()
{
cout<<"Destructoriscalled"<}
};
voidmain()
{
inttemp;
Rectanglerect,Default(8,9);
cout<cout<<"默认长度:
"<"<cout<<"矩形的周长为:
"<cout<<"矩形的面积为:
"<
cout<<"是否需要修改参数(1:
修改0:
退出):
";
cin>>temp;
while(temp==1)
{
cout<<"请输入矩形的长和宽:
"<rect.Set_Rectangle();
cout<<"长度:
"<"<cout<<"矩形的周长为:
"<"<
cout<<"是否需要修改参数(1:
修改0:
退出):
";
cin>>temp;
}
cout<