C++复习.docx

上传人:b****5 文档编号:7373594 上传时间:2023-01-23 格式:DOCX 页数:40 大小:25.40KB
下载 相关 举报
C++复习.docx_第1页
第1页 / 共40页
C++复习.docx_第2页
第2页 / 共40页
C++复习.docx_第3页
第3页 / 共40页
C++复习.docx_第4页
第4页 / 共40页
C++复习.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

C++复习.docx

《C++复习.docx》由会员分享,可在线阅读,更多相关《C++复习.docx(40页珍藏版)》请在冰豆网上搜索。

C++复习.docx

C++复习

C++方向考察题库V2.0版(11级用)

第一部分:

C++基础(80分左右)

说明:

复习时请在机器上进行输入,并采用单步调试加深理解。

考试时程序基本不变,但其中的数字或字符会有改变,所以需完全弄懂,不能死记硬背。

一、读程序

1.#include

classSample

{

public:

Sample();

Sample(int);

~Sample();

voiddisplay();

protected:

intx;

};

Sample:

:

Sample()

{

x=0;

cout<<"constructingnormally\n";

}

Sample:

:

Sample(intm)

{

x=m;

cout<<"constructingwithanumber:

"<

}

voidSample:

:

display()

{

cout<<"displayanumber:

"<

}

Sample:

:

~Sample()

{

cout<<"destructing\n";

}

voidmain()

{

Sampleobj1;//调用无参构造函数

Sampleobj2(20);//调用有一个参数的构造函数

obj1.display();//调用成员函数

obj2.display();

}//程序结束前调用析构函数

考查内容:

构造函数和析构函数

constructingnormallyconstructingwithanumber。

20displayanumber0displayanumber20.destructingdestructing

2.#include

classMyClass{

public:

intnumber;

voidset(inti);

};

intnumber=3;

voidMyClass:

:

set(inti)

{

number=i;

}

voidmain()

{

MyClassmy1;//调用默认构造函数

intnumber=10;//定义普通局部变量

my1.set(5);//调用成员函数,给成员变量赋值

cout<

my1.set(number);

cout<

my1.set(:

:

number);//:

:

numer表示全局变量

cout<

}

考查内容:

普通局部变量、成员变量、全局变量的区别

5103

3.#include

#include

classstudentid{

public:

studentid(intid=0){

value=id;

cout<<”Assigningstudentid”<

}

~studentid(){

cout<<”destructingid”<

}

protected:

intvalue;

};

classstudent

{

public:

student(char*pname=”noname”,intssid=0):

id(ssid)

{

cout<<”constructingstudent”<

strcpy(name,pname);

name[sizeof(name)-1]=’\0’;

}

~student()

{

cout<<”destructingstudent”<

}

protected:

charname[20];

studentidid;

};

voidmain(){

students(“randy”,9818);

}

assigningstudentid9818

constructingstudentrandy

destructionstudent

destructingid9818

考查要点:

类的成员变量为对象时,构造函数析构函数的调用顺序

4.#includeclassA

{public:

intx;

A(inti){x=i;}

voidfun1(intj)

{x+=j;

cout<<”fun1:

”<

}

voidfun2(intj)

{x+=j;

cout<<”fun2:

”<

}

};

voidmain()

{Ac1

(2),c2(5);

void(A:

:

*pfun)(int)=A:

:

fun1;定义了pfun让该指针指向fun1

(c1.*pfun)(5);x=2,j=5.这里输出的就是2+5=7

pfun=A:

:

fun2;让指针指向fun2

(c2.*pfun)(10);x=5,j=10.输出的值就是5+10=15

}

输出的答案是7,15

考查要点:

指向类成员函数的指针的使用

5.#include

classR

{

public:

R(intr1,intr2){R1=r1;R2=r2;}

voidprint();

voidprint()const;

private:

intR1,R2;

};

voidR:

:

print()

{

cout<

"<

}

voidR:

:

print()const

{

cout<

}

voidmain(){

Ra(5,4);

a.print();

constRb(20,52);

b.print();

}

5:

420;52

考查要点:

普通成员函数与常成员函数的区别

6.没看懂

#include

classOld{

public:

Old(inti=0)

{o=i;}

voidPrint()const

{cout<

private:

into;

};

classNew{

public:

New(){

n=0;

cout<<"D-Constructor.\n";

}

New(inti,intj):

d(i){

n=j;

cout<<"Constructor.\n";

}

voidPrint()const{

d.Print();

cout<

}

~New()

{cout<<"Destructor."<

private:

intn;

Oldd;

};

Newm1(8,1);

voidfun(New&m)

{

Newm0;

m.Print();

m0.Print();

}

intmain(){

constNewm2(6,2);

staticNewm3(3,3);

New*p=newNew(5,4);

fun(m3);

m1.Print();

m2.Print();

(*p).Print();

deletep;

}

运行结果:

Constructor.

Constructor.

Constructor.

Constructor.

D-Constructor.

3,3

0,0

Destructor.0

8,1

6,2

5,4

Destructor.4

Destructor.3

Destructor.2

Destructor.1

考查要点:

常对象、静态对象、指向对象的指针的用法

7.#include

classX

{

 friendclassY;

 public:

voidSet(inti)

 {x=i;}

 voidDisplay()

 {cout<<”x=”<

private:

 intx;

 staticinty;

};

classY{

public:

 Y(inti,intj);

 voidDisplay();

private:

 Xa;

};

 

intX:

:

y=10;

Y:

:

Y(inti,intj){a.x=i;X:

:

y=j;}

voidY:

:

Display()

 {cout<<”x=”<

:

y<

 

voidmain( ){

 Xb;

 b.Set(15);

 b.Display();

 Yc(16,19);

 c.Display();

 b.Display();}

X=15,y=10

X=16,y=19

X=15,y=19

考查内容:

友元类,友元类的所有函数都是友元函数

8.#include

#include

classassistant;

classprofessor

{

charpName[40];

intpAge;

longpPhone;

public:

professor(char*pn,intpa,longpp)

{

strcpy(pName,pn);

pAge=pa;

pPhone=pp;

}

voiddisplay(assistant&a);

};

classassistant

{

charaName[40];

intaAge;

longaPhone;

public:

assistant(char*an,intaa,longap)

{

strcpy(aName,an);

aAge=aa;

aPhone=ap;

}

friendvoidprofessor:

:

display(assistant&a);

};

voidprofessor:

:

display(assistant&a){

cout<<”\t\tName\t\tAge\t\tTelephone”<

cout<<”Professor”;

cout<<”\t”<

cout<<”Assistant”;

cout<<”\t”<

}

intmain( )

{

professorp1(“Joseph”,52,1335);

assistanta1(“Jack”,25,8322);

p1.display(a1);

}

NameAgeTelephone

ProfessorJoseph521335

AssistantJack258322

考查内容:

类成员函数作为友元,引用作参数

 

9.#include

classB;

classA

{

inti;

public:

intset(B&);

intget(){returni;}

A(intx){i=x;}

};

classB

{

inti;

public:

   B(intx){i=x;}

friendA;

};

intA:

:

set(B&b){returni=b.i;}

intmain(){

Aa

(1);

Bb

(2);

cout<

a.set(b);

cout<

}

1,2

考查内容:

友元类

10.#include

classTest1{

public:

Test1(){cout<<"CallCommonConstructor!

"<

Test1(constTest1&a)

{cout<<"CallCopyConstructor!

"<

~Test1(){cout<<"CallDestructor!

"<

};

Test1fun1(Test1a);//对象名作形参,返回对象

Test1&fun2(Test1&a);//对象引用作形参,返回形参

voidmain(){

Test1a;//调用构造函数(分配空间后初始化)

cout<<"CallByValue"<

fun1(a);//实参和形参都为对象,会调用拷贝构造函数;返回值为对象,也会调用拷贝构造函数

cout<<"CallByReference"<

fun2(a);//实参为对象,形参为引用

}//程序结束,调用析构函数

Test1fun1(Test1a)

{

cout<<"Returnobject"<

returna;

}

Test1&fun2(Test1&a)

{

cout<<"ReturnReference"<

returna;

}

CallCommonConstruction!

CallByValue

CallCopyConstructor!

Returnobject

CallCopyConstructor!

CallDestructor!

CallDestructor!

//这两次调用是因为使用了两次拷贝构造函数

CallByReference

ReturnReference

CallDestructor!

//这次调用是因为使用的非默认构造函数

考察内容:

1)形参为对象和返回值为对象时,调用拷贝构造函数

2)形参为引用,只是给实参起一个别名

11.#include

classB{

intm;

public:

B(inti=0)

{m=i;cout<<"Constructorcalled."<

voidPrint()const{cout<

~B(){cout<<"Destructorcalled."<

};

voidfun(constB&c)//形参为引用

{

c.Print();

}

voidmain(){

fun(10);//类型转换:

从普通变量到类对象,要调用带一个参数的构造函数

}

Constructorcalled.19

10

Constructorcalled.10

考查内容:

1)形参为引用

2)实参为普通变量,形参为类对象(或类引用),要调用带一个参数的构造函数

voidmain(){

int&rp=fun();//定义函数的引用

int*p=&rp;//定义指针,赋值为rp的指针

cout<<*p<

//上一条语句的调用相当于:

cout<

//因为返回值为引用,又相当于cout<

deletep;//释放p指针指向的空间

*p=10;//给rp赋值

cout<

}

int&fun(){

int*p=newint;//在内存中分配一个整数变量的大小,地址赋值给p

if(!

p)//如果分配不成绩

{

cout<<"Error,Memoryallocationfailture!

"<

exit(0);

}

int&rp=*p;//若分配成绩,定义rp是p指向空间的引用

rp=5;//即*p=5

returnrp;//返回rp,即返回*p

}

考查内容:

返回值为引用

1.#include

classTest;

voidfun1(Testt);

Testfun2();

classTest{

public:

Test(intn=1){val=n;

cout<<"Con."<

Test(constTest&t){val=t.val;cout<<"Copycon."<

Test&operator=(Test&t)//赋值运算符重载

{

val=t.val;

cout<<"Assignment."<

return*this;//返回本身的引用

}

private:

intval;

};

voidmain(){

Testt1

(1);

Testt2=t1;//调用拷贝构造函数,什么是拷贝构造函数

Testt3;

t3=t1;//调用赋值运算符重载

fun1(t2);

t3=fun2();

}

voidfun1(Testt){}

Testfun2(){

Testt;

returnt;

}

代码有误,没能运算出来

考查内容:

赋值运算符重载

2.

#include

voidmain(){

inta[]={10,20,30,40},*pa=a;

int*&pb=pa;//定义指针的引用,即pa的别名为pb

pb++;

cout<<*pa<

}

20

考察内容:

指针的引用

3.分析以下程序的执行结果

#include

classSample{

intx,y;

public:

Sample(){x=y=0;}

Sample(inti,intj){x=i;y=j;}

voidcopy(Sample&s);

voidsetxy(inti,intj){x=i;y=j;}

voidprint(){cout<<"x="<

};

voidSample:

:

copy(Sample&s)

{

x=s.x;y=s.y;

}

voidfunc(Samples1,Sample&s2)

{

s1.setxy(10,20);

s2.setxy(30,40);

}

voidmain()

{

Samplep(1,2),q;

q.copy(p);

func(p,q);

p.print();

q.print();

}

运行不起啊

考察内容:

引用作参数

4.#include

classnumber{

private:

intval;

public:

number(inti){val=i;}

operatorint();

};

number:

:

operatorint()

{

//cout<<"operatorint()..."<

returnval;

}

voidmain()

{

numbern(15);//调用有一个参数的构造函数

inti=n;//调用operatorint()类对象赋值给整型变量

cout<

i+=n;//调用operatorint()

cout<

cout<

}

15

30

15

5.#include

classCArray

{

public:

CArray(inti)

{

Length=i;

Buffer=newchar[Length];

}

~CArray(){delete[]Buffer;}

intGetLength(){returnLength;}

char&operator[](inti);

private:

intLength;

char*Buffer;

};

char&CArray:

:

operator[](inti)//返回引用

{

staticcharch;

if(i=0)

returnBuffer[i];//返回引用,operator[]调用是Buffer[i]的引用

else

{

cout<<"\nIndexoutofrange";

returnch;//返回引用,operator[]调用是ch的引用

}

}

voidmain()

{

intcnt;

CArraystring1(6);

char*string2="string";

for(cnt=0;cnt<8;cnt++)

string1[cnt]=string2[cnt];//string2[cnt]是buffer[cnt]的引用

cout<

for(cnt=0;cnt<8;cnt++)

cout<

cout<

cout<

}

Indexoutofrange

Indexoutofrange

string

Indexoutofrange

Indexoutofrange

6

考查内容:

返回值为引用

6.#include

#include

classBookStuff{

public:

BookStuff(char*title,char*publisher,char*author);

voidshow_book(void)

{

cout<<"Book:

"<

author<<"Publisher:

"<

};

operatorchar*();

private:

chartitle[64];

charauthor[64];

charpublisher[64];

};

BookStuff:

:

Bo

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

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1