四川大学C++面向对象程序设计模拟试题.docx

上传人:b****7 文档编号:15744907 上传时间:2023-07-07 格式:DOCX 页数:16 大小:18.09KB
下载 相关 举报
四川大学C++面向对象程序设计模拟试题.docx_第1页
第1页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第2页
第2页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第3页
第3页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第4页
第4页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第5页
第5页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第6页
第6页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第7页
第7页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第8页
第8页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第9页
第9页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第10页
第10页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第11页
第11页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第12页
第12页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第13页
第13页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第14页
第14页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第15页
第15页 / 共16页
四川大学C++面向对象程序设计模拟试题.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

四川大学C++面向对象程序设计模拟试题.docx

《四川大学C++面向对象程序设计模拟试题.docx》由会员分享,可在线阅读,更多相关《四川大学C++面向对象程序设计模拟试题.docx(16页珍藏版)》请在冰点文库上搜索。

四川大学C++面向对象程序设计模拟试题.docx

四川大学C++面向对象程序设计模拟试题

C++面向对象程序设计模拟试题一

一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个备选项中,只有一个是符合题目要求的,请将其代码填写在题后的括号。

错选、多选或未选均无分。

1.说明虚函数的关键字是(B)。

A)inlineB)virtualC)defineD)static

2.在标准C++中,每个程序中都必须包含有这样一个函数,该函数的函数名为(A)。

A)mainB)MAINC)nameD)function

3.cout是某个类的标准对象的引用,该类是(A)。

A)ostreamB)istreamC)stdoutD)stdin

4.如果在类外的非类的成员函数中有函数调用CPoint:

:

func();则函数func()是类CPoint的(C)。

A)私有静态成员函数B)公有非静态成员函数

C)公有静态成员函数B)友元函数

5.如果class类中的所有成员在定义时都没有使用关键字public、private或protected,则所有成员缺省定义为(C)。

A)publicB)protectedC)privateD)static

6.一个类的所有对象共享的是(D)。

A)私有数据成员B)公有数据成员

C)保护数据成员D)静态数据成员

7.动态联编所支持的多态性称为(D)。

A)虚函数B)继承

C)编译时多态性D)运行时多态性

8.定义类模板时要使用关键字(D)。

A)constB)newC)deleteD)template

9.对虚基类的定义(A)。

A)不需要使用虚函数B)必须使用虚函数

C)必须使用privateD)必须使用public

10.类类型转换函数(A)。

A)不能带有参数B)只能带一个参数

C)只能带2个参数D)只能带3个参数

二、填空题(本大题共5小题,每小题2分,共10分)不写解答过程,将正确的答案写在每小题的空格。

错填或不填均无分。

1.在用C++进行程序设计时,最好用(new)代替malloc。

2.函数模板中紧随template之后尖括号的类型参数都要寇以保留字(class或typename)。

3.编译时多态性可以用(重载)函数实现。

4.拷贝构造函数用它所在类的(对象)作为参数。

5.用关键字static修饰的类的成员称为(静态)成员。

三、程序分析题(本大题共6小题,每小题5分,共30分)给出下面各程序的输出结果。

1.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

classArray

{

public:

Array(inta[],intiSize):

elem(a),size(iSize)

{

}

intGetSize()

{

returnsize;

}

int&operator[](inti)

{

returnelem[i-1];

}

private:

int*elem;

intsize;

};

intmain()

{

ints[]={3,7,2,1,5};

Arrayar(s,5);

ar[1]=9;

for(inti=1;i<=5;i++)

{

cout<

}

cout<

return0;

}

上面程序的输出结果为:

97215

 

2.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

template

voidPrint(Typea[],intn)

{

for(inti=0;i

{

cout<

}

}

intmain()

{

inta[]={5,6,8};

doubleb[]={6.8,9.6};

Print(a,sizeof(a)/sizeof(int));

Print(b,2);

cout<

return0;

}

上面程序的输出结果为:

5686.89.6

 

3.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

classTest

{

public:

Test(intn):

num(n)

{

count++;

}

~Test()

{

}

voidPrint()const;

staticintGetCount()

{

returncount;

}

private:

intnum;

staticintcount;

};

intTest:

:

count=0;

voidTest:

:

Print()const

{

cout<num<<""<count<<"";

}

intmain()

{

TestoTest1(6);

oTest1.Print();

TestoTest2(8);

oTest2.Print();

cout<

:

GetCount();

cout<

return0;

}

上面程序的输出结果为:

61822

 

4.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

classTest

{

public:

Test(inta=0,intb=0,intc=0):

x(a),y(b),z(c){}

voidPrint()

{

cout<

cout<

}

voidPrint()const

{

cout<

}

private:

intx,y;

constintz;

};

intmain()

{

Testobj1;

obj1.Print();

Testobj2(1,6,8);

obj2.Print();

constTestobj3(6,0,18);

obj3.Print();

cout<

return0;

}

上面程序的输出结果为:

001618

 

5.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

classMyClass

{

private:

staticintn;

public:

MyClass(){n+=1;}

~MyClass(){n-=1;}

staticintGetNum(){returnn;}

};

intMyClass:

:

n=0;

intmain()

{

cout<

:

GetNum()<

MyClassobj;

cout<

:

GetNum()<

MyClass*p=newMyClass;

cout<

:

GetNum()<

deletep;

cout<

:

GetNum()<

cout<<"end"<

return0;

}

上面程序的输出结果为:

0121end

 

6.阅读下面程序,写出输出结果。

#include

usingnamespacestd;

classA

{

private:

inta;

public:

A(){cout<<"无参构造函数"<

A(inta){cout<<"含参构造函数a="<

A(constA©):

a(copy.a){cout<<"复制构造函数"<

~A(){cout<<"析构函数"<

};

intmain()

{

Aobj1,obj2

(1),obj3(obj2);

return0;

}

上面程序的输出结果为:

无参构造函数含参构造函数a=1复制构造函数析构函数析构函数析构函数

 

四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。

1.将如下程序补充完整。

#include

usingnamespacestd;

classTest

{

private:

intnum;

public:

Test(intnum=0){[1]this->num或Integer:

:

num=num;}//初始化数据成员num为形参num

intGetNum()const{returnnum;}

};

intmain()

{

Testobj;

cout<

return0;

}

2.将如下程序补充完整。

#include

usingnamespacestd;

classA

{

private:

inta;

public:

A(intm):

a(m){}

voidShow()const{cout<

};

classB:

A

{

private:

intb;

public:

B(intm,intn=0):

[2]A(m),b(n){}//初始化数据成员b的值为n

voidShow()const

{

A:

:

Show();

cout<

}

};

intmain()

{

Bobj(8);

obj.Show();

return0;

}

3.下列程序的输出结果为:

0

1

0

试将程序补充完整。

#include

usingnamespacestd;

classPoint

{

private:

intx,y;

staticintcount;

public:

Point(intm=0,intn=0):

x(m),y(n){count++;}

~Point(){count--;}

intGetX()const{returnx;}

intGetY()const{returny;}

staticvoidShowCount(){cout<

};

[3]intPoint:

:

count=0;//静态数据成员的初始化为0

intmain()

{

Point:

:

ShowCount();

Point*p=newPoint;

Point:

:

ShowCount();

deletep;

Point:

:

ShowCount();

return0;

}

4.将如下程序补充完整。

#include

usingnamespacestd;

classComplex

{

private:

doublerealPart;

doubleimagePart;

public:

Complex(doublereal=0,doubleimage=0):

realPart(real),imagePart(image){}

doubleGetRealPart()const{returnrealPart;}

doubleGetImagePart()const{returnimagePart;}

[4]Complexoperator+(constComplex&a)const//重载加法运算符+

{

Complexb;

b.realPart=this->realPart+a.realPart;

b.imagePart=this->imagePart+a.imagePart;

returnb;

}

};

intmain()

{

Complexa(1,2),b(2,6),c;

c=a+b;

cout<<"a="<

cout<<"b="<

cout<<"c="<

return0;

}

五、编程题(本大题共2小题,第1小题12分,第2小题16分,共28分)

1.编写一个函数模板,用于求参数的绝对值,并编写测试程序进行测试。

函数模板声明如下:

template

TypeAbs(Typea);

 

1.参考程序:

#include

usingnamespacestd;

template

TypeAbs(Typea)

{

if(a>=0)returna;

elsereturn-a;

}

intmain()

{

cout<

cout<

cout<

cout<

return0;

}

 

2.定义一个复数类Complex,定义带有2个参数(其中一个为缺省参数)的构造函数,显示复数值的函数Show(),重载“+”运算符(用成员函数实现),并编写测试程序进行测试。

参考程序:

#include

usingnamespacestd;

classComplex

{

public:

Complex(doubler,doublei=0)

{

real=r;

image=i;

}

voidShow()

{

cout<

if(image>0)cout<<"+"<

elseif(image<0)cout<<"-"<<-image<<"i"<

elsecout<

}

Complexoperator+(constComplex&obj)

{

Complextemp(real+obj.real,image+obj.image);

returntemp;

}

private:

doublereal,image;

};

intmain()

{

Complexz1(2,6),z2(3,8),z3(0);

z1.Show();

z2.Show();

z3.Show();

z3=z1+z2;

z3.Show();

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2