版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C/C+ Programmi ng in terview questi ons andan swersBy Satish Shetty, July 14th, 2004What is en capsulatio n?Containing and hiding information about an object, such as internal data structures and code. En capsulati on isolates (使隔离) the in ter nal complexity of an object's operati on from the re
2、st of the applicati on. For example, a clie nt comp onent ask ing for net reve nue(收益) from a bus in ess objectn eed not know the data's origi n.What is in herita nee?Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and meth
3、od implementations of the base class and extends it by overriding methods and adding additional properties and methods.What is Polymorphism? ?Polymorphism allows a client to treat different objects in the same way even if they were created from differe nt classes and exhibit (展现)differe nt behaviors
4、.You can use impleme ntati on(实现) in herita nce to achieve polymorphism in lan guages such as C+and Java.Base class objects poin ter can in voke(调用)methods in derived class objects.You can also achieve polymorphism in C+ by fun cti on overload ing and operator overload ing.What is con structor or ct
5、or?Constructor creates an object and initializes it. It also creates vtable 变量列表? for virtual functions. It is differe nt from other methods in a class.What is destructor?Destructor usually deletes any extra resources allocated by the object.What is default con structor?Con structor with no argume n
6、ts or all the argume nts has default values.What is copy con structor?Constructor which initializes the it's object member variables ( by shallow copying ) with another object of the same class. If you don't implement one in your class then compiler implements one for you. for example:Boo Ob
7、jl (lO) ; / cal ling Boo con structorBoo Obj2 (Objl ) ; / calling boo copy constructorBoo Obj2 = Obj 1;/ calling boo copy constructorWhen are copy constructors called?Copy constructors are called in following cases:a) when a function returns an object of that class by valueb) when the object of that
8、 class is passed by value as an argument to a functionc) when you construct an object based on another object of the same classd) When compiler generates a temporary objectWhat is assignment operator?Default assignment operator handles assigning one object to another of the same class. Member to mem
9、ber copy (shallow copy)What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one.? default ctor copy ctor assignment operator default destructor address operatorWhat is conversion constructor?constructor with a
10、single argument makes that constructor as conversion ctor and it can be used for type conversion.for example: class Boo(public:Boo( int i);Boo BooObject = 10 ; / assigning int 10 Boo objectWhat is conversion operator?class can have a public method for specific data type conversions. for example:clas
11、s Boodouble value;public:Boo(int i)operator double()return value;Boo BooObject;double i = BooObject; / assigning object to variable i of type double, now conversion operator gets called to assign the value.What is diff between malloc()/free() and new/delete?malloc allocates memory for object in heap
12、 but doesn't invoke object's constructor to initiallize the object.new allocates memory and also invokes constructor to initialize the object. malloc() and free() do not support object semanticsDoes not construct and destruct objects string * ptr = (string *)(malloc (sizeof(string) Are not s
13、afeDoes not calculate the size of the objects that it constructReturns a pointer to voidint *p = (int *) (malloc(sizeof(int);int *p = new int;Are not extensible new and delete can be overloaded in a class "delete" first calls the object's termination routine (i.e. its destructor) and t
14、hen releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty :-Int_t *my_ints = new Int_t10;delete my_ints;what is the diff between "new" andnoperat
15、or new" ?"operator new" works like malloc.What is difference between template and macro?There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.If macro parameter has a post-incremented varia
16、ble ( like c+ ), the increment is performed two times. Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.for example:Macro:#define min(i, j
17、) (i < j ? i: j) template: template<class T> Tmin (T i,Tj)(return i < j ? i: j; What are C+ storage classes?autoregisterstaticexternauto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their defi
18、nition. They are not visible outside that block register: a type of auto variable, a suggestion to the compiler to use a CPU register for performance static: a variable that is known only in the function that contains its definition but is never destroyed and retains=keep its value between calls to
19、that function. It exists from the time the program begins executionextern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.What are storag
20、e qualifiers in C+ ?They are.constvolatilemutableConstkeyword indicates that memory once initialized, should not be altered by a program.volatilekeyword indicates that the value in the memory location can be altered even though nothing in the programcode modifies the contents, for example if you hav
21、e a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.mutable keyword indicates that particular member of a structure or class can be alter
22、ed even if a particular structure variable, class, or class member function is constant.struct data char name 80;mutable double salary;const data MyStruct = ( "Satish Shetty", 1000 ); /initlized by compilerstrcpy ( MyS, "Shilpa Shetty"); / compiler errorMyStruct. salara
23、y = 2000 ; / compiler is happy allowedWhat is reference ?reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.prepending variable with symbol makes it as reference.for example:int a;int &b = a;& 读 ampWhat is passing by reference?Metho
24、d of passing arguments to a function which takes parameter of type reference.for example:void swap( int & x, int & y )int temp = x;x = y;y = temp;int a=2, b=3;swap( a, b);Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they
25、refer to original variables a and b. so no extra memory needed to pass arguments and it is more efficient.When do use ''const'' reference arguments in function?a) Using const protects you against programming errors that inadvertently 不经意的 alter data.b) Using const allows function to
26、process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.c) Using a const reference allows the function to generate and use a temporary variable appropriately.When are temporary variables created by C+ compiler?Provided
27、 that function parameter is a "const reference", compiler generates temporary variable in following 2 ways.a) The actual argument is the correct type, but it isn't Lvaluedouble Cube(const double & num)num = num * num * num;return num;double temp = 2.0;double value = cube(3.0 + temp
28、); / argument is a expression and not a Lvalue;b) The actual argument is of the wrong type, but of a type that can be converted to the correct type long temp = 3L;double value = cuberoot (temp); / long to double conversionWhat is virtual function?When derived class overrides the base class method by
29、 redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.class parent(void Show()(cout ? "i'm parent" ? endl;class child: public p
30、arent(void Show()(cout ? "i'm child" ? endl;parent * parent_object_ptr = new child; parent_object_ptr->show() / calls parent->show() i now we goto virtual world. class parent(virtual void Show()(cout ? "i'm parent" ? endl;class child: public parent(void Show()(cout
31、? "i'm child" ? endl;parent * parent_object_ptr = new child; parent_object_ptr->show() / calls child->show()What is pure virtual function? or what is abstract class?When you define only function prototype in a base class without implementation and do the complete implementation 实
32、现 in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class.You can make a pure virtual function or abstract class this way.class Boovoid foo() = 0;Boo MyBoo; / compilation errorWhat is Memory alignment?The term alignment prim
33、arily means the tendency 趋向 of an address pointer value to be a multiple of some power of two. So a pointer with two byte alignment has a zero in the least significant bit. And a pointer with four byte alignment has a zero in both the two least significant bits. And so on. More alignment means a lon
34、ger sequence of zero bits in the lowest bits of a pointer.What problem does the namespace feature solve?Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a li
35、brary*s external declarations with a unique namespace that eliminates 消除 the potential for those collisions. namespace identifier ( namespace-body )A namespace declaration identifies and assigns a name to a declarative region.The identifier in a namespace declaration must be unique in the declarativ
36、e region in which it is used.The identifier is the name of the namespace and is used to reference its members.What is the use of 'using' declaration?A using declaration makes it possible to use a name from a namespace without the scope 范围 operator.What is an Iterator 迭代器 class?A class that i
37、s used to traverse through 穿过 the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without vi
38、olating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a const
39、ruct, which provides an interface that, when called, yields either the next element in the container, or some value denoting thefact that there are no more eleme nts to exam in e. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.What i
40、s a dangling 悬挂 pointer?A dangling pointer arises when you use the address of an object after its lifetime is over. This may occurin situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.What do you mean by Stack unw
41、inding?It is a process during exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and where it is caught.抛岀异常与栈展开 (stack unwinding)抛岀异常时,将暂停当前函数的执行,开始查找匹配的catch子句。首先检查throw本身是 否在try块内部,如果是,检查与该try相关的catch子句,看是否可以处理该异常。如
42、果不能处理,就退岀当前函数,并且释放当前函数的内存并销毁局部对象,继续到上层的调用函数中查找,直到找到一个可以处理该异常的catcho这个过程称为栈展开(stackunwinding)。当处理该异常的catch结束之后,紧接着该catch之后的点继续执行。1. 为局部对象调用析构函数如上所述,在栈展开的过程中,会释放局部对象所占用的内存并运行类类型局部对象的析构函数。但需要注意的是,如果一个块通过new动态分配内存,并且在释放该资源之前发生异常,该块因异常而退岀,那么在栈展开期间不会释放该资源,编译器不会删除该指针,这样就会造成内存泄露。2. 析构函数应该从不抛岀异常在为某个异常进行栈展开的时
43、候,析构函数如果又抛岀自己的未经处理的另一个异常,将会导致调用标准库 terminate函数。通常terminate函数将调用abort函数,导致程序的非正常退岀。所以析构函数应该从不抛岀异常。3. 异常与构造函数如果在构造函数对象时发生异常,此时该对象可能只是被部分构造,要保证能够适当的撤销这些已构造的成员。4. 未捕获的异常将会终止程序不能不处理异常。如果找不到匹配的catch,程序就会调用库函数terminate。Name the operators that cannot be overloaded?sizeof,:, ?:What is a container class? What
44、 are the types of container classes?A container class is a class that is used to hold objects in memory or external storage. A container class acts as a gen eric holder. A container class has a predefi ned behavior and a well-k nown in terface. A container class is a support ing class whose purpose
45、is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heteroge neous 不均匀的多样的contain er; whe n the container is holdi ng a group of objects that are allthe same, the container is called a homogen
46、eous 单一的均 匀的 container.顺序性容器vector从后面快速的插入与删除,直接访间任何兀素deque从前面或后面快速的插入与删除,直接访问任何兀素list双链表,从任何地方快速插入与删除关联容器set快速查找,不允许重复值multiset快速查找,允许重复值map一对多映射,基于关键字快速查找,不允许重复值multimap一对多映射,基于关键字快速查找,允许重复值容器适配器stack后进先出queue先进先出priority_queu最高优先级兀素总是第一个出列eWhat is inline fun ctio n?Thenline keyword tells the comp
47、iler to substitute 替代 the code within the function definition for every instanee of a function call. However, substitution occurs only at the compiler's discretion灵活'性.For example, the compiler does not inline a function if its address is taken or if it is too large to inline.使用预处理器实现,没有了参数压
48、栈,代码生成等一系列的操作,因此,效率很高,这是它在C中被使用的一个主要原因What is overloadi ng?With the C+ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope.-Any two fun cti ons in a set of overloaded fun cti ons must have d
49、iffere nt argume nt lists.-Overloading functions with argument lists of the same types, based on return type alone, is an error.What is Overridi ng?To override a method, a subclass of the class that orig in ally declared the method must declare a method with the same name, return type (or a subclass
50、 of that return type), and same parameter list. The definition of the method overriding is:? Must have same method name.? Must have same data type.? Must have same argument list.Overriding a method means that replacing a method functionality in child class. To imply overriding fun cti on ality we n
51、eed pare nt and child classes. In the child class you defi ne the same method sig nature as one defi ned in the pare nt class.What is ''this'' poi nter?The this poin ter is a poin ter accessible only withi n the member fun cti ons of a class, struct, or union type.It points to the ob
52、ject for which the member function is called. Static member functions do not have a this poin ter.When a non-static member fun cti on is called for an object, the address of the object is passed as a hidde n argume nt to the fun cti on. For example, the follow ing fun cti on call myDate.setMonth( 3
53、);can be interpreted 解释 this way:setMonth( &myDate, 3 );The object's address is available from with in the member fun cti on as the this poin ter. It is legal, though unn ecessary, to use the this poin ter whe n referri ng to members of the class.What happe ns whe n you make call delete this
54、;" ?The code has two built-in pitfalls 陷阱 / 误区.First, if it executes in amember function for an extern,static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so t
55、he class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise 死亡 / 转让.As far as the instantiating program is concerned 有关的,the object rema ins in scope and con ti nues to exist eve n though the obje
56、ct did itself in.Subsequent 后来的 dereferencing 间接 弓丨用 (dereferencingpointer重弓 I 用 指针,dereferencing! operator取值运算符 )of the pointer can and usually does lead to disaster 不幸.You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, "delete
57、this" could cause a disaster.How virtual functions are implemented执行 C+?Virtual functions are implemented using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a deriv
58、ed class is constructed, its base class is constructed first which creates the vtable. If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable en tries for the object may not have bee n set up by the derived class con structor yet, so you might end up calli ng base class impleme ntati ons of those virtual fun cti onsWhat is n ame man gli ng in C+?The process of encoding the parame
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工贸企业安全标准化自评报告
- 油气井增产材料研发-洞察分析
- 眼科临床研究与眼部疼痛-洞察分析
- 业务对象模型标准化-洞察分析
- 文化产业价值评估体系-洞察分析
- 水电站规划与建设-洞察分析
- 水表数据质量监控方法-洞察分析
- 威胁情报可视化技术-洞察分析
- 水暖工程项目管理-洞察分析
- 弦论中的黑洞解-洞察分析
- 2024-2030年中国轮毂电机行业市场发展趋势与前景展望战略分析报告
- (完整版)兽医临床诊断学
- GB/T 23586-2022酱卤肉制品质量通则
- 抗震支架计算书
- 大学生如果提高自己安全意识
- 意识障碍的判断及护理
- 《尾矿库安全监测技术规范》
- 人工智能基础与应用(第2版)全套教学课件
- 数据资产入表理论与实践
- 《建筑施工安全检查标准》JGJ59-20248
- 磁共振技术在食品加工中的应用
评论
0/150
提交评论