Thursday, March 31, 2011

C++ Interview questions - part2

When C++ creates a default constructor ?
- C++ create a default constructor, if an explicit constructor is not defined by the programmer. A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.
- default constructor for a class as a constructor that can be called with no arguments (this includes a constructor whose parameters all have default arguments)
- The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class.
- This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body.
- if some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. This means that a default constructor may not exist for a class.
- When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new MyClass; the default constructor is used to initialize the object
- When an array of objects is declared, e.g. MyClass x[10];; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements
- When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
- When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
- In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly, e.g. vector(10); initializes the vector with 10 elements, which are filled with the default-constructed value of our type.

Link :

Whats the difference between copy constrctor and overloaded assignment operator ?
- If a new object has to be created before the copying can occur, the copy constructor is used.
- If a new object does not have to be created before the copying can occur, the assignment operator is used.
Eg. base a;
base b;
There are three general cases where the copy constructor is called instead of the assignment operator:
1. When instantiating one object and initializing it with values from another object.
Eg. base b=a;
2. When passing an object by value.
3. When an object is returned from a function by value.
Link :

Why an empty class have size of one byte.?
- The reason is, the language standard states that all classes must have a memory size of at least 1 byte so that the class doesn't occupy the same memory space with another class. It'll be more clear if you think of two empty classes(class with no members).
- Each instance of the class/struct must have its own unique address , compiler compiler just adds a byte for padding. where as in case, if you have a member variable say "int a" inside the class/struct, it will be 4 bytes , not 5 bytes !!!

What is initialization list ?
Link :

How C++ implemented map container ? whats the data-structure it uses?
- Most probably a balanced binary search tree is used to implement map container.

1 comment: