哈工大秋《C程序设计》考试题A_第1页
哈工大秋《C程序设计》考试题A_第2页
哈工大秋《C程序设计》考试题A_第3页
免费预览已结束,剩余4页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、哈工大 2005年秋季学期班号C+程序设计试 题(A卷)学号考试时间120分钟满分 70姓名题号-一一-二二-三四五总分实验(20)平时(10)总成绩分数一、填空题。(共20分,每空1分)C+、Java、C#等)具有三大特性,它1. 面向对象的程序设计方法(可用各种语言实现,如为 2.已知m = 4,执行m+;后,表达式的值是_m的值是_执行+m;后,表达式规的值是,m的值是。范 3. 已知m = 3, n = 2,则执行语句 m > n ? m+ : +n , m+, m+n;后,表达式的值是。4. 循环语句while(int i = 0) i-; 执行的循环次数是次;语句遵宀for(

2、int i(0), j(0); i = j = 1; i+, j-);执行的循环次数是 次。守与 5.为了使函数能够返回两个或者更多的值,可将形参设置为下列两种方式,它们分别.场, 是_或_,如果希望实参不被修改,那么需要对形参使用关键字 _限制。旳 6.定义在类体外的函数,如果希望成为内联函数,则必须在类体内的声明中用关键字_标识;运算符的重载必须用关键字_标识。7.在C+中,类的继承方式有三种,需要用关键字和来定义。默认继承方式为。&在C+中,定义友元类或者友元函数必须使用关键字 。二、判断题,正确的请打",错误的请打X。(共10分,每题1分)1. 编译C+源程序的过程中

3、,出现了警告错误,也可以生成可执行文件。()2. C+中,使用#define定义的常量必须给出类型说明,而使用 const给出的常量不用给 出类型说明。()3. 表达式的值的类型只取决于运算符,与操作数无关。()4. break语句可以出现在各种不同循环语句的循环体中,continue语句只能出现在循环体中。()5. 函数没有返回值时,可以不加任何类型的说明符;任何一个函数都可以设置默认参数,但是不允许将一个函数的所有参数设置为默认参数。()6. 指向函数的指针可以作为函数参数;使用数组名作函数参数时,实参数组与形参数组是共享内存 单元的。()7. 结构与联合的差别在于结构中的每个成员都有自己

4、的内存地址,且都是不相同的,而联合中的所有成员是共享内存块的,且内存块的大小与成员中的最大内存块相同。()8. 如果类的一个成员函数只存取该类的静态数据成员,可将该成员函数说明为静态成员函数;友元类的所有成员函数都可以对该类中的私有成员进行存取操作。()9. 类型转换函数是类的成员函数,它是用来进行强制类型转换的,它不能定义为友元函数,也不能定义为静态的成员函数;带有一个参数的构造函数具有类型转换的作用。()10. 在公有继承中,派生类的对象不可以访问基类中的保护成员;在私有继承中,派生类的对象不可以访问基类中的公有成员;在保护继承中,派生类可以访问基类中的保护成员。()三、请分析下列程序,并

5、回答问题1给出下列程序的输出结果,并说明其功能。=4- !: A X if t x a. fi A x aF 4 * F * *f -!>* .#include <iostream.h>void swap( int&, int& );* >.int main():int i = 5, j = 10;:cout<<"ti= "<<i<<"tj= " <<j<<e ndl;+swap(i, j);:cout<<"ti= "<

6、;<i<<"tj= "<<j<<e ndl;:+*:return 0;+E+*fivoid swap(i nt& v1, int& v2)int tmp = v2;v2 = v1;v1 = tmp;答:1、输出结果如下:(共16分)2、给出下列程序的输出结果,并说明其功能。#i nclude <iostream.h>-.int fac(i nt);void mai n()¥int s(0);for(i nt i(1); i <= 5; i+)Is += fac(i); cout<&l

7、t;s<<e ndl;int fac(i nt n):static int b = 1;Ib *= n; return b;答:2、输出结果如下:该程序的主要功能是:该程序的主要功能是:2 / 7int &fun( char cha, int &n, int &c)if(cha >= 'O' && cha <= 9)return n;elsereturn c;F « » S e F ¥ 4- B I F W B 4 -F V 4 F ¥ 1- H B W I- B -I F

8、V 4 *FV49B«-P*!>4a * B答:3、该程序段的主要功能是:如果输入数据为:abc2R&4W39#,请问输出结果是什么?3、阅读下列程序,回答后面的问题。#in elude <iostream.h>int &fun( char cha, int &n, int &c); void mai n()int tn (0), tc(0), m(0);cout<<"E nter characters:" char ch;cin> >ch;while(ch != '#')m

9、 = fun (ch, tn, tc)+; cin> >ch;cout<<t n<<en dl; cout<<tc<<e ndl;4、i#in clude <iostream.h>int mai n()class base public:derived *pd = new derived;virtual int foo( int ival = 1024 ) jbase *pb = pd;cout < "base:foo() - ival: " < ival < en dl;int va

10、l = pb->foo();return ival;cout << "main(): val through base: "<< val << endl; val = pd->foo();/ . !cout << "ma in() : val through derived: "<< val <<ien dl;class derived : public base 1public:virtual int foo( int ival = 2048 ) 答:4、该程序段的输出

11、结果是:cout << "derived:foo() - ival: " << ival << en dl;return ival;11 :/ .;i四、阅读下列文档,并回答提出的问题。(共16分)1、根据下列文档说明,请简要地回答后面给出的几个问题。(6分)The methods for default assignment and initialization are “ memberwiseassignment a”nd “ memberwise initialization, ” respectively. Memberwise

12、assignment consists of copying one object to the other, a mr aetmbe a time, as if assigning each member individually. Memberwise initialization consists of copying one object to the other, a member at a time, as if initializing each member individually. The primary difference between the two is that

13、 memberwise assignment invokes each member' sassignment operator (operator= ), whereas memberwise initialization invokes each member 's copy constructor.Memberwise assignment is performed only by the assignment operator declared in the form:type& type : operator=( const | volatile type&a

14、mp; )Default assignment operators for memberwise assignment cannot be generated if any of the followingconditions exist: A member class has constmembers. A member class has reference members. A member class or its base class has a private assignment operator ( operator=). A base class or member clas

15、s has no assignment operator (operator=).Default copy constructors for memberwise initialization cannot be generated if the class or one of its base classes has a private copy con structor or if any of the follow ing con diti ons exist: A member class hasconst members. A member class has reference m

16、embers. A member class or its base class has a private copy con structor. A base class or member class has no copy con structor.The default assignment operators and copy constructors for a given class are always declared, but they are not defi ned uni ess both of the follow ing con diti ons are met:

17、 The class does not provide a user-defi nedfun cti on for this copy. The program requires that the fun cti on be prese nt. This requireme nt exists if an assignment or initialization is encountered that requires memberwise copying or if the address of the class operator= function is taken.If both of

18、 these conditions are not met, the compiler is not required to generate code for the default assignment operator and copy constructor functions (elimination of such code is an optimization performed by the Microsoft C+ compiler). Specifically, if the class declares a user-defined operator= that take

19、s an argument of type“referencleastos-name,” no default assignment operator is generated. If the class declaresa copy constructor, no default copy constructor is generated. 在C+的类定义中,请简要回答默认的赋值方法与默认的初始化方法之间的差别。 请问在什么情况下,编译器是不会产生默认的赋值方法的? 请问在什么情况下,编译器是不会产生默认的初始化方法的?2、阅读下列说明文档,回答后面的问题。(4分)Virtual base

20、classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple inheritance. Each nonvirtual object contains a copy of the data members defined in the base class. This duplication wastes space and requires you to specify which copy of the base class members you wantwhe

21、n ever you access them. When a base class is specified as a virtual base, it can act as an in direct base more tha n once without duplicati on of its data members. A sin gle copy of its data members is shared by all the base classes that use it as a virtual base. The virtual keyword appears in the b

22、ase lists of the derived classes, not the base class.1为什么要提出虚基类的概念,它有什么好处?答:Q如何定义虚基类呢?答:3、阅读下列说明文档,回答后面的问题。(6分)“ Virtual fun cti ons” are functions that en sure that the correct fun ctio n is called for an object, regardless(the expressi on used to make the function call. Suppose a base class contai

23、ns a function declared as virtual and a derived class defines the same function. The function from the derived class is invoked for objects of the derived class, even if it is called using a pointer or reference to the base class. Functions in derived classes override virtual fun ctio ns in base cla

24、sses only if their type is the same. A function in a derived class cannot differ from a virtual fun cti on in a base class in its retur n type only; the argume nt list must differ as well.When calli ng a function using poin ters or referen ces, the follow ing rules apply:A call to a virtual function

25、 is resolved according to the underlying type of object for which it is called.A call to a non virtual function is resolved accord ing to the type of the poin ter or reference.virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual. The

26、virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defi ned uni ess they are declared using the pure-specifier. The virtual function-call mecha nism ca

27、n be suppressed by explicitly qualifying the function name using the scope-resolution operator (:).Abstract classes cannot be used for: variables or member data、 argume nt types、function retur n typesand types of explicit con vers ions. Ano ther restrict ion is that if the con structor for an abstra

28、ct class calls a pure virtual fun cti on, either directly or in directly, the result is un defi ned. However, con structors and destructors for abstract classes can call other member fun cti ons.根据你的理解,给出下列程序的输出结果。.:#include <iostream.h>class Base :public:Base() cout<<"C on structor of Base !"<<e ndl; Base() cout<<"Destructor of Base !&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论