1、【
填空题
】
有如下程序段:
int x=1,Y=2,z=3;
x=x^z;
y=y^z;
z=x^y;
cout<<x<<y<<z;
执行这个程序段的输出是
。
[每空2分]
答案:
[""]
2、【
填空题
】
有如下程序段:
fer(int i=1; i<=50;i++){
if(i%3 != 0)
continue;
else
if(i%5!=0)
continue;
tout<<i<<",";
}
执行这个程序段的输出是
。
[每空2分]
答案:
[""]
3、【
填空题
】
有如下程序段:
Char c [20]="examination";
c[4]=0;
cout<<c<<endl;
执行这个程序段的输出是
。
[每空2分]
答案:
[""]
4、【
填空题
】
下面的函数利用递归实现了求1+2+3+……+n的功能:
int sum(int n){
if(n==0)
return 0;
else
return n+sum(n-1);
}
在执行sum(10)的过程中,递归调用sum函数的次数是
。
[每空2分]
答案:
[""]
5、【
填空题
】
非成员函数应该声明为类
函数才能访问该类的私有成员。
[每空2分]
答案:
[""]
6、【
填空题
】
有如下程序:
#include<iostream>
using namespace std;
class Monitor{
public:
Monitor(char t):type (t){}
void print( ) const
{cout<<"The type of monitor is"<<type<<endl;}
private:
char type;
};
class Computer{
public:
Computer(int i,char c):
{}
void Print()const
{cout<<"The computer is"<<id<<endl;mon.Print();}
private:
int id;
Monitor mon;
};
const Computer myComputer(101,'B');
myComputer .Print();
return 0;
}
请将程序补充完整,使程序在运行时输出:
The computer is 101
'The type of monitor i.s 8
[每空2分]
答案:
[""]
7、【
填空题
】
有如下程序:
#include <iostream>
using namespace std
class Animal{
public:
virtual char* getType()const { return "Animal";}
virtual char* getVoice()const { return "Voice";}
};
Class Dog : public Animal {
public:
char* getType ( ) const {return "Dog";}
char* getVoice ( ) const {return "Woof"}
};
void type(Animal& a) {cout<<a.getType();}
void speak(Animal a) {cout<<a.getVoice();}
int main( ) {
Dog d; type (d);cout<<" speak";speak(d);cout<<endi;
return 0;
}
运行时的输出结果是
。
[每空2分]
答案:
[""]
8、【
填空题
】
补充完整下面的类定义:
const double PI=3 .14;
class Circle{ //圆形物体的抽象基类
protected:
double r; //半径
public:
Circle(double radius=0):r(radius){}
;//计算圆形物体表面积的纯虚函数声明
};
class Cylinder:public Circle { //圆柱体类
double h; //高度
public:
Cylindr(double radius=0, doubli height=0):
Circle(radius),h (height){}
Virtual double Area() { //计算圆柱体的表面积
return 2*PI*r*(r+h);
}
};
[每空2分]
答案:
[""]
9、【
填空题
】
补充完整下面的类定义:
class XCH{
char* a;
public:
XCH(char* as) { //构造函数
a=new char[strlen(aa)+1];
strcpy(a,aa);
}
XCH& operator=(const XCH& x) //重载赋值函数
Delele []a;
A=new char[strlen(x.a)+l];
strcpy(a, x .a)
;
}
~XCH() {delete []a;}
};
[每空2分]
答案:
[""]
10、【
填空题
】
补充完整下面的模板定义:
template<class Type> //Type为类型参数
class Xtwo{ //由两个Type类型的数据成员构成的模板类
Type a;
Type b;
public:
Xtwe(Type aa=0, Type bb=0):a(aa),b(bb){}
int Ccmpare (){//比较a和b的大小
if (a>b)returm 1;
else if(a==b) return 0;
else return -1;
}
Type Snm() {return a+b;} //返回a和b之和
Type Mult(); //函数声明,返回a和b之乘积
};
Template<class Type>
::Mult(){return a*b;} //Mult 函数的类外定义
[每空2分]
答案:
[""]