c++模拟题14.docx
《c++模拟题14.docx》由会员分享,可在线阅读,更多相关《c++模拟题14.docx(38页珍藏版)》请在冰点文库上搜索。
![c++模拟题14.docx](https://file1.bingdoc.com/fileroot1/2023-6/1/79e51cd1-1a2b-449e-82ad-773dea3f83de/79e51cd1-1a2b-449e-82ad-773dea3f83de1.gif)
c++模拟题14
模拟题_
一、判断对错题(10%)(对错X)
()1.自动变量用堆方式创建,它与块共存亡。
()2.运算符+二是右结合的。
()3.表达式cout«99的类型为int。
()4.++i是左值,但i++不是左值。
()5.Y[I][J]与*(*Y+I)+J不等价。
()6.从外部看类的private成员和protected成员有区别。
()7.运算符&不可以重载。
()8.基类的protected成员经过protected派生,在派生类中它变成了private成员。
()9.2.0/-3.0是一个正确的表达式。
()10.字符'\12'的ASCII为12。
二、填空题(20%)
ttinclude
typedefstructnode
{
longdata;
node*next;
};
classstack
{
private:
node*head;
1
stack();
'stack();
voidpush(②);
3pop();
4print();
};
stack:
:
stack()
{head=new⑤
head->next=⑥}
stack:
:
stack()
{
node*p;
p=head;
while(p)
{
head二head->next;
⑦p=head;
voidstack:
:
push(longx)
{
node*p二newnode;
p->data=x;
p->next二head-〉next;
w二p;
cout«"Push"«x«/,ok!
"<}
longstack:
:
pop()
{
node*p=head->next;
if(p!
=NULL)
{
longx=p->data;
head->next=p->next:
⑨
cout«^popok!
//«endl;
returnx;
}
cout«//Stackisempty!
/*«endl;
return0;
}
voidstack:
:
print()
node*p二head->next;cout«,,Stack_Top,,<cout«p->data«endl;
⑩;
}
cout〈<"Stack_bottom,,<}一
三、简答题(20%)(按条列出论点)
1.注解。
2.new运算。
3.宏定义。
4.private成员。
5.
构造函数。
1.写一基于文件流的程序,删除C++源程序中的单行注解。
要求:
C++源程序文件名和删除单行注解后的文件名均在命令行中给出。
2.己知A[N]是整数数组,试以递归函数实现求N个整数之和。
3.请为Fraction类(分数类)定义下列重载运算符函数(注意函数原型)
⑴复合赋值运算+二。
⑵等于运算二。
⑶插入运算《。
classFraction
{
private:
intfz;//分子
intfm;//分母
public:
•■•
};
模拟题一参考答案
一、判断对错题(10%)本题共10分,每题1分。
①X
②V
③X
④V
⑤J
⑥V
⑦X
⑧V
⑨V
⑩X
二、填空题(20%)
本题共20分,每空2分。
答案只少“;”或“:
”等符号的给1分。
①publie:
②long
③long
④void
⑤node;
©NULL;
⑦de1etep;
⑧head->next
⑨deletep;
⑩p二p->next
三、简要论述题(20%)
本题共20分,每题4分。
概念解释正确给2分;论述要求有两条论点,每
条给]分
、(非参考答案中的论点只要正确同样给分)。
⑴注解
O写程序者为读程序者作的说明。
OC卄编译器把所有的注解视为空白。
O注解有多行注解(/**/)和单行注解(//)O
O注解通常用來说明程序或模块的名称、用途、编写时间、编写人、变量说明和算法说明等。
⑵new运算
onew是个单目运算,功能是给程序实体动态地分配空间。
。
语法格式X类型指针二newX类型。
o用new申请空间的同时还可以进行初始化。
o用new申请的空间可用delete运算收回。
⑶宏定义
O宏定义就是用一个宏名字來命名一个字符串。
O编译预处理时宏名将被用宏体简单替换。
O使用宏定义可以提高程序的可读性、可修改性与可移植性。
O宏定义可以带参数。
(4)private成员
oprivate成员为类成员的一种类型。
oprivate成员只能由类作用域中的函数访问。
o基类的private成员在派生类中也是不可见的。
oprivate成员只能通过public成员访问。
⑸构造函数
O构造函数是类的一种特殊成员。
O构造函数用來创建类的对象(实例)。
O构造函数具有特定的名字一与类名相同。
O构造函数可以重载。
四、程序设计题(50%)
本题共50分。
(第1、2题各10分;第3题有3小题,每小题10,分共计
30分。
)
每题评分细则:
1函数原型正确3分(函数返回值类型1分,函数参数类型和个数2分)。
2函数总体结构正确4分(函数的总体结构指主要的循环结构和选择结构)。
3其他3分(实现的细节)。
1.
井include
鼻include
voidmain(intargc,char*argv[])
{
if(argc!
=3)exit
(1);
charcO,cl=*';
intk=l;
ifstreamin(argv[l],ios:
:
inios:
:
binary);
ofstreamout(argv[2],ios:
:
outIios:
:
binary);
while(!
in.eof())
{
in.read(&c0,1);
if(k&&c0==7,&&cl=7')k=0;
if(!
k&&c0==,\x0d,)k=l;
if(k&&cl==7'&&c0!
=7*)out.write(&cl,1);
if(k&&c0!
=7')out.write(&c0,1);
cl=c0;
}
in.close0;
out・close();
2.
inttga(int*a,intn)
{
if(nelsereturn(a[n-l]+tga(a,n-1));
}
3・
①
fraction^fraction:
:
operator+=(fraction&f)
{
fz=fz*f.fm+fm*f.fz;
fm=fm*f・fm;
return*this;
}
2intoperator^(fraction&fl,fraction&f2)
{
if(fl.fz==f2.fz&&fl.fm==f2.fm)
return1;
else
return0;
}
3ostream&operator〈<(ostream&os,fraction&f){
os«f.fz«/*/z,«f.fm«endl:
returnos;
模拟题二
一、判断对错题(10分)(对V,错X)
()1.在类定义中不允许对所定义的数据成员进行初始化。
()2.程序中不能直接调用构造函数,在创建对象时系统自动调用构造函数。
()3.从外部看类的private成员和protected成员没有区别。
()4.可以将派生类对象赋给基类对象,也可以将基类对象赋给派生类对象。
()5.构造函数和析构函数都不能为虚函数。
()6.C卄按列优先存放数组元素。
()7.1/2的值等于0.5。
()8.基类的protected成员经过protected派生后,在派生类中仍为protected成员。
()9.所有运算符都可以重载。
()10.表达式cout<<99的类型为into
二、单项选择题(20分)
1.下列有关类的说法不正确的是()。
A.类是一种用户自定义的数据类型
B.只有类中的成员函数才能存取类中的私有数据
C.在类中,如果不作特别说明,所有的数据均为私有类型
D.在类中,如果不作特别说明,所有的成员函数均为公有类型
2.在C++程序中,对象之间的相互通信通过()o
B•调用成员函数实现
D.函数重载实现析构函数的个数有()o
B.1C.不确定
不是类的成员函数的是(
B.析构函数
D.拷贝构造函数
5.在多基继承的构造函数定义中,儿个基类的构造函数之间用()分隔。
A.
D.
:
B•;C.
6.若类A和类B的定义如下:
classA
inti,j;public:
voidget();
//...
};
classB:
A
intk;
public:
voidmake(int);
//...
};
voidB:
:
make(intj)
k=i*j;
}
则上述定义中,非法的表达式是()。
A.voidget();B.intk;C.voidmake(int)D.k=i*j;
7.己知:
print函数是一个类的常成员函数,它无返回值,下列表示中正确的
是()。
A.constvoidprint()B.voidconstprint()
C.voidprint(const)D.voidprint()const
8.在类外部可以用p.a的形式访问派生类对象p的基类成员a,则a应是()。
A.私有继承的公有成员B.公有继承的私有成员
C.公有继承的保护成员D.公有继承的公有成员
9.下列关于运算符重载的描述中,正确的叙述是()o
A.运算符重载可以改变运算数的个数
B.运算符重载可以改变语法结构
C.运算符重载可以构造新的运算符
D.运算符重载不可以改变优先级和结合性
10.下列带缺省值参数的函数说明中,正确的说明是()o
A.intFun(intx=l,inty=2,intz);
B.intFun(intx,inty=2,intz=3);
C.intFun(intx,inty=2,intz);
D.intFun(intx=l,inty,intz=3);
三、完成程序题:
根据题目要求,完成程序填空。
(20分)
1.本程序在3位正整数中寻找符合下列条件的整数:
它既是完全平方数,乂有两位数字相同,例如144,676等。
用程序找出所有满足上述条件的3位数并输出。
intflag(inta,intb,intc)
{
return!
(()*()*());
}
voidmain()
{
intn,k,a,b,c;
for(k=l;;k++)
辻(n<100)
辻(n>999)
a=n/100;
b=(n/10)%10;
c=n%10;
if(flag(a,b,c))
cout«n«,,=,/<}
2.类CPoint中的成员函数Init的功能是用已知对象给另一对象赋值,请将其补充完整。
classCPoint
{
intx,y;
public:
CPoint(inti,intj){x=i;y=j;}
voidInit(CPoint&k)
{
if()return;//防止自身赋值
=k;
}
};
3.补充以下类,使其能正确运行。
^include
classP
{
private:
intxl,x2;
staticinty;
public:
P(){xl=i;x2=j;}
};
二0;//对静态成员y赋值
voidmain()
{
Pdata[20]:
}
4.在下面程序横线处填上适当的字句,使其输出结果为0,56,56o^include
classbase
{
public:
intfunc(){return0;}
};
classderived:
publicbase
{
public:
inta,b,c;
setValue(intx,inty,intz){a=x;b二y;c二z;}intfunc(){return(a+b)*c;}
voidmain()
{baseb;
derivedd;
cout«b.func()<d・setValue(3,5,7);
cout«d.func()<base&pb二d;cout«pb・func()<}
四、程序分析题:
给出下面程序输出结果。
(15分)1・#include
intf(inti){return--i;}intg(int&i){return--i;}voidmain()
{
inta,b,c,d,e;a=b=c=d=10;a+=f(g(a));b+=g(e=g(b));c+=g(e=f(c));d+二f(f(d));
cout<<,,a=/z«a«endl;cout«,,b=z,«b«endl;cout<〈"c二"《c<〈endl;cout«,,d=z,«d«endl;cout<<,,e=,/«e«endl;
}
输出为:
2.ttinclude
classSample
{
intx;
public:
Sample(inta){x=a;}
frienddoublesquare(Sample&s);
};
doublesquare(Sample&s){returns.x*s・x;}voidmain()
Samplesi(20);
Samples2(30);
cout«19si.square二"<}
输出为:
3.ttinclude
classbasel
{
intx;
public:
basel(inti){x二i;cout«,,baselconstructorcalled!
z,<}〜basel(){cout〈〈"baseldestructorcalled!
/"<}
};
classbase2
{
inty;
public:
base2(inti){y=i;cout«,,base2constructorcalled!
z,</"<}
};
classderivate:
publicbase2,publicbasel
{
public:
derivate(inti,intj):
basel(i),base2(j)
{cout〈〈"derivateconstructorcalled!
"<〈endl;}
〜derivate(){cout<〃〈};
voidmain()
{
derivated(3,4);
}
输出为:
五、程序设计题(35分)
1.(10分)写一递归函数计算F(n)二1+++・•・+
2.(10分)写一基于文件流的程序将文件中大写字母转换成小写字母。
要求:
输入和输出文件名均在命令行中给出。
3.(15分)编写一个程序输入3个学生的英语和计算机成绩,并按总分从高到低排序。
要求设计一个学生类Student,其定义如下:
classStudent
{
intenglish,computer,total;
public:
voidgetscore():
//获取一个学生的成绩
voiddisplay();//显示一个学生的成绩
};
模拟题二参考答案
一、判断对错题(10分,每题1分)(对错X)
1.V2.X3.74.X5.X6.X7.X8.V9.X10.X
二、单项选择题(20分,每题2分)
1.D2.B3.B4.C5.C6.D7.D8.D9.D10.A
三、完成程序题:
根据题目要求,完成程序填空。
(20分)
(前四空(指:
1.①②)每空1分,其余每空2分)
1.①a~ba~cb~c②n=k*k③continue④break
2.①this==&k
(2)*this
3.①inti=0,intj二0(说明:
默认值可为任何合法的整形值)
②intp:
:
y
4.①virtual②void
四、程序分析题:
给出下面程序输出结果。
(15分)
1.(5分,每个1分)
a二17
b=17
c=18
d=18
e=8
2.(4分,每个2分)
si.square=400
s2.square=900
3.(6分,每个1分)
base2constructorcalled!
baselconstructorcalled!
derivateconstruetorcalled!
derivatedestructorcalled!
baseldestruetorcalled!
base2destruetorcalled!
五、程序设计题(35分)
第1、2题每题评分细则:
1函数原型正确3分(函数返回值类型1分,函数参数类型和个数2分)。
2函数总体结构正确4分(函数的总体结构指主要的循环结构和选择结构)。
3其他3分(实现的细节)。
第3题每题评分细则:
1函数原型正确2分(函数返回值类型1分,函数参数类型和个数1分)。
2函数总体结构正确2分(函数的总体结构指主要的循环结构和选择结构)。
3其它2分(实现的细节)。
1.(10分)
doublesum(intn)
if(n~l)return1:
returnsum(n~l)+(double)1/n;
}
2.(10分)
〈参考程序1>:
#include^include
voidmain(intargc,char*argv[])
{
if(argc!
=3){cerr〈〈"wrong!
"〈〈endl;exit
(1);}charc:
ifstreamin(argv[l],ios:
:
in|ios:
:
binary);ofstreamout(argv[2],ios:
:
out|ios:
:
binary);while(!
in.eof())
{
in.read(&c,1);
if(c>二'A'&&c<='Z')c=c+32;
out・write(&c,1);
}
in.close();
out・close();
}
〈参考程序2>:
voidmain(intargc,char*argv[])
{
if(argc!
=3){cerr<z,«endl;exit(l);}charc:
ifstreamin(argv[l]);
ofstreamout(argv[2]);
while(in.get(c))
{
辻(c>=A9&&c<=Z9)c+二32;
out«c;
}
in.close();
out・close();
}
3.(15分)
#includeclassStudent
intenglish,computer,totai:
public:
//获取一个学生成绩
〃显示一个学生成绩
//将若干个学生按总分从高到低排序
voidgetscore():
voiddisplay();
voidsort(Student*);~Student();
};
voidStudent:
:
getscore()
{
cout〈<"输入英语成绩:
";cin»english;
cout«,z输入计算机成绩:
”;cin»computer;
total=english+computer;
}
voidStudent:
:
sort(Student*p){
inttmp,i,j;
for(j=0;j<2;j++)for(i=0;i<2;i++)
if(totaltotal)
{
tmp=total;
total二p->total;p->totai二tmp;
tmp二english;english=p->english;p->english二tmp;
tmp=computer;
computer二p->computer;p->computer=tmp;
}
}
voidStudent:
:
display()
{
cout«zz英语=,,«english«,/计算机二"《computer<〈"总分=,,«total«endl;
}
voidmain()
{
Student*A[3];
for(intj=0;j<3;j++)
A[j]二newStudent;cout<<,/学生,z«j+l<A[j]->getscore();
}
inti;
for(j=0;j<2;j++)
for(i=0;i<2;i++)
A[i]->sort(A[i+lJ);cout<"〈〈endl;for(i=0;i<3;i++)
A[i]->display();
}
模拟题三
一、判断对错题(10分)(对V,错X)
()1•构造函数的访问属性可以是public的,但不可以是protected和private。
()2.析构函数可以被显式调用,也可以被系统自动调用。
()3.可以用派生类对象地址來初始化一个指向基类对象的指针。
()4.C++有三种存储类。
()5.从