最新c++题库及答案(3)

2019-01-10 11:41

class Magic {double x; public:

Magic(double d=0.00):x(fabs(d)) {}

Magic operator+(______) {

return Magic(sqrt(x*x+c.x*c.x)); }

_______operator<<(ostream & stream,Magic & c) { stream<

void main() {Magic ma;

cout<

答案:operator+(Magic&c),friend ostream&operator

[解析]对加法进行重载,operator+(Magic & c),是对插入符进行重载,要访问成员所以定义

为友元函数,friend ostream & operator。

4. 下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。 #include _________; _________; void main() {double rad; cout<<\cin>>rad;

double l=2.0*pi*rad; double s=pi*rad*rad;

cout<<\\n The long is:\cout<<\:\

答案:using namespace std,#define pi 3.14159

[解析]进行输入或输出要引入iostream, 所以using namespace std;从标点看没有分号,所以

使用宏定义,#define pi 3.14159。

5. 程序实现大写字母转换成小写字母。 #include void main() {char a; _______;

cin>>a; if(_______) a=a+i;

cout<

答案:int i=32;,a>=A && a<=Z

[解析]大写字母变小写字母相差32,需要对i声明并初始化。大写字母变小写字母。要判断字

符是大写字母。

五、程序分析题(本大题共4小题,每小题5分,共20分) 1. 给出下面程序输出结果。 #include class a {public:

virtual void print()

{cout<< \};

class b:public a {};

class c:public b {public:

void print(){cout<<\};

void show(a *p) {(*p).print(); }

void main() {a a; b b; c c;

show(&a); show(&b); show(&c); }

答案:a prog... a prog... c prog...

[解析]考查多态性的。a类对象调用本身的虚函数,b类因为没有覆写print,所以仍然调用基

类的虚函数。而c类重新定义print虚函数,所以调用c类的print。 2. 给出下面程序输出结果。 #include #include #include

bool fun(long n); void main()

{long a=10,b=30,l=0; if(a%2==0) a++;

for(long m=a;m<=b;m+=2) if(fun(m))

{if(l++==0) cout <

cout <

bool fun(long n)

{int sqrtm=(int)sqrt(n); for(int i=2;i<=sqrtm;i++) if(n%i==0) return false; return true; }

答案:11 13 17 19 23 29

[解析]循环体用来判断n是否是质数的函数,在main函数判断10~30之间质数。 3. 给出下面程序输出结果。 #include class Test {int x,y; public:

Test(int i,int j=0) {x=i;y=j;}

int get(int i,int j) {return i+j;} };

void main()

{Test t1(2),t2(4,6);

int (Test::*p)(int,int=10); p=Test::get;

cout<<(t1.*p)(5)<

cout<<(p1->*p)(7,20)<

答案:15 27

[解析]指向类成员函数的指针的使用,*p指向Test类中有两个参数的函数的一个指针。 P=Test::get.这样p就和get发生了联系。(t1.*p)(5)等价于调用一个参数的get函数。 4. #include #include #include

class student

{char name[8]; int deg;

char level[7];

friend class process; // 说明友元类 public:

student(char na[],int d) { strcpy(name,na); deg=d; } };

class process { public:

void trans(student &s) {int i=s.deg/10; switch(i) {case 9:

strcpy(s.level, \优\case 8:

strcpy(s.level,\良\case 7:

strcpy(s.level,\中\case 6:

strcpy(s.level,\及格\default:

strcpy(s.level,\不及格\} }

void show(student &s)

{cout<

void main()

{ student st[]={student(\张三\李四\王五 \孙六\process p;

cout<<\结果:\姓名\成绩\等级\for(int i=0;i<4;i++) { p.trans(st[i]); p.show(st[i]);} }

答案:结果:姓名成绩等级 张三78中 李四92优 王五62及格

孙六88良

六、程序设计题(本大题共1小题,共10分)

1. 已定义一个Shape抽象类,在此基础上派生出矩形Rectangle和圆形Circle类,二者都有 GetPerim()函数计算对象的周长,并编写测试main()函数。 class Shape {public: Shape(){} ~Shape(){}

virtual float GetPerim()=0; }

答案:class Rectangle:public Shape {public:

Rectangle(float i,float j):L(i),W(j){} ~Rectangle(){}

float GetPerim(){return 2*(L+W);} private: float L,W; };

class Circle:public Shape {public:

Circle(float r):R(r){}

float GetPerim(){return 3.14*2*R;} private: float R; };

void main() {Shape * sp;

sp=new Circle(10);

cout<GetPerim ()<

cout<GetPerim()<


最新c++题库及答案(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:第四章 酸碱滴定法(新)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: