You are on page 1of 6

C++ FAQ 1. What is the difference between C++ structure and C++ class?

The default access level assigned to members of struct is public while the default access level assigned to a class is private. 2. What is encapsulation? Encapsulation is welding of code and data together into objects. 3. What is inheritance? Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. 4. What is polymorphism? In Greek this means "many shapes." As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. 5. What would you say if you saw "delete this" while reviewing your peer's code? You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, "delete this" could cause a disaster. 6. What is the difference between public, protected, and private members of a class? Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. 7. What is the difference between non-virtual and virtual functions? The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. 8. What is a pure virtual function? It is a member function without implementation. 9. What is an abstract base class? It is a class that has one or more pure virtual functions. 10. What is the difference between MyClass p; and MyClass p();? MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. 11. 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 pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. 12. Why were the templates introduced? Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating datadependent portions from data-independent portions, and templates were introduced to help you do that. 13. What is a static member of a class? Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. 14. What feature of C++ would you use if you wanted to design a member function that guarantees to leave "this object unchanged? It is "const" as in: "int MyFunc (int test) const;" 15. Can you overload a function based only on whether a parameter is a value or a reference? No. Passing by value and by reference looks identical to the caller. 16. What is the difference between function overloading and function overriding? Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ.

17. . Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. 18. What is the difference between assignment and initialization in C++? Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. 19. When are copy constructors called? Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function,

and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). 20. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. 21. . Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. 22. What is function's signature? Function's signature is its name plus the number and types of the parameters it accepts. 23. What does extern "C" int func(int *, Foo) accomplish? It will turn off "name mangling" for this function so that one can link to code compiled by C compiler. 24. 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. 25. What is the difference between a pointer and a reference? A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. 26. How can you access the static member of a class? <ClassName>::<StaticMemberName>. 27. How are prefix and postfix versions of operator++() differentiated? The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. 28. What functions does C++ silently write and call? Constructors, destructors, copy constructors, assignment operators, and address-of operators.

29. 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. 30. What is the difference between delete and delete[ ]? Delete deletes one object; delete[ ] deletes an array of objects. 31. Name two cases where you MUST use initialization list as opposed to assignment in constructors. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. 32. What is the difference between const char *myPointer and char *const myPointer? Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. 33. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the "A=B=C;" statement would be allowed by a compiler but "(A=B)=C;" would not be allowed by a compiler? Make operator=return a reference to a const object. 34. 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. 35. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? No. Friendship cannot be inherited. 36. 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. 37. 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(). 38. Why should you prefer throw/catch mechanism to setjmp/longjmp? The main problem with longjmp() is that it does not destroy local objects properly. 39. Can you think of a situation where your program would crash without reaching the breakpoint 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.

40. Does a class provide default Copy Constructor? Yes! The default copy constructor is being provided automatically by the compiler if not implemented separately. In this, it puts the code for coping the data members and other variables that stay in stack. If something is being created by malloc or new in the heap, those are not being copied by the default copy constructor provide by the compiler. 41. Can a program have a virtual constructor? Generally we dont need an overridable constructor. So constructor should not be declared as virtual. But in a class, we can make a static method, which will call the private/protected constructor and create an object. In that case the constructor is called as virtual constructor. 42. When and in what condition a destructor is called? There is only one destructor exists for a class. There can be two condition when the destructor is called. When attempt is made to delete the object and when user closes the application. When the application is being closed, all the objects are being deleted first and destructor corresponding to all the objects are called. 43. When does a programmer need a virtual destructor? A good design should provide scope to override the destructors of all the classes. Destructor is the function that controls 44. What is a constructor initializer list? 45. When must you use a constructor initializer list? 46. What is the order of initalization for data? 47. What are the advantages/disadvantages of using #define? 48. What are the advantages/disadvantages of using inline and const? 49. What is virtual inheritance? 50. What is a template? 51. What is an iterator? 52. What is std::auto_ptr? 53. It is possible to build a C++ compiler on top of a C compiler. How would you do this? clue: cfront

54. What's the difference between SQL, DDL, and DML? 55. What's a join? An inner join? An outer join? 56. Can you explain the singleton, visitor, facade, or handle class design pattern? 57. What is a mutable member? 58. What is an explicit constructor? 59. What is the Standard Template Library?( iterators, allocators, algorithms)

You might also like