您的当前位置:首页正文

C++复习题 (3)

来源:九壹网
一) 选择题

1对定义重载函数的要求中,错误的是D A要求参数的个数不同

B要求参数中至少有一个不同

C要求参数个数相同时,参数类型不同 D要求函数的返回值不同

3以下那个函数函数表示虚函数c A virtual void vf(int) B virtual void vf(int)=0 Cvirtual void vf( )=0 D virtual void vf(int){}

4拷贝构造函数的形参是C A某个对象名

B某个对象的成员名 C某个对象的引用名 D某个对象的指针名

5有关虚函数和抽象类的描述中,B是错的

A纯虚函数是一种特殊的虚函数,它没有具体的定义

B一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类 C抽象类是指具有纯虚函数的类

D抽象类只能作为基类来使用,其纯虚函数

6已定义的float x,则以下对指针变量p进行定义且赋初值的语句中正确的是D A float *p=1024; B int *p=x; C float p=&x; D float *p=&x;

7假定要对类型AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句正确的是B A AB operator +( AB&a, AB&b) B AB operator +( AB&a) C operator ( AB&a) D AB& operator +()

8从下对范本的说明,正确的是C A template

B template

C template< class T1,calss T2>

D template< class T1;calss T2>(分号错)

9派生类对象对它的基类成员中(公有继承的公有成员)是可以放回的 A公有继承的保护成员 B公有继承的私有成员 C公有继承的公有成员 D私有继承的私有成员

10派生类的构造函数的成员初始化列表中不能包含 A基类的构造函数

B基类的子对象的初始化 C派生类的子对象的初始化

D派生类中一般数据成员的初始化

11假定AB为一个类,则执行“AB a (4),b[3],*p”语句时,自动调用该类构造函数的次数为4 (二)填空为题

1编译时多态性使用通过(重载)获得。

2对一个类的数据成员初始化语句通过构造函数中的(赋值语句),也可以通过(初始化列表)

实现。

3对象的三大基本特征:多态性,继承性和(封装性)。 4用(virtual)声明函数为虚函数。

5一个const对象只能访问(常)成员函数。

6字符串“ hello,world”在内存中释放时,占用(13)个字节。 7语句int(*p)[n]的含义是定义了一个(数组指针)。

8假定一个类AB执行语句AB X,则将自动调用该类的(默认构造函数)。 9c++提供了一个测试某种数据所占内存的运算符(size of)。

10所有函数模板定义都是以关键词template用以关键的(类型参数表)。 (三)读程序题

1-----------------------------------------

#include void main() {int k=1,j=2,n=3; int s=(j+=k,n+=j);

cout<<\"s=\"<答案: s=6j=3 54

2--------------------------------------- #include long f(int x) {

int y=2;

static int z=1;

x+=y++,z++; return x; }

void main() {int x=2,k;

for (int i=0;i<2;i++) k=f(x);

cout<<\"k=\"<答案: k=4

3---------------------------------------- #include class test {

private:

int num; public: test();

int TEST() {return num;} ~test(); };

test::test() {

num=0;

cout<<\"test constructor\"<test::~test() {

cout<<\"test destructor\"<void main() {

test x[3]; }

答案:

test constructor test constructor test constructor test destructor test destructor test destructor

4-----------------------------------------

#include class base {

public: base()

{cout<<\"构造函数\"<{cout<<\"构造函数(int)\"<{cout<<\"析构函数\"<class derived:public base {

public:

derived()

{cout<<\"derived 构造函数\"<derived(int m):base(m)

{cout<<\"derived 构造函数(int)\"<~derived()

{cout<<\"derived 析构函数\"<void main() {

derived obj1; derived obj2(10); }

构造函数

derived 构造函数 构造函数(int)

derived 构造函数(int) derived 析构函数 析构函数

derived 析构函数 析构函数

5------------------------------------------------- #include using namespace std;

class con {

char id; public:

char getid()const{return id;} con():id('A'){cout<<1;}

con(char id):id(id){cout<<3;}

con(con &c):id(c.getid()){cout<<5;} };

void show(con c) {cout<con c1; show(c1); con c2('B'); show(c2); }

答案: 15A35B

6--------------------------------------------- #include class Test {

static int val; int a; public:

static int func() {return val++;} void sfunc(Test &r) {r.a=25;cout<int Test::val=20; void main() {

cout<cout<答案: 20 21 25

7-------------------------------------------

#include void main() {

int n1,n2; n2=1298; while(n2!=0) {

n1=n2%10; n2=n2/10;

cout<答案: 8 9 2 1 8------------------------------------- #include class sample {

char c1,c2; public:

sample(char a) {c2=(c1=a)-32;} void disp() {

cout<void main() {

sample a('a'),b('b'); a.disp(); b.disp(); }

答案: a转换为A b转换为B

9------------------------------------- #include int count=0; class point {

int x,y; public: point()

{x=1;y=1;count++;} ~point() {

count--; }

friend void disp(); };

void disp() {

cout<<\"There are \"<void main() {

point a; disp();

{point b[5];disp();} disp(); }

答案:

There are 1 points There are 6 points There are 1 points (四)写程序题

1设计一个类string要求string类包括私有成员函数,char *str,在构造函数中初始化这个数据。写出它的两个构造函数(有参和拷贝),析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分) #include using namespace std; class String {

private:

char *str; int len; public:

void showstr() {

cout<<\"String:\"<String() {

len=0;

str=NULL; }

String(char *p)

{

len=strlen(p); if(len!=0)

{str=new char[len+1]; strcpy(str,p); }

cout<<\"构造函数\"<~String() {

if(str!=NULL) {delete[]str; str=NULL; len=0;}

cout<<\"析构函数\"<String(const String &r) { len=r.len; if(len!=0) { str=new char[len+1]; strcpy(str,r.str); }

cout<<\"拷贝构造函数\"<void main() {

String s1(\"nihao\"); s1.showstr(); String s2=s1; s2.showstr(); }

2编写 一个求两个正整数m和n最大公约数的程序并测试程序。 #include void main() {

int m,n,t,r; cin>>m>>n; while (n!=0) { r=m%n; m=n; n=r;

}

cout<<\"最大公约数是:\"<3将fibnacial 序列1,1,2,3,5,„„的前20项存放在a[n]中,并显示出相应的元素

#include #include void main() {

int i,a[20]={1,1}; for (i=2;i<20;i++) a[i]=a[i-1]+a[i-2]; for (i=0;i<20;i++) {if(i%5==0) cout<cout<cout<4设计一个类person要求person类包括私有成员函数,char *Name,在构造函数中初始化这个数据。写出它的两个构造函数(一般参数和对象的引用),析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分) #include using namespace std; class Person {

private:

char *Name; int len; public:

void showName() {

cout<<\"Person:\"<Person () {

len=0;

Name=NULL; }

Person (char *x) {

len=strlen(x); if(len!=0)

{Name=new char[len+1];

} {

strcpy(Name,x);

cout<<\"构造函数\"<~Person () if(Name!=NULL) {delete[]Name; Name=NULL; len=0;

}

cout<<\"析构函数\"<Person (Person &xm) {

len=xm.len; if(len!=0) {

Name=new char[len+1]; strcpy(Name,xm.Name); }

cout<<\"拷贝构造函数\"<void main() {

Person s1(\"Liming\"); s1.showName (); Person s2=s1; s2.showName(); }

5输出100~200之间能被3整除,但不能被7整除的数及其个数。 #include #include void main() {

int i,j,count;int a[100]; count=0;

for(i=100,j=0;i<=200 && j<100;i++) if (i%3==0 && i%7!=0) {

a[j]=i; j++;

count++;

}

for (i=0;iif(i%6==0)cout<cout<cout<<”个数为:”<6完成下列函数,实现将a[n]中的元素按逆序重新存放。 1---------------------------- #include #include #define N 5

void swap(int a[],int n) {

int i,t;

for (i=0;i{t=a[i];a[i]=a[n-i-1];a[n-i-1]=t;} for (i=0;ivoid main() {

int a[N],i;

for(i=0;i>a[i]; swap(a,N); cout<2-------------------------- #include #include #define n 5 void main() {

int a[n],i,t;

for(i=0;i>a[i];

for (i=0;i{t=a[i];a[i]=a[n-i-1];a[n-i-1]=t;} for (i=0;i}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top