版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
《C++程序设计》
分类练习题
《C++程序设计》课程组编写
目录
第一部分简答题与论述题................................1
1.1简答题........................................1
1.2简述题.......................................1
第二部分分析程序运行结果或给出程序功能................3
2.1结构化程序设计................................3
2.2数组与指针...................................5
2.3函数.........................................9
2.4类与对象基本知识............................12
2.5类继承......................................18
2.6类多态......................................22
2.7I/O与文件操作..............................26
第三部分程序设计部分................................27
3.1类的定义(只给出类结构的声明)..............27
3.2类操作的实现................................28
3.3继承与派生..................................31
3.4重载........................................34
3.5文件操作....................................37
第四部分综合应用系统的分析、设计..................39
第五部分常见的通用算法与设计思想....................41
第一部简答题与论述题
一、简答题
(一)定义功能说明
1、分别说明下列定义的含义:
intpl;char*p6[10]
intp2l10J;int**p7;
int*p3;intp8();
int*p4[10]int(*p9)();
char*p5;intplO[IOJ[lOJ;
(二)类的基本性质特点
1、面向对象程序设计方法的基本特征主要包括哪四项?
2、构造函数及析构函数具有哪些功能?它们分别在何时执行?
(三)多态性的定义
1、运算符重载有哪两种方式,分别给出定义运算符重载的格式?
(四)继承与派生的定义
1、什么是继承与派生?
2、继承的三种继承方式是什么?
3、由基类而生成的派生类需要经历哪3个步骤?
第二部分分析程序运行结果或给出程序功能
(一)结构化程序设计练习题目
1.给出程序的运行结果:
#include<iostream.h>
voidmain()
(
inti,sum=0;
i=l;
do{
sum二sum+i;
i=i+1;
}while(i<=10);
cout«nsum="«sum«endl;
)
2.给出程序的运行结果:
#include<iostream.h>
voidmain()
|
intm;
m=9;
cout«"pleaseinputanumber:\nM;
cin»m;
inti;
for(i=2;i<m;i++)
if(m%i==0)
break;
if(m==i)
cout«m«nisprimeAn";
else
cout«m«"isn'tprimeAn";
)
3.给出程序的运行结果:
#include<iostream.h>
voidmain()
inta=3,b=2,c=l;
intd,logic;
d=a++;
cout«,"a="«a«",d=',«d«endl;
d=++b;
cout«"b=,,«b«",d=,'«d«endl;
b*=a+c;
cout«a«',,,,«b«n,,,«c«endl;
b=2;
logic=a>b;
cout«nlogic=n«logic«endl;
logic=(c<a)ll(c<b);
cout«nlogic=n«logic«endl;
)
4.给出程序的运行结果:
#include<iostream.h>
voidmain()
{
inti=l,sum=0;
while(i<10){
sum+=i++;
if(sum>20)
break;
)
cout«i;
)
5.给出程序的运行结果:
#include<string.h>
#include<iostream.h>
voidmain()
(
charstring[8OJ;
strcpy(string,nHelloworldfrom");
strcat(string,"strcpy");
strcat(string,"and");
strcat(string,"strcat!");
cout«string«endl;
)
6.给出程序的运行结果:
#include<string.h>
#include<iostream.h>
#include<string.h>
#include<iostream.h>
chara[10]=Hhellou;
charb[10]=',worldn;
voidmain()
(
intc;
c=strcmp(a,b);
if(c==0)
cout«Haisequaltobu«endl;
else
if(c>0)
cout«naisgreaterthanbu«endl;
else
cout«naislessthanbM«endl;
)
7.给出程序的运行结果:
#include<iostream.h>
voidmain()
(
inta=-l,b=4,k;
k=(a++<=0)&&!(b-<=0);
cout«k«a«b«endl;
)
8.给出程序的运行结果:
#include<iostream.h>
intm[3][3]={{1},{2},{3});
intn[3][3]={1,2,3);
voidmain()
{cout«m[l][0]4-n[01f0]«,\n,«m[0][l]+n[l][0]«,\n,;
(二)数组与指针部分练习题目
1.给出程序的运行结果:
#include<iostream.h>
#include<string.h>
voidmain()
(
charch[J="howareyou";
cout«Hsizeofarray:"«sizeof(ch)«endl;
cout«usizeofstring:"«strlen(Hhowareyou")«endl;
(提示:各单词之间有一个空格)
2.给出程序的运行结果:
#include<iostream.h>
voidmain()
{int*iPtr;
intiCount=18;
iPtr=&iCount;
cout«*iPtr«endl;
)
3.给出程序的运行结果:
#include<iostream.h>
voidmain()
{intiArray[4];
intsum=0;
int*iPtr=iArray;
for(inti=0;i<4;i++)
iArray[i]=i*2;
for(intindex=0;index<4;index++){
sum+=*iPtr;
iPtr++;
)
cout«"sumis”«sum«endl;
4.给出程序的运行结果:
#include<iostream.h>
voidmystrcpyCchar*dest,constchar*source)
{
while(*dest++=*source++);
)
voidmain()
(
chara[20]=nHowareyou!";
charb[20J;
mystrcpy(b,a);
cout«b«endl;
5.给出程序的运行结果:
#include<iostream.h>
voidmain()
charbuffer[10]="ABC”;
char*pc;
pc=Hhellou;
cout«pc«endl;
pc++;
cout«pc«endl;
cout<v*pc«endl;
pc=buffer;
cout«pc;
1
6.给出程序的运行结果:
#include<iostream.h>
#include<string.h>
voidmain()
(
charbuffer1[10]="hello";
charbuffer2llOJ="hello";
if(strcmp(buffer1,buffer2)==0)
cout«"equal\n";
else
cout«"notequal\n";
)
7.给出程序的运行结果:
#include<iostream.h>
voidmain()
(
int*v,b;
v=&b;b=100;*v+=b;
cout«b;
)
8.给出程序的运行结果:
#include<iostream.h>
voidcallbyval(inta,intb,intc)
{a=3;b=2;c=l;}
voidcallbypointer(int*a,int*b,int*c)
{*a=3;*b=2;*c=l;}
voidcallbyreference(int&a,int&b,int&c)
{a=l;b=2;c=3;}
voidmain()
inta=l,b=2,c=3;
int&al=a;
int&bl=a;
int&cl=a;
callbyval(a,b,c);
cout«a«b«c«endl;
callbypointer(&a,&b,&c);
cout«a«b«c«endl;
callbyreference(al,bl,cl);
cout«a«b«c«endl;
)
9.给出程序的运行结果:
#include<iostream.h>
voidast(intx,inty,int*cp,int*dp)
(
*cp=x*y;
*dp=x%y;
)
voidmain()
{inta,b,c,d;
a=2;b=3;
ast(a,b,&c,&d);
cout«c«d«endl;
10.给出程序的运行结果:
#include<iostream.h>
intf(char*s)
{char*p=s;
while(*p)p++;
returnp-s;
)
voidmain(void)
{cout«f(nABCDEFn);}
11.给出程序的运行结果:
#include<iostream.h>
voidmain()
{inta[10]={10,20,30,40,50,60},*pl=&a[0],*p2=&a[5];
coutvv"*p1=u«*pI«n,*p2=u«*p2;
pl++;p2-;
cout«n\n*p1="vv*p1vv”,*p2="vv*p2;
pl+=2;
cout«"\n*pl="«*pl«",*(p2+1)="«*(p2+1);
cout«"\np2与pl之间整型变量的个数="«p2-pl;
(三)函数部分练习题目
1.给出程序的运行结果:
#include<iostream.h>
voidfunc();
intn=l;
voidmain()
(
staticinta;
intb=-10;
cout«na:u«a«nb:H«b«nn:"«n«endl;
b+=4;
func();
cout«"a:u«a«ub:"«b«"n:H«n«endl;
n+=10;
func();
)
voidfunc()
{
staticinta=2;
intb=5;
a+=2;
n+=12;
b+=5;
cout«Ha:u«a«"b:M«b«Hn:"«n«endl;
)
2.给出程序的运行结果:
#include<iostream.h>
intsum(int[],int);
voidmain()
(
staticintia[5]={2,3,6,8,10};
intsumOfArray;
sumOfArray=sum(ia,5);
cout«"sumofarray:"«sumOfArray«endl;
intsum(intarray[],intlen)
intiSum=O;
for(inti=0;i<len;i++)
iSum+=array[i];
returniSum;
)
3.给出程序的运行结果:
#include<iostream.h>
intmaximum(int[][4],int,int);
voidmain()
{
intsg[3][4]={{68,77,73,86),
{87,96,78,89),
{90,70,81,86));
cout«nthemaxgradeis:"«maximum(sg,34)«endl;
)
intmaximum(intgrade[][4],intpupils,inttests)
(
intmax=0;
for(inti=0;i<pupils;i++)
for(intj=0;j<tests;j++)
if(grade[i][j]>max)
max=gradefi][j];
returnmax;
)
4.给出程序的运行结果:
#include<iostream.h>
voidswap(intjnt);
voidmain()
(
inta=3,b=8;
cout«"a=n«a«",b=M«b«endl;
swap(a,b);
cout«"afterswapping...\nn;
cout«Ma=n«a<<",b=M«b«endl;
)
voidswap(intx,inty)
(
inttemp=x;
x=y;
y=temp;
5.给出程序的运行结果:
#include<iostream.h>
voidswap(int&x,int&y);
voidmain()
(
intx=5,y=6;
cout«"beforeswap,x:n«x«",y:n«y«endl;
swap(x,y);
cout«nafterswap,x:"«x«*',y:"«y«endl;
)
voidswap(int&rx,int&ry)
(
inttemp=rx;rx=ry;ry=temp;
)
6.给出程序的运行结果:
#include<iostream.h>
double&fn(constdouble&pd)
(
staticdoublead=32;
ad+=pd;
cout«Hfnbeingcalled...thevalueis:"«pd«endl;
returnad;
)
voidmain()
(
doublea=345.6;
double&pa=fn(a);
cout«pa«endl;
a=55.5;
pa=fn(a);
cout«pa«endl;
)
7.给出程序的运行结果:
#include<iostream.h>
voidprint(doublea)
(
cout«++a;
)
voidprint(inta,intb)
cout«b«a;
voidmain()
(
print(1.2);
cout<v"
print(3,4);
)
8.给出程序的运行结果:
#include<iostream.h>
classSample
{
intn;
public:
Sample(inti){n=i;}
friendintadd(Sample&sl,Sample&s2);
);
intadd(Sample&sl,Sample&s2)
(
returnsl.n+s2.n;
)
voidmain()
(
Samplesi(10),s2(20);
cout«add(sl,s2)
(四)类与对象基本知识部分的练习题目
1.给出程序的运行结果:
#include<iostream.h>
classStudent(
public:
Student()
{
cout«"constructingstudent.\n";
)
);
classTeacher{
public:
Teacher()
cout«nconstructingteacher.\nn;
);
classTutorPair{
public:
TutorPair()
(
cout〈〈”constructingtutorpair.\n";
)
protected:
Studentstudent;
Teacherteacher;
);
voidmain()
{
TutorPairtp;
cout«"backinmain.'n”;
)
2.给出程序的运行结果:
#include<iostream.h>
classStudent{
public:
Student()
(
cout«"constructingstudent.\nM;
)
〜Student。
(
cout«"destructingstudent.\n";
}
);
classTeacher{
public:
Teacher()
{
cout«"constructingteacherAn";
}
~Teacher()
{
cout«"destructingteacherAn";
}
);
classTutorPair{
public:
TutorPair()
(
cout«"constructingtutorpair.\nn;
)
-TutorPair()
(
cout«Hdestructingtutorpair.\n";
)
protected:
Studentstudent;
Teacherteacher;
);
voidmain()
(
TutorPairtp;
cout«nbackinmain.'n”;
)
3.给出程序的运行结果:
#include<iostream.h>
classTdate{
public:
Tdate(intm=4,intd=15,inty=1995)
(
month=m;day=d;year=y;
cout«month«'7"«day«year«endl;
)
protected:
intmonth;
intday;
intyear;
);
voidmain()
(
Tdateaday;
Tdatebday(2);
Tdatecday(3,12);
Tdatedday(l,2,1998);
)
4.给出程序的运行结果:
#include<iostream.h>
classA{
public:
A(intj):age(j),num(j+1)
(
cout«nage:"«age«",num:"«num«endl;
)
protected:
intnum;
intage;
);
voidmain()
(
Asa(15);
)
5.给出程序的运行结果:
#include<iostream.h>
classMyclass
(
public:
Myclass(inta,intb,intc);
voidPrintNumber();
voidPrinlSum();
private:
intA,B,C;
staticintSum;
);
intMyclass::Sum=0;
Myclass::Myclass(inta,intb,intc)
(
A=a;
B=b;
C=c;
Sum+=A+B+C;
)
voidMyclass::PrintNumber()
{cout«,'Number=H«A«,V,«B«,',n«C«endl;}
voidMyclass::PrintSum()
{cout«',Sum=',«Sum«endl;}
voidmain()
MyclassM(3,7,10),N(14,9,l);
M.PrintNumber();
N.PrintNumber();
M.PrintSum();
N.PrintSum();
)
6.给出程序的运行结果:
#include<iostream.h>
classMyclass
(
public:
Myclass();
voidGetSum(inta);
intSum;
private:
intA;
);
Myclass::Myclass()
{
Sum=10;
)
voidMyclass::GetSum(inta)
(
A=a;
Sum+=A;
)
voidPrintSum(Myclass&A)
(
cout«"Sum="«A.Sum«endl;
)
voidmain()
(
MyclassM,N;
M.GetSum(3);
N.GetSum(7);
PrintSum(M);
)
7.给出程序的运行结果:
#include<iostream.h>
#include<math.h>
classPoint
(
public:
Point(doublexx,doubleyy){x=xx;y=yy;}
voidGetxy();
frienddoubleDistance(Point&a,Point&b);
private:
doublex,y;
);
voidPoint::Getxy()
(
cout«,'<',«x«H;,«y«u),'«endl;
)
doubleDistance(Point&a,Point&b)
(
doubledx=a.x-b.x;
doubledy=a.y-b.y;
returnsqrt(dx*dx+dy*dy);
)
voidmain()
(
Pointpl(3.0,4.0),p2(6.0,8.0);
pl.Getxy();
p2.Getxy();
doubled=Distance(pl,p2);
cout«HDistanceisn«d«endl;
)
8.给出程序的运行结果:
#include<iostream.h>
classA
{
public:
A(intx,inty){X=x;Y=y;}
inta(){returnX;}
intb(){returnY;)
intc();
intd();
private:
intX,Y;
);
inlineintA::c()
(
returna()+b();
)
inlineintA::d()
returnc();
voidmain()
(
Am(3,5);
inti=m.d();
cout«i«endl;
)
9.给出程序的运行结果:
#include<iostream.h>
classB
(
inti;
public:
B(intx){i=x;}
friendclassA;
);
classA
(
inti;
public:
intset(B&);
intget(){returni;}
A(intx){i=x;}
);
intA::set(B&b)
(
returni=b.i;
)
voidmain()
(
Aa(l);
Bb(2);
cout«a.get()«',,n;
a.set(b);
cout«a.get();
(五)继承部分练习题目
1.给出程序的运行结果:
#include<iostream.h>
classBase{
public:
virtualvoidfn()
{
cout«"InBaseclass'd;
)
);
classSubClass:publicBase{
public:
virtualvoidfn()
{
cout«"InSubClass\n";
}
);
voidtest(Base&b)
(
b.fn();
)
voidmain()
(
Basebe;
SubClasssc;
cout«HCallingtest(bc)\n";
test(bc);
cout«nCallingtest(sc)\n";
test(sc);
)
2.给出程序的运行结果:
#include<iostream.h>
classBase{
public:
voidfn()
(
cout«HInBaseclass\nn;
)
);
classSubClass:publicBase{
public:
voidfn()
(
cout«"InSubClass\nn;
)
);
voidtest(Base&b)
b.fn();
)
voidmain()
(
Basebe;
SubClasssc;
cout«"Callingtest(bc)\n";
test(bc);
cout«"Callingtest(sc)\n";
test(sc);
)
3.给出程序的运行结果:
#include<iostream.h>
classAnimal{
public:
Animal(){}
voideat(){cout«"eat.\n";}
);
classGiraffe:privateAnimal{
public:
Giraffe(){}
voidStretchNeck(){cout«nstretchneck.\nu;}
voidtake(){eat();}
);
voidFunc(Giraife&an)
(
an.take();
)
voidmain()
(
Giraffegir;
gir.StretchNeck();
Func(gir);
)
4.给出程序的运行结果:
#include<iostream.h>
classBl
{intx;
public:
Bl(inta){x=a;cout<<”基类Bl的构造函数!\n";}
〜Bl(){coutvv”基类Bl的析构函数!\n”;}
);
classB2
{inty;
public:
B2(inta){y=a;coutvv”基类B2的构造函数!\n";}
~B2(){cout<<"基类B2的析构函数!\n";}
);
classD:publicBl,publicB2
{intz;
Blbl,b2;
public:
D(inta,intb):B1(a),B2(20),bl(200),b2(a+b)
{z=b;coutvv”派生类D的构造函数!\n";}
~D(){coutcv"派生类D的析构函数!\n”;}
);
voidmain(void)
{D<1(100,200);}
5.给出程序的运行结果:
#include<iostream.h>
classA{
protected:
intx;
public:
A(){x=1000;}
voidprint(){cout«',x=',«x«'\t,;}
);
classB:publicA{
inty;
public:
B(){y=2000;}
voidprint()
{cout«',y="«y«,\n,;}
);
voidmain(void)
{Aa,*pa;
Bb;
a.print();b.print();
pa=&a;pa->print();
pa=&b;pa->print();
(六)多态部分练习题目
1.给出程序的运行结果:
#include<iostream.h>
classCDate
(
public:
CDate();
CDate(intd);
CDate(intm,intd);
CDate(inty,intm,intd);
protected:
intyear;
intmonth;
intday;
);
CDate::CDate()
(
year=1999;month=ll;day=24;
cout«year«,7u«month«,7"«day«endl;
)
CDate::CDate(intd)
(
year=1999;month=11;day=d;
cout«year«'7"«month«'7"«day«endl;
)
CDate::CDate(intm,intd)
(
year=1999;month=m;day=d;
cout«year«'7,,«month«'7"«day«endl;
)
CDate::CDate(inty,intm,intd)
(
year=y;month=m;day=d;
cout«year«'V"«month«n/"«day«endl;
)
voidmain()
(
CDateaday;
CDatebdate(10);
CDatecdate(8,8);
CDateddate(1998,1,1);
2.给出程序的运行结果:
#include<iostream.h>
classA
(
public:
virtual~A(){cout«nA:>A()CalledAn";}
);
classB:publicA
(
public:
B(inti){buf=newchar[i];}
virtual-B()
(
deleteUbuf;
cout«MB::-B()Called.\nM;
}
private:
char*buf;
);
voidfun(A*a)
(
deletea;
)
voidmain()
(
A*a=newB(15);
fun(a);
)
3.给出程序的运行结果:
#include<iostream.h>
classUndergraduate
(
public:
voidDisplay()
(
cout<<nCallBaseClassn<<endl;
cout<<nUnergraduateLiMingn<<endl;
)
};
classMasteripublicUndergraduate
public:
voidDisplay()
cout<<*'CallMasterclass**<<endl;
cout<<nMasterWangWein<<endl;
}
};
classDoctoripublicMaster
{
public:
voidDisplay()
(
cout<<*'CallDoctorClassH<<endl;
cout<<nDoctorZhangHuan<<endl;
}
);
voidmain()
(
Undergraduatesiz*pointer;
Masters2;
Doctors3;
pointer=&sl;
pointer->Display();
pointer=&s2;
pointer->Display();
)
4.给出程序的运行结果:
#include<iostream.h>
classpoint
(
public:
point(doubleI,doublej)
{x=I;y=j;}
virtualdoubleArea()
{return0.0;}
private:
doublex,y;
};
classRectangle:publicpoint
(
public:
Rectangle(doubleI,doublej,doublek,double1);
virtualdoubleArea()
returnw*h;
)
private:
doublew,h;
);
Rectangle::Rectangle(doubleI,doublej,doublek,double1):point(I,j)
(
w=k;h=l;
)
voidfun(point&s){
cout«s.Area();
}
voidmain()
(
Rectanglerec(3.0,5.2,15.0,25.0);
fun(rec);
)
5.给出程序的运行结果:
#include<iostream.h>
classA{
intx;
public:
A(inta=0){x=a;cout«HcallA(int=0)\n";}
);
classB{
inty;
public:
B(inta=0){y=a;cout«HcallB(int=0)\n";}
);
classC:publicB,virtualpublicA{
intz;
public:
C(inta=0,intb=0):B(b+20),A(b)
{z=a;cout«"callC(int=0,int=0)\nM;}
);
voidmain(void)
{Cc(100,200);}
(七)I/O与文件操作部分练习题目
1.给出该程序的功能:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
voidmain()
{fstreamoutfile,infile;
infile.open(,'f2.dat",ios::in);
if(!infile)
{cout«"f2.datcan'topen."«endl;
abort();}
outfile.open(,'f4.dat,',ios::out);
if(!outfile)
{cout«"f4.datcan'topen."«endl;
abort();}
charch;
while(infile.get(ch))
outfile.put(ch);
infile.close();
outfile.close();
)
2.给出程序的运行结果:
#include<iostream.h>
voidmain()
{ofstreamostrm;
ostrm.open(〃fl.dat〃);
ostrm<<120<<endl;
ostrm«310.85«endl;
ostrm.close();
ifstreamistrm(〃fl.dat〃);
intn;
doubled;
istrm>>n»d;
cout<<n«,z,,,«d<<endl;
istrm.close();
)
第三部分程序设计部分
一、类的定义(只给出类结构的声明)
(注意:对于成员函数,如果有参数,必须注明参数的含义
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年云南省特种设备非金属焊接作业证考试练习题
- 供应链管理优化协议书
- 制定安全生产方案(2篇)
- 防溺水观后感300字左右作文
- 安全生产检查计划方案(2篇)
- 机器学习(慕课版)实验及参考结果 实验2 基于Bayes的手写字符识别
- 高性能功能陶瓷结构陶瓷项目可行性报告
- 2024年包装测试设备项目建议书
- 2024年媒介代理项目建议书
- 微合金粉末项目综合评估报告
- 信息技术应用能力提升20优秀设计-多技术融合英语教学案例
- 瑞凌焊机维修手册
- 厦门旅游景点介绍英文PPT课件
- 中国癫痫临床诊疗指南完整版
- 高速公路工地试验室建设规划设计方案
- 浮选机三大规程
- 新人教四年级下册数学每日一练
- 河北人民版小学科学四年级上册教学课件3-12《让灯泡亮起来》
- 小学三年级1000以内的进退位竖式加减法(A4直接打印版)
- 四氯乙烯干洗机操作流程
- 毕业设计论文中期检查模板.doc
评论
0/150
提交评论