}
}
24.
#include<>
structstudent
{char*num;
char*name;
intage;
};
voidmain()
{int*ip;float*fp;char*cp;
structstudent*stp;
fp=newfloat;
cp=newchar;
*cp='A';
ip=newint[10];
for(inti=0;i<10;i++)
ip[i]=2*i+2;
stp=newstructstudent;
stp->num="12";
stp->name="Li";
stp->age=18;
cout<<*fp<<""<<*cp<<""<cout<<"num="<num<<"name="<name<<"age="<age<for(i=0;i<10;i++)
cout<cout<deletecp;
deletefp;
delete[]ip;
deletestp;
}
25.***
#include<>
classPoint
{
public:
Point(intxx=0,intyy=0)
{X=xx;Y=yy;cout<<"构造函数被调用"<Point(Point&p);
intGetX(){returnX;}
intGetY(){returnY;}
private:
intX,Y;
};
Point:
:
Point(Point&p)
{
X=;
Y=;
cout<<"拷贝构造函数被调用"<}
Pointfun2()
{PointA(1,2);
returnA;
}
voidfun1(Pointp)
{cout<<()<}
voidmain()
{PointA(1,2);
fun1(A);
PointB(A);
cout<<()<B=fun2();
cout<<()<}
26.***
#include<>
#include<>
classPoint
{
public:
Point(intxx=0,intyy=0)
{
X=xx;Y=yy;
cout<<"构造函数被调用"<}
Point(Point&p);
intGetX(){returnX;}
intGetY(){returnY;}
private:
intX,Y;};
Point:
:
Point(Point&p)
{
X=;
Y=;
cout<<"Point拷贝构造函数调用"<}
classDistance
{
public:
Distance(Pointxp1,Pointxp2);
doubleGetDis(){returndist;}
private:
Pointp1,p2;
doubledist;
};
Distance:
:
Distance(Pointxp1,Pointxp2)
:
p1(xp1),p2(xp2)
{
cout<<"Distance构造函数调用"<doublex=double()());
doubley=double()());
dist=sqrt(x*x+y*y);
}
voidmain()
{
Pointmyp1(1,1),myp2(4,5);
Distancemyd(myp1,myp2);
cout<<"Thedistanceis:
";
cout<<()<}
27.
#include<>
classBase{
public:
Base(intx,inty){a=x;b=y;}
voidShow(){cout<<"Base:
"<private:
inta,b;
};
classDerived:
publicBase{
public:
Derived(intx,inty,intz):
Base(x,y),c(z){}
voidShow(){cout<<"Derived:
"<private:
intc;
};
voidmain()
{
Baseb(50,50),*pb;
Derivedd(10,20,30);
pb=&b;
pb->Show();
pb=&d;
pb->Show();
}