private:
inta;
intb;
};
classB:
publicA
{
public:
B(){c=0;}
B(inti):
A(i){c=0;}
B(inti,intj):
A(i,j){c=0;}
B(inti,intj,intk):
A(i,j){c=k;}
voiddisplay1()
{display();
cout<<"c="<}
private:
intc;
};
intmain()
{Bb1;
Bb2
(1);
Bb3(1,3);
Bb4(1,3,5);
b1.display1();
b2.display1();
b3.display1();
b4.display1();
return0;
}
9、以下程序的执行结果是30,30。
#include
usingnamespacestd;
classPoint
{
intx,y;
public:
Point(intvx,intvy){x=vx;y=vy;}
Point(){x=0;y=0;}
Pointoperator+(Pointp1)
{
Pointp;
intpx=x+p.x;
intpy=y+p.y;
returnPoint(px,py);
}
Pointoperator-(Pointp1)
{
Pointp;
intpx=x-p1.x;
intpy=y-p1.y;
returnPoint(px,py);
}
voidprint()
{
cout<}
};
intmain()
{
Pointp1(10,10),p2(20,20);
p1=p1+p2;
p1.print();
return0;
}
10、以下程序的执行结果是baseclass
deriveclass
derive2class。
#include
usingnamespacestd;
classbase
{
public:
virtualvoidwho()
{
cout<<”baseclass”<}
};
classderive:
publicbase
{
public:
voidwho()
{
cout<<”deriveclass”<}
};
classderive2:
publicbase
{
public:
voidwho()
{
cout<<”derive2class”<}
};
intmain()
{
baseobj1,*p;
deriveobj2;
derive2obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();
p=&obj3;
p->who();
return0;
}
11、以下程序的执行结果是f1functionofbase
f2functionofbase
f3functionofbase
f1functionofderive
f2functionofbase
f4functionofderive。
#include
usingnamespacestd;
classbase
{
public:
virtualvoidf1()
{
cout<<”f1functionofbase”<}
virtualvoidf2()
{
cout<<”f2functionofbase”<}
virtualvoidf3()
{
cout<<”f3functionofbase”<}
virtualvoidf4()
{
cout<<”f3functionofbase”<}
};
classderive:
publicbase
{
voidf1()
{
cout<<”f1functionofderive”<}
voidf2(intx)
{
cout<<”f2functionofderive”<}
voidf4()
{
cout<<”f4functionofderive”<}
};
intmain()
{
baseobj1,*p;
deriveobj2;
p=&obj1;
p->f1();
p->f2();
p->f3();
p=&obj2;
p->f1();
p->f2();
p->f4();
return0;
}
12、以下程序的执行结果是(10,20)
(13,23)
(16,26)。
#include
classPoint
{
public:
Point(){x=0;y=0;}
Point(unsignedx,unsignedy)
{
Point:
:
x=x;
Point:
:
y=y;
}
unsignedgetx(){returnx;}
unsignedgety(){returny;}
voidprint()
{
cout<<”Point(“<}
friendPointoperator+(Point&pt,intnoffset);
friendPointoperator+(intnoffset,Point&pt);
private:
unsignedx,y;
};
Pointoperator+(Point&pt,intnoffset)
{
Pointptemp=pt;
ptemp.x+=noffset;
ptemp.y+=noffset;
returnptemp;
}
Pointoperator+(intnoffset,Point&pt)
{
Pointptemp=pt;
ptemp.x+=noffset;
ptemp.y+=noffset;
returnptemp;
}
intmain()
{
Pointpt(10,20);
pt.print();
pt=pt+3;
pt.print();
pt=3+pt;
pt.print();
return0;
}
四、程序分析题
1、分析下列程序的输出结果(注:
除了给出结果外,还要进行简单分析)
#include
usingnamespacestd;
classL
{
public:
voidInitL(intx,inty){X=x;Y=y;}
voidMove(intx,inty){X+=x;Y+=y;}
intGetX(){returnX;}
intGetY(){returnY;}
private:
intX,Y;
};
classR:
publicL
{
public:
voidInitR(intx,inty,intw,inth)
{
InitL(x,y);
W=w;
H=h;
}
intGetW(){returnW;}
intGetH(){returnH;}
private:
intW,H;
};
classV:
publicR
{
public:
voidfun(){Move(3,2);}
};
intmain()
{
Vv;
v.InitR(10,20,30,40);
v.