Sie sind auf Seite 1von 12


Question: What is virtual constructors/destructors? Answer: Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function(matching the pointer type) is called on the object.There is a simple solution to this problem declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they dont have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance? Yes. Question: What are the advantages of inheritance? It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional. Question: What is the difference between declaration and definition? The declaration tells the compiler that at some later point we plan to present the definition of this declaration. E.g.: void stars () //function declaration The definition contains the actual implementation. E.g.: void stars () // declarator { for(int j=10; j>=0; j--) //function body cout<<*; cout<<endl; } Question: What do you mean by inline function? Answer: The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables. Question: Difference between realloc() and free()? Answer: The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer. Question: What is a template? Answer: Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones: template <class indetifier> function_declaration; template <typename indetifier>function_declaration; The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way. Question: What is the difference between class and structure? Answer: Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private. Question: What is RTTI?

Page 1 of 12

Answer: Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many homegrown versions with a solid, consistent approach. Question: What is encapsulation? Answer: Packaging an objects variables within its methods is called encapsulation. Question: What is an object? Answer: Object is a software bundle of variables and related methods. Objects have state and behavior. Question: What is public, protected, private? Answer: Public, protected and private are three access specifiers in C++. Public data members and member functions are accessible outside the class. Protected data members and member functions are only available to derived classes. Private data members and member functions cant be accessed outside the class. However there is an exception can be using friend classes. Question: What is namespace? Answer: Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces. The form to use namespaces is: namespace identifier { namespace-body } Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example: namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator :: For example, to access the previous variables we would have to put: general::a general::b The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error. Question: What do you mean by Answer: Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own. Question: What is function overloading and operator overloading? Answer: Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types. Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs). Question: What is virtual class and friend class? Answer: Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

Page 2 of 12

Question: What do you mean by binding of data and functions? Answer: Encapsulation. Question: What is the difference between an object and a class? Answer: Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects. A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change. The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change. Question: What is a class? Answer: Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class. Question: What is friend function? Answer: As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition. Question: What is abstraction? Answer: Abstraction is of the process of hiding unwanted details from the user. Question: What are virtual functions? Answer: A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class. Question: What is a scope resolution operator? Answer: A scope resolution operator (::), can be used to define the member functions of a class outside the class. Question: What do you mean by pure virtual functions? Answer: A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero. class Shape { public: virtual void draw() = 0; }; Question: What is polymorphism? Explain with an example? Answer: "Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus + sign, used for adding two integers or for using it to concatenate two strings.


Is it possible to have Virtual Constructor? If yes, how?If not, Why not possible ? There is nothing like Virtual Constructor. The Constructor cant be virtual as the constructor is a code which is responsible for creating a instance of a class and it cant be delegated to any other object by virtual keyword means.

2. 3.

What about Virtual Destructor? Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object baller is balling to , proper destructor will be called. What is Pure Virtual Function? Why and when it is used ? The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation

Page 3 of 12

error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

4. 5. 6. 7.

What is problem with Runtime type identification? The run time type identification comes at a cost of performance penalty. Compiler maintains the class. How Virtual functions call up is maintained? Through Look up tables added by the compile to every class image. This also leads to performance penalty. Can inline functions have a recursion? No. Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time. How do you link a C++ program to C functions? By using the extern "C" linkage specification around the C function declarations. Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.

8. 9.

Explain the scope resolution operator? It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope. How many ways are there to initialize an int with a constant? 1. int foo = 123; 2. int bar(123);


What is your reaction to this line of code? delete this; It is not a good programming Practice. A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious. The code has two built-in pitfalls. First, if it executes in a member 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 the 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 remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the baller can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.

11. 12.

What is the difference between a copy constructor and an overloaded assignment operator? A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class. When should you use multiple inheritance? There are three acceptable answers:- "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way." Consider an Asset class, Building class, Vehicle class, and CompanyCar class. All company cars are vehicles. Some company cars are assets because the organizations own them. Others might be leased. Not all assets are vehicles. Money accounts are assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all buildings are assets. Ad infinitum. When you diagram these relationships, it becomes apparent that multiple inheritance is a likely and intuitive way to model this common problem domain. The applicant should understand, however, that multiple inheritance, like a chainsaw, is a useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.


What is a virtual destructor? The simple answer is that a virtual destructor is one that is declared with the virtual attribute. The behavior of a virtual destructor is what is important. If you destroy an object through a baller or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be comple

14. 15.

Can a constructor throw a exception? How to handle the error when the constructor fails? The constructor never throws a error. What are the debugging methods you use when came across a problem? Debugging with tools like : GDB, DBG, Forte, Visual Studio. Analyzing the Core dump. Using tusc to trace the last system call before crash. Putting Debug statements in the program source code.


How the compilers arranges the various sections in the executable image? The executable had following sections:Data Section (uninitialized data variable section, initialized data variable section ) Code Section Remember that all static variables are allocated in the initialized variable section.

Page 4 of 12

128314019.doc 17. 18.

Explain the ISA and HASA class relationships. How would you implement each in a class design? A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class. This relationship is best implemented by embedding an object of the Salary class in the Employee class. When is a template a better solution than a base class? When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class.


What are the differences between a C++ struct and C++ class ? The default member and base-class access specifies are different. This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.

20. 21. 22. 23.

How do you know that your class needs a virtual destructor? If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a baller to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. What is the difference between new/delete and malloc/free? Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. What happens when a function throws an exception that was not specified by an exception specification for this function? Unexpected() is called, which, by default, will eventually trigger abort(). Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()? C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.

24. 25. 26. 27.

What issue do auto_ptr objects address? If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Is there any problem with the following: char *a=NULL; char& p = *a;? The result is undefined. You should never do this. A reference must always refer to some object. Why do C++ compilers need name mangling? Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Is there anything you can do in C++ that you cannot do in C? No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C

o o o o o o o o o o o o o o o

What are the major differences between C and C++? What are the differences between new and malloc? What is the difference between delete and delete[]? What are the differences between a struct in C and in C++? What are the advantages/disadvantages of using #define? What are the advantages/disadvantages of using inline and const? What is the difference between a baller and a reference? When would you use a baller? A reference? What does it mean to take the address of a reference? What does it mean to declare a function or variable as static? What is the order of initalization for data? What is name mangling/name decoration? What kind of problems does name mangling cause? How do you work around them? What is a class? What are the differences between a struct and a class in C++? What is the difference between public, private, and protected access? For class CFoo { }; what default methods will the compiler generate for you>? How can you force the compiler to not generate them? What is the purpose of a constructor? Destructor? What is a constructor initializer list?

Page 5 of 12

o o o o o o o o

When must you use a constructor initializer list? What is a: Constructor? Destructor? Default constructor? Copy constructor? Conversion constructor? What does it mean to declare a...


member function as virtual? member function as static? member function as static? member variable as static? destructor as static? Can you explain the term "resource acquisition is initialization?" What is a "pure virtual" member function? What is the difference between public, private, and protected inheritance? What is virtual inheritance? What is placement new?

What is the difference between operator new and the new operator? What is exception handling? Explain what happens when an exception is thrown in C++. What happens if an exception is not caught? What happens if an exception is throws from an object's constructor? What happens if an exception is throws from an object's destructor? What are the costs and benefits of using exceptions? When would you choose to return an error code rather than throw an exception? What is a template? What is partial specialization or template specialization? How can you force instantiation of a template? What is an iterator? What is an algorithm (in terms of the STL/C++ standard library)? What is std::auto_ptr?

o o o o o o

What is wrong with this statement? std::auto_ptr ptr(new char[10]);

It is possible to build a C++ compiler on top of a C compiler. How would you do this?

-*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*C++ interview questions This set of C++ interview questions was sent to TechInterviews from Australia:

Page 6 of 12

1. 2. 3. 4. 5. 6. What is the difference between an ARRAY and a LIST? What is faster : access the element in an ARRAY or in a LIST? Define a constructor - what it is and how it might be called (2 methods). Describe PRIVATE, PROTECTED and PUBLIC the differences and give examples. What is a COPY CONSTRUCTOR and when is it called (this is a frequent question !)? Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE. 7. What is the word you will use when defining a function in base class to allow this function to be a polimorphic function? 8. What are 2 ways of exporting a function from a DLL? 9. You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc() 10. What is a callback function. Explain in C and C++ and WIN API environment. 11. (From WINDOWS API area): what is LPARAM and WPARAM? Read all | Browse topics: C++ -*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*C and C++ questions for the interview 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. What is the output of printf(%d) What will happen if I say delete this Difference between C structure and C++ structure. Diffrence between a assignment operator and a copy constructor What is the difference between overloading and overridding? Explain the need for Virtual Destructor. Can we have Virtual Constructors? What are the different types of polymorphism? What are Virtual Functions? How to implement virtual functions in C What are the different types of Storage classes? What is Namespace? What are the types of STL containers?. Difference between vector and array? How to write a program such that it will delete itself after exectution? Can we generate a C++ source code from the binary file? What are inline functions? What is strstream ? Explain passing by value, passing by pointer and passing by reference Have you heard of mutable keyword? What is a RTTI? Is there something that I can do in C and not in C++? What is the difference between calloc and malloc? What will happen if I allocate memory using new and free it using free or allocate sing calloc and free it using delete? Difference between printf and sprintf. What is map in STL? When shall I use Multiple Inheritance? Explain working of printf. Talk sometiming about profiling? How many lines of code you have written for a single program? How to write Multithreaded applications using C++? Write any small program that will compile in C but not in C++ What is Memory Alignment? Why preincrement operator is faster than postincrement? What are the techniques you use for debugging? How to reduce a final size of executable? Give 2 examples of a code optimization.

Read all | Browse topics: C++ -*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*--*-*-*-*-*-*-*-

C++ notes for discussion

This is not really a set of interview questions, a reader sent what looks like a copy of his notes from C++ class that describes various tricks and gotchas for C++.

Page 7 of 12

128314019.doc 1. What is the output of printf(%d)? 1. %d helps to read integer data type of a given variable 2. when we write (%d, X) compiler will print the value of x assumed in the main 3. but nothing after (%d) so the output will be garbage 4. printf is an overload function doesnt check consistency of the arg list segmentation fault 2. What will happen if I say delete this? - destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test() 1. pointer var will still be valid 2. object created by new exists until explicitly destroyed by delete 3. space it occupied can be reused by new 4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX 5. delete = delete objects 6. delete[] delete array 7. delete operator destroys the object created with new by deallocating the memory assoc. with the object 8. if a destructor has been defined fir a class delete invokes that desructor 3. Difference between C structure and C++ structure - C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++ 1. structures are a way of storing many different values in variables of potentially diff types under under the same name 2. classes and structures make program modular, easier to modify make things compact 3. useful when a lot of data needs to be grouped together 4. struct Tag {}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure 5. members of a struct in C are by default public, in C++ private 6. unions like structs except they share memory allocates largest data type in memory - like a giant storage: store one small OR one large but never both @ the same time 7. pointers can point to struct: 8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members 9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible 10. structs usually used for data only structures, classes for classes that have procedures and member functions 11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object 12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions 13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming 4. Difference between assignment operator and copy constructor - -assignment operator = assigning a variable to a value - copy constructor 1. constructor with only one parameter of its same type that assigns to every nonstatic class member variable of the object a copy of the passed object 2. copy assignment operator must correctly deal with a well constructed object - but copy constructor initializes uninitialized memory 3. copy constructor takes care of initialization by an object of the same type x

Page 8 of 12

128314019.doc 4. for a class for which the copy assignment and copy constructor not explicitly declared missing operation will be generated by the compiler. Copy operations are not inherited - copy of a class object is a copy of each member 5. memberwise assignment: each member of the right hand object is assigned to the corresponding member of the left hand object 6. if a class needs a copy constructor it will also need an assignment operator 7. copy constructor creates a new object, assignment operator has to deal w/ existing data in the object 8. assignment is like deconstruction followed by construction 9. assignment operator assigns a value to a already existing object 10. copy constructor creates a new object by copying an existing one 11. copy constructor initializes a freshly created object using data from an existing one. It must allocate memory if necessary then copy the data 12. the assignment operator makes an already existing object into a copy of an existing one. 13. copy constructor always creates a new object, assignment never does 5. Difference between overloading and overriding? 1. Overload - two functions that appear in the same scope are overloaded if they have the same name but have different parameter list 2. main() cannot be overloaded 3. notational convenience - compiler invokes the functions that is the best match on the args found by finding the best match between the type of arg expr and parameter 4. if declare a function locally, that function hides rather than overload the same function declared in an outer scope 5. Overriding - the ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass 6. the overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class 7. writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature 8. all functions in the derived class hide the base class functions with the same name except in the case of a virtual functions which override the base class functions with the same signature 6. Virtual 1. single most important feature of C++ BUT virtual costs 2. allows derived classes to replace the implementation provided by the base class 3. without virtual functions C++ wouldnt be object oriented 4. Programming with classes but w/o dynamic binding == object based not OO 5. dynamic binding can improve reuse by letting old code call new code 6. functions defined as virtual are ones that the base expects its derived classes to redefine 7. virtual precedes return type of a function 8. virtual keyword appears only on the member function declaration inside the class 9. virtual keyword may not be used on a function definition that appears outside the class body 10. default member functions are nonvirtual 7. Dynamic Binding 1. delaying until runtime the selection of which function to run 2. refers to the runtime choice of which virtual function to run based on the underlying type of the object to which a reference or a pointer is based 3. applies only to functions declared as virtual when called thru reference or ptr 4. in C++ dynamic binding happens when a virtual function is called through a reference (|| ptr) to a base class. The face that ref or ptr might refer to either a base or a derived class object is the key to dynamic binding. Calls to virtual functions made thru a reference or ptr are resolved at run time: the function that is called is the one defined by the actual type of the object to which the reference or pointer refers Page 9 of 12

128314019.doc 8. Explain the need for a virtual destructor 1. destructor for the base parts are invoked automatically 2. we might delete a ptr to the base type that actually points to a derived object 3. if we delete a ptr to base then the base class destructor is run and the members of the base class are cleared up. If the objectis a derived type then the behavior is undefined 4. to ensure that the proper destructor is run the destructor must be virtual in the base class 5. virtual destructor needed if base pointer that points to a derived object is ever deleted (even if it doesnt do any work) 9. Rule of 3 1. if a class needs a destructor, it will also need an assignment operator and copy constructor 2. compiler always synthesizes a destructor for us 3. destroys each nonstatic member in the reverse order from that in which the object was created 4. it destroys the members in reverse order from which they are declared in the class1. if someone will derive from your class2. and if someone will say new derived where derived is derived from your class3. and if someone will say delete p, where the actual objects type is derived but the pointer ps type is your class 5. make destructor virtual if your class has any virtual functions 10. Why do you need a virtual destructor when someone says delete using a Base ptr thats pointing @ a derived object? - when you say delete p and the class of p has a virtual destructor the destructor that gets invoked is the one assoc with the type of the object*p not necessarily the one assoc with the type of the pointer == GOOD 11. Different types of polymorphism 1. types related by inheritance as polymorphic types because we can use many forms of a derived or base type interchangeably 2. only applies to ref or ptr to types related by inheritance. 3. Inheritance - lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different 4. derived classes 1. can use w/o change those operations that dont depend on the specifics of the derived type 2. redefine those member functions that do depend on its type 3. derived class may define additional members beyond those it inherits from its base class. 5. Dynamic Binding - lets us write programs that use objects of any type in an inheritance hierarchy w/o caring about the objects specific types 6. happens when a virtual function is called through a reference || ptr to a base class 7. The fact that a reference or ptr might refer to either a base or derived class object is the key to dynamic binding 8. calls to virtual functions made though a reference/ptr resolved @ runtime 9. the function that is called is the one defined by the actual type of the object to which ref/ptr refers 12. How to implement virtual functions in C - keep function pointers in function and use those function ptrs to perform the operation 13. What are the different type of Storage classes? 1. automatic storage: stack memory - static storage: for namespace scope objects and local statics 2. free store: or heap for dynamically allocated objects == design patterns 14. What is a namespace? 1. every name defined in a global scope must be unique w/in that scope 2. name collisions: same name used in our own code or code supplied to us by indie producers == namespace pollution 3. name clashing - namespace provides controlled mechanism for preventing name collisions 4. allows us to group a set of global classes/obj/funcs 5. in order to access variables from outside namespace have to use scope :: operator Page 10 of 12

128314019.doc 6. using namespace serves to assoc the present nesting level with a certain namespace so that objectand funcs of that namespace can be accessible directly as if they were defined in the global scope 15. Types of STL containers - containers are objects that store other objects and that has methods for accessing its elements - has iterator - vector 16. Difference between vector and array - -array: data structure used dto store a group of objects of the same type sequentially in memory - vector: container class from STL - holds objects of various types - resize, shrinks grows as elements added - bugs such as accessing out of bounds of an array are avoided 17. Write a program that will delete itself after execution. Int main(int argc, char **argv) { remove(argv[0]);return 0;} 18. What are inline functions? 1. treated like macro definitions by C++ compiler 2. meant to be used if theres a need to repetitively execute a small block if code which is smaller 3. always evaluates every argument once 4. defined in header file 5. avoids function call overload because calling a function is slower than evaluating the equivalent expression 6. its a request to the compiler, the compiler can ignore the request 19. What is strstream? defines classes that support iostreams, array of char obj 20. Passing by ptr/val/refArg? 1. passing by val/refvoid c::f(int arg) by value arg is a new int existing only in function. Its initial value is copied from i. modifications to arg wont affect the I in the main function 2. void c::f(const int arg) by value (i.e. copied) the const keyword means that arg cant be changed, but even if it could it wouldnt affect the I in the main function 3. void c::f(int& arg) - -by reference, arg is an alias for I. no copying is done. More efficient than methods that use copy. Change in arg == change in I in the calling function 4. void c::f(const int& arg) - -by reference, int provided in main call cant be changed, read only. Combines safety with efficacy. 5. void c::f(const int& arg) const like previous but final const that in addition the function f cant change member variables of cArg passing using pointers 6. void c::f(int *arg) by reference changing *arg will change the I in the calling function 7. void c::f(const int *arg) by reference but this time the I int in the main function cant be changed read only 8. void c::f(int * const arg) by reference the pointer arg cant be changed but what it points to (namely I of the calling function) can 9. void c::f(const int * const arg) by reference the pointer arg cant be changed neither can what it points to 21. Mutable keyword? 1. keyword is the key to make exceptions to const 2. mutable data member is allowed to change during a const member function 3. mutable data member is never const even when it is a member of a const object 4. a const member function may change a mutable member 22. Something you can do in C but not in C++? C++ applications generally slower at runtime and compilation input/output 23. Difference between calloc and malloc? 1. malloc: allocate s bytes 2. calloc: allocate n times s bytes initialized to 0 24. What will happen if I allocate memory using new & free memory using free? WRONG 25. Difference between printf and sprintf? 1. sprintf: a function that puts together a string, output goes to an array of char instead of stdout 2. printf: prints to stdout 26. map in STL? Page 11 of 12

128314019.doc 1. used to store key - value pairs, value retrieved using the key 2. store data indexed by keys of any type desire instead of integers as with arrays 3. maps are fast 0(log(n)) insertion and lookup time 4. std::map<key_type, data_type>EX:Std::map<string, char> grade_list //grade_list[john] = b 27. When will we use multiple inheritance? 1. use it judiciously class 2. when MI enters the design scope it becomes possible to inherit the same name (function/typedef) from more than one base class == ambiguity 3. C++ first identifies the function thats the best match for the call 4. C++ resolves calls to overload before accessibility, therefore the accessibility of Elec_Gadget() checkout is never evaluated because both are good matches == ERROR 5. resolve ambiguity mp.Borrowable_Item::checkOut(); mp.Elec_Gadget::checkOut(); //error because trying to access private 6. deadly MI diamond: anytime you have an inheritance hierarchy w/ more than one path between a base class and a derived classEX:FileInput File Output FileIOFile//File and IOFile both have paths through InputFile and OutputFile 28. Multithreading - C++ does not have a notion of multithreading, no notion of concurrency 29. Why is the pre-increment operator faster than the post-increment operator? pre is more efficient that post because for post the object must increment itself and then return a temporary containing its old value. True for even built in types 30. What is a hash and what would you use it for? 31. What is a dot product and what is a cross product - what would you use them for? 32. What is 2 ^ 101?


Page 12 of 12