c++ Flashcards
How pointer to member functions are used in C++?
Tip : .* and ->* operator
Base class has 2 functions. Show and Fun. Show function calls to Fun. Fun is a virtual function which is overloaded by the derived class. Create an object of derived class and call non-virtual function show() of base class. Which Fun() is called? Why?
Derived class’s Fun will be called since once a function is declared as virtual. The function to be called depends on the type of the object. No matter whether the object is stored in a pointer variable or a reference variable or its just an object
What is the use of handle class
.h file gives information about implementation of a class, like private functions, member variables etc. The user of .h file can use this information to his advantage e.g. can access private data using pointers. Also any change in private section of class would force recompilation of the client. To avoid all above problems, Handle classes are used. In handle class, only public interface of the class is exported. The handle class contains variable of one more stuct/class which will have actual class definition. In short handle class is a wrapper on main class having only public interface of the main class.
What is the type of this pointer and its usage
this pointer is of class type and is always a constant.
Can you call constructor explicitely? How?
Yes, its possible to call constructor explicitely. Just call it as a normal function call. Note that calling a constructor always creates a new object. If the function call doesn’t appear to right side of assignment statement then a local object is created and is destroyed as soon as control flows over to ;. e.g. Test is a class then Test(); statement creates a object of type Test and destroys it immediately.
How memory is deallocated if constructor throws exception?
Memory should get properly deallocated for class members and base class members. Class members- 1. ptr -> smart pointers should be used. 2. objects as members - these objects should be initialized using member initialization list. Using this method, member objects will get initialized completely before entering class constructor and hense their destructor will get called if exception occurs.
can u have one constructor private and one as public? Can u create instances of the class?
Yes, its possible to have one const private and one public in a class. Its possible to create instances of such class but only using constructor declared as public
can u call one contructor from inside other .
Yes, Calling a constructor always creates a new object. If the function call doesn’t appear to right side of assignment statement then a local object is created and is destroyed as soon as control flows over to ;. e.g. Test is a class then Test(); statement creates a object of type Test and destroys it immediately.
overloaded operators cannot declare a parameter with defult value ( function call operator is exception). why?
This is as per syntax of built-in operators. No Build-in operators (e.g. + ) take any default parameter. Same reason is applied for function call operator taking default value. Note: function call operator can take any number of arguments whereas all other operators takes only fixed number of operands. Its possible to define multiple function call operators in one class taking diff no of arguments
what is polymorphism & its types. / How polymorphism is achieved in c++.
Polymorphism means taking multiple forms depending on a context. Static polymorphism: operator overloading, function overloading and templates. Dynamic polymorphism: virtual functions
what is dynamic binding
Static binding - The code to be executed for a function call is known at compile time. Dynamic binding - The code to be executed is not known at compile time and will be decided at run time. E.g. runtime polymorphism, the code to be executed depends on the type of object pointed to by a pointer and not on the type of pointer;.
What is inheritance and encapsulation
Inheritance: the ability to derive new classes from existing classes. A derived class (or “subclass”) inherits the instance variables and methods of the “base class” (or “superclass”), and may add new instance variables and methods. New methods may be defined with the same names as those in the base class, in which case they override the original one. Encapsulation: the technique of keeping together data structures and the methods (procedures) which act on them.
what happens when you increment function pointer
VC++ compiler doesn’t allow to increment function pointer.
how to overload prefix / postfix operators
Postfix operator takes one dummy parameter (of type int for VC++ compiler).
why local varibales don’t preserve value
Local variables are allocated on stack and whenever control goes out of function, stack is cleaned up. So local variables don’t preserve values
what are diff ways to multiply a number
- using + operator. 2. if multiply by 2 then using right shift operator
what are properties of reference
1> A reference must be initialized when it is created. (Pointers can be initialized at any time.) 2>Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.) 3>You cannot have NULL references. Reference is always connected to a legitimate piece of storage.
what are diff types of inheritance
Single inheritance, multi-level inheritance, multiple inheritance, hybrid inheritance
What is the diff between macro & inline function
Macro code is replaced by the actual calls. It is processed by preprocessor and not the compiler. Type checking of arguments is not performed in macro. Inline function is a request to the compiler to inline its definition with the function call. Its upto the compiler to treat the function as inline or not.
What is the diff between malloc & new
1> New don’t need sizeof the object to be allocated. Determines size itself. 2> New calls to constructor. 3> No need to typecast the pointer after allocated by new. 4>New can be overloaded.
What is inline function ? In what cases function can’t be inlined? Why?
Inline is a request to the compiler to replace function call by its body. Its totally upto compiler when to inline a function and when not. Typical cases are 1. Inline function called recursively. 2. If function is quite big etc.
Assignment operator? Does compiler provide it?
Yes compiler synthesizes assignment operator if not provided. Compiler provided operator= does bitwise copy of the object.