c++ Flashcards

1
Q

How pointer to member functions are used in C++?

A

Tip : .* and ->* operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the use of handle class

A

.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the type of this pointer and its usage

A

this pointer is of class type and is always a constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you call constructor explicitely? How?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How memory is deallocated if constructor throws exception?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

can u have one constructor private and one as public? Can u create instances of the class?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can u call one contructor from inside other .

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

overloaded operators cannot declare a parameter with defult value ( function call operator is exception). why?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is polymorphism & its types. / How polymorphism is achieved in c++.

A

Polymorphism means taking multiple forms depending on a context. Static polymorphism: operator overloading, function overloading and templates. Dynamic polymorphism: virtual functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is dynamic binding

A

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;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is inheritance and encapsulation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what happens when you increment function pointer

A

VC++ compiler doesn’t allow to increment function pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to overload prefix / postfix operators

A

Postfix operator takes one dummy parameter (of type int for VC++ compiler).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

why local varibales don’t preserve value

A

Local variables are allocated on stack and whenever control goes out of function, stack is cleaned up. So local variables don’t preserve values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what are diff ways to multiply a number

A
  1. using + operator. 2. if multiply by 2 then using right shift operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what are properties of reference

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what are diff types of inheritance

A

Single inheritance, multi-level inheritance, multiple inheritance, hybrid inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the diff between macro & inline function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the diff between malloc & new

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is inline function ? In what cases function can’t be inlined? Why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Assignment operator? Does compiler provide it?

A

Yes compiler synthesizes assignment operator if not provided. Compiler provided operator= does bitwise copy of the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Write a pointer to function and call that function using funtion pointer

A

Some care should be taken while declaring pointer to function. The simplest way to declare such pointer is 1. Take the definition of the function 2. put * (pointer) before function name and wrap it in a () bracket. E.g. void foo(char) so function pointer declaration would be void (foo) (char) so now foo becomes a pointer to a function which returns void and takes char as a parameter. The best way could be to declare a typedef and then use it as a datatype e.g. typedef void(FPTR) (char) now FPTR is a user defined data type and so variables can be declared using it FPTR test;

24
Q

Why cant you overload sizeof, ::, ., :? Operators

A

. -> This operator is used to access members of class. If overloaded, the class members wont get accessed. Sizeof() -> gives how many memory is taken by the object. Its hardware dependant and also since its related to memory better to not play with it. ?:-> ternary operator, can take operands as expressions as well. If we overload this operator, both the expression will be passed on to the stack and will be evaluated immediately. This changes its default behaviour. also what would be the type of the expression. ::-> scope resolution operator is used to access data from other namespaces and class members. So shouldn’t be overloaded.

25
Q

Which operators cant be overloaded using friend function? Why?

A

the operators [], (), = and -> cannot be overloaded using friend function. These operator must take their first operand of type class on which they are overloaded. Non-static member function ensures that first operand is lvalue

26
Q

Where does explicit keywork is used?

A

If constructor takes a single argument object of another type then such constructor is used for automatic type conversion. Explicit keyword is used in constructor declaration. It ensures disabling automatic type conversion using constructor.

27
Q

Constructor is throwing some exception. How the memory allocated for that object is deleted.

A

Throwing exceptions out of the constructor is a good practice. Always use smart pointers instead of raw pointer and use member initialization list for initializing the member object variables

28
Q

User has provided parametarised constructor. Will compiler provide default constructor.

A

Default constructor is provided only when no parametarized constructor is there. If user has provided parametarized constructor, he should provide default constructor as well

29
Q

What is the size of a reference.

A

Size of reference is same as the type of variable it is refering to. So sizeof(int ref) = 4, sizeof(double ref) = 8, sizeof(bool ref) = 1.

30
Q

What is difference between pass by reference and pass by pointer

A

No difference in the way parameters are passed. The only difference is ease of use. For reference, you don’t need address of operator to access value in it. Its just an alias to the existing variable.

31
Q

Can constructors and destructors throw exception. Is it a good programming practice.

A

Yes both constructors and destructors can throw exceptions. Constructors throwing exceptions is a good programming practice. Constructors don’t return any value so exception is a good way to communicate to main program about any errors generating in constructors. If constructor is throwing any exception, make sure that you use member initialization list to initialize class member and smart pointers. Its not good to throw exception out of destructor. Destructor might get called as a result of exception and if destructor again throws exception then the program might get abnormally terminated.

32
Q

What is lvalue.

A

lvalue is the value which can be changed i.e. not constant. It generally appears on lhs of an expression.

33
Q

A function call is rvalue always? But it can be lvalue if the return type is a reference? What is the use of it.

A

This is useful only in case of [] operator overloading. Returning a reference makes it possible to appear as lhs.

34
Q

What happens if you make copy constructor private.

A

By making copy constructor private, you can restrict anyone making copies of your object by passing it to a function, or returning from a function or initializing one with another.

35
Q

What is the overhead of exception handling?

A

When an exception is thrown there is considerable runtime overhead. Also some extra information is put on the stack to aid in stack unwinding.

36
Q

What will be the return value for operator[]

A

Two versions of [] operator should be declared. One returning reference and other returing a const reference. Make the other function const as well. This will make sure that [] operator works on const objects as well.

37
Q

User defined conversion operators. How to avoid such conversions

A

User defined conversion operators are used to define automatic conversion of one type to another. Once such operator is defined there is no way to disable the automatic conversion.

38
Q

What should be the return type for prefix and postfix operators

A

Prefix operator: here the object is changed and then used in the remaining expression. So should be returned as non const reference. Postfix operator: Object is used and then changed. A temporary object is returned and returned by value. The return type should be constant since (j++)++ is not allowed by language.

39
Q

Which is most efficient, prefix or postfix?

A

Prefix operator is more efficient than postfix since Prefix takes no arguments while postfix takes one dummy argument. The return type of prefix is by reference whilst return type of postfix operator is by value.

40
Q

what is return type of operator =? Why?

A

It should be reference always. By this method user can create more complex expressions using assignment operator. E.g. e=b=c=d

41
Q

What is the use of dynamic cast operator

A

Used to find exact type of an object when you have only a pointer or reference to the base type. It finds out what kind of object is pointed to by the base class pointer. This is called downcasting. Note that downcasting is allowed only if virtual machanism is involved, since RTTI uses Vtable to find out exact type of the object.

42
Q

Why to check for self assignment in operator = or copy constructor

A

If class is allocating some memory in its constructor then its very important to check for self assignmnet in assignment operator and copy constructor.

43
Q

Why declare a copy constructor and assignment operator for classes that dynamically allocate memory

A

If these functions are not provided then during initialization and assignment deep copy of the objects is performed. So if the object is holding dynamically allocated memory, this could result in multiple objects pointing to same memory location.

44
Q

Why to prefer initialization list than assignment in constructor body?

A

All member variables are created when control enters constructor’s body and inside constructor assignment happens. So if the class has objects as its members, two functions are invoked (i.e. constructor and assignment operator). So its better to use member iniitalization list. Also this method ensures destruction of member objects if constructor throws exception.

45
Q

How to initialize constant members and reference members in constructor

A

Using member initialization list

46
Q

In what order class members get created using member initialization list?

A

It depends on the order in which member objects are declared in the class and not on the order how it is included in member initialization list.

47
Q

What Problem could arise if exception is thrown out of constructor

A

If exception is thrown out of constructor then corresponding destructor is not called since object is not constructed completely. So the resources allocated by the constructor before throwing exception wont get released. The solution to this problem is make all resource holding members as class object.

48
Q

Can destructor throw exception? If yes what are the adv. And disavantages?

A

Destructor should not throw any exception since destructors are called as a result of exception and if exceptions are thrown during unwinding then program may get terminated abnormally.

49
Q

Why catch exception by reference?

A

If you throw an object of derived class and catch it by value in the handler for an object of base, then that thrown object is sliced. So you wont get information about the derived part. To avoid this always throw exceptions by reference. Throwing by pointer will envolve coupling since thrower and catcher should agree on how the exception object is allocated and deallocated. also it may happen that exception is occured because of heap exhaustion.

50
Q

What is the cost of exception handling?

A

When an exception is thrown there is considerable runtime overhead. Also some extra information is put on the stack to aid in stack unwinding.

51
Q

Placement new syntax? How to destruct object in this case

A

Placement new operators syntax is same as operator new. The first parameter is of type size_t and after that it can take any no of parameters of any type. Generally second parameter is the location where object to be allocated. The objects created using placement new operators need to be destroyed explicitely by calling destructor of that object. e.g. ptr->X::~X(). Also there is a placement delete form of operator delete. This function is called only if object allocated using placement new throws some exception in its constructor. Placement delete ensures that no memory is leaked.

52
Q

What is object slicing

A

When a derived class object is passed by value to a function accepting base class object, the object is sliced and only base part is passed on to the function. This is called object slicing

53
Q

What is the Difference between class and struct

A
  1. all struct members are public by default and in class all are private. 2. Same is for default inheritance, struct entails public inheritance by default and class private.
54
Q

What special care need to be taken while writing derived class’s assignment operator and copy constructor. What default action is taken otherwise.

A

Assignment operator is not inherited automatically. The derived class’s assignment operator should make an explicit call to base class’s assignment operator. Also derived class’s copy constructor should make an explicit call to base class’s copy constructor otherwise by default compiler calls to the default constructor.

55
Q

What is the use of private constructor?

A

Private constructor stops creation of object by external world. Only class members can create instance of the class. This method is used in singleton design pattern

56
Q

Can pure virtual functions have body

A

Yes, Pure virtual functions can have body. E.g. Declare base class’s destructor as pure virtual. This will make sure that all the derived classes object are getting destructed properly (since base class destructor is virtual, all the destructors will be called in in order).