1、C+复习C+方向考察题库V2.0版(11级用)第一部分:C+基础(80分左右)说明:复习时请在机器上进行输入,并采用单步调试加深理解。考试时程序基本不变,但其中的数字或字符会有改变,所以需完全弄懂,不能死记硬背。一、 读程序1. #includeclass Sample public: Sample(); Sample(int); Sample(); void display(); protected: int x;Sample:Sample() x=0; coutconstructing normallyn;Sample:Sample(int m) x=m; coutconstructing
2、 with a number: xendl;void Sample:display() coutdisplay a number:xendl;Sample:Sample() coutdestructingn;void main() Sample obj1; /调用无参构造函数 Sample obj2(20); /调用有一个参数的构造函数 obj1.display(); /调用成员函数 obj2.display(); /程序结束前调用析构函数考查内容:构造函数和析构函数constructing normally constructing with a number。20 display a nu
3、mber 0 display a number 20. destructing destructing 2. #include class MyClass public: int number; void set(int i); ; int number=3; void MyClass:set (int i) number=i; void main() MyClass my1; /调用默认构造函数 int number=10; /定义普通局部变量 my1.set(5); /调用成员函数,给成员变量赋值 coutmy1.numberendl; my1.set(number); coutmy1.n
4、umberendl; my1.set(:number); / :numer表示全局变量 coutmy1.number; 考查内容:普通局部变量、成员变量、全局变量的区别5 10 33. #include #include class studentidpublic: studentid(int id=0) value=id; cout”Assigning student id”valueendl; studentid() cout”destructing id”value endl; protected: int value;class studentpublic: student(char
5、*pname=”noname”,int ssid=0):id(ssid) cout ”constructing student”pnameendl; strcpy(name,pname); namesizeof(name)-1=0; student() cout”destructing student”endl; protected: char name20; studentid id;void main() student s(“randy”,9818);assigning student id9818constructing studentrandydestruction studentd
6、estructing id9818考查要点:类的成员变量为对象时,构造函数析构函数的调用顺序4. #include class A public:int x;A(int i)x=i;void fun1(int j) x+=j;cout”fun1:”xendl;void fun2(int j) x+=j;cout”fun2:”xendl;void main() A c1(2),c2(5);void (A:*pfun)(int)=A:fun1; 定义了pfun让该指针指向fun1(c1.*pfun)(5); x=2,j=5.这里输出的就是2+5=7pfun=A:fun2; 让指针指向fun2(c2
7、.*pfun)(10);x=5,j=10.输出的值就是5+10=15输出的答案是7,15考查要点:指向类成员函数的指针的使用5. #include class Rpublic: R(int r1,int r2) R1=r1;R2=r2; void print(); void print() const;private: int R1,R2;void R:print() coutR1:R2endl;void R:print() const coutR1;R2endl;void main() R a(5,4); a.print(); const R b(20,52); b.print();5:4
8、20;52考查要点:普通成员函数与常成员函数的区别6. 没看懂#includeclass Oldpublic:Old(int i=0)o=i;void Print()constcouto,;private:int o;class Newpublic:New()n=0;coutD-Constructor.n; New(int i, int j):d(i) n=j;coutConstructor.n;void Print()const d.Print(); coutnendl;New()coutDestructor.nendl;private: int n;Old d;New m1(8,1);vo
9、id fun(New &m)New m0;m.Print();m0.Print();int main()const New m2(6,2);static New m3(3,3);New *p=new New(5,4);fun(m3);m1.Print();m2.Print();(*p).Print();delete p; 运行结果:Constructor.Constructor.Constructor.Constructor.D-Constructor.3,30,0Destructor.08,16,25,4Destructor.4Destructor.3Destructor.2Destruct
10、or.1考查要点:常对象、静态对象、指向对象的指针的用法7. #includeclass Xfriend class Y;public:void Set(int i) x=i; void Display()cout”x=”x”, ”y=”y endl;private:int x;static int y;class Ypublic:Y(int i, int j);void Display();private:X a;int X:y=10;Y:Y(int i, int j) a.x=i; X:y=j;void Y:Display()cout”x=”a.x”, ”y=”X:yendl;void m
11、ain( )X b;b.Set(15);b.Display();Y c(16,19);c.Display();b.Display();X=15,y=10X=16,y=19X=15,y=19考查内容:友元类,友元类的所有函数都是友元函数8. #include#includeclass assistant;class professorchar pName40;int pAge;long pPhone;public:professor(char *pn, int pa, long pp)strcpy(pName, pn);pAge=pa;pPhone=pp;void display(assista
12、nt &a);class assistantchar aName40;int aAge;long aPhone;public:assistant(char *an, int aa, long ap)strcpy(aName, an);aAge=aa;aPhone=ap;friend void professor:display(assistant &a);void professor:display(assistant &a)cout” ttNamettAgettTelephone” endl;cout”Professor”;cout”t”pName”tt”pAge”tt”pPhoneendl
13、;cout”Assistant”;cout”t”a.aName”tt”a.aAge”tt”a.aPhoneendl;int main( )professor p1(“Joseph”, 52,1335);assistant a1(“Jack”, 25, 8322);p1.display(a1); Name Age TelephoneProfessor Joseph 52 1335Assistant Jack 25 8322考查内容:类成员函数作为友元,引用作参数9. #includeclass B;class Aint i;public: int set(B&);int get()return
14、i;A(int x)i=x;classBint i;public: B(int x)i=x;friend A; ;int A:set(B&b)return i=b.i; int main()A a(1);B b(2);couta.get(),;a.set(b);couta.get()endl;1,2考查内容:友元类10. #includeclass Test1public: Test1()coutCall Common Constructor! endl; /构造函数 Test1(const Test1& a) coutCall Copy Constructor! endl; /拷贝构造函数
15、Test1() coutCall Destructor!endl; /析构函数;Test1 fun1(Test1 a); /对象名作形参,返回对象Test1& fun2(Test1& a); /对象引用作形参,返回形参void main() Test1 a; /调用构造函数(分配空间后初始化) coutCall By Valueendl; fun1(a); /实参和形参都为对象,会调用拷贝构造函数;返回值为对象,也会调用拷贝构造函数 coutCall By Referenceendl; fun2(a); /实参为对象,形参为引用 /程序结束,调用析构函数Test1 fun1(Test1 a)
16、coutReturn objectendl; return a;Test1& fun2(Test1& a) coutReturn Referenceendl; return a;Call Common Construction!Call By ValueCall Copy Constructor!Return objectCall Copy Constructor!Call Destructor!Call Destructor! /这两次调用是因为使用了两次拷贝构造函数Call By ReferenceReturn ReferenceCall Destructor! /这次调用是因为使用的非默
17、认构造函数考察内容:1)形参为对象和返回值为对象时,调用拷贝构造函数2)形参为引用,只是给实参起一个别名11. #include class B int m;public: B(int i = 0) m=i; coutConstructor called.mendl; void Print( ) const coutmendl; /常成员函数 B( ) coutDestructor called.mendl; ;void fun(const B &c) /形参为引用 c.Print( );void main() fun(10); /类型转换:从普通变量到类对象,要调用带一个参数的构造函数Con
18、structor called.1910Constructor called.10考查内容:1)形参为引用2)实参为普通变量,形参为类对象(或类引用),要调用带一个参数的构造函数void main() int &rp=fun(); /定义函数的引用 int *p=&rp; /定义指针,赋值为rp的指针 cout*pendl; /输出rp的值,即fun()的值 / 上一条语句的调用相当于: coutfun()endl; /因为返回值为引用,又相当于 coutrpendl;(此rp为fun()函数中的rp) delete p; /释放p指针指向的空间 *p=10; /给rp赋值 coutrp; /
19、输出rp的值int &fun() int *p=new int; /在内存中分配一个整数变量的大小,地址赋值给p if(!p) /如果分配不成绩 coutError,Memory allocation failture!endl; exit(0); int &rp=*p; /若分配成绩,定义rp是p指向空间的引用 rp=5; / 即 *p=5 return rp; /返回rp,即返回*p考查内容:返回值为引用1. #include class Test;void fun1(Test t);Test fun2();class Testpublic: Test(int n=1) val=n;cou
20、t Con. endl; Test(const Test& t) val=t.val; coutCopy con.endl; Test& operator = (Test& t) /赋值运算符重载 val=t.val; coutAssignment.endl; return *this; /返回本身的引用 private: int val;void main() Test t1(1); Test t2=t1; /调用拷贝构造函数,什么是拷贝构造函数 Test t3; t3=t1; /调用赋值运算符重载 fun1(t2); t3=fun2();void fun1(Test t)Test fun2
21、() Test t; return t;代码有误,没能运算出来考查内容:赋值运算符重载2. #includevoid main() int a=10,20,30,40,*pa=a; int *&pb=pa; /定义指针的引用,即pa的别名为pb pb+; cout*paendl;20考察内容:指针的引用3. 分析以下程序的执行结果#includeclass Sample int x,y; public: Sample()x=y=0; Sample(int i,int j)x=i;y=j; void copy(Sample &s); void setxy(int i,int j)x=i;y=j;
22、void print()coutx=x,y= y endl;void Sample:copy(Sample &s) x=s.x;y=s.y;void func(Sample s1,Sample &s2) s1.setxy(10,20); s2.setxy(30,40); void main( ) Sample p(1,2),q; q.copy(p); func(p,q); p.print(); q.print();运行不起啊考察内容:引用作参数4. #includeclass numberprivate: int val;public: number(int i)val=i; operator
23、 int();number:operator int() /coutoperator int().valendl; return val;void main() number n(15); /调用有一个参数的构造函数 int i=n; /调用operator int() 类对象赋值给整型变量 coutiendl; i+=n; /调用operator int() coutiendl; coutint(n)endl; /调用operator int() 1530155. #includeclass CArraypublic: CArray(int i) Length=i; Buffer=new c
24、harLength; CArray()delete Buffer; int GetLength() return Length; char& operator(int i);private: int Length; char* Buffer;char& CArray:operator(int i) /返回引用 static char ch; if(i=0) return Bufferi; /返回引用,operator调用是Bufferi的引用 else coutnIndex out of range; return ch; /返回引用,operator调用是ch的引用 void main()
25、int cnt; CArray string1(6); char* string2=string; for(cnt=0;cnt8;cnt+) string1cnt=string2cnt; / string2cnt 是buffercnt的引用 coutendl; for(cnt=0;cnt8;cnt+) coutstring1cnt; coutendl; coutstring1.GetLength()endl;Index out of rangeIndex out of rangestringIndex out of rangeIndex out of range6考查内容:返回值为引用6. #include #include class BookStuff public: BookStuff(char *title, char *publisher, char *author); void show_book(void) cout Book: title by author Publisher: publisher endl; ; operator char *(); private: char title64; char author64; char publisher64;BookStuff:Bo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1