Set - 2

Question 6 :

You have two pairs: new() and delete() and another pair : alloc() and free(). 
Explain differences between eg. new() and malloc() ?

Answer :

1.) "new and delete" are preprocessors while "malloc() and free()" are functions. [we dont use brackets will calling new or delete]. 
2.) no need of allocate the memory while using "new" but in "malloc()" we have to use "sizeof()". 
3.) "new" will initlize the new memory to 0 but "malloc()" gives random value in the new alloted memory location [better to use calloc()] 

new() allocates continous space for the object instace
malloc() allocates distributed space.
new() is castless, meaning that allocates memory for this specific type,
malloc(), calloc() allocate space for void * that is cated to the specific class type pointer. 

Question 7 :

What is the difference between an ARRAY and a LIST?

Answer :

Array is collection of homogeneous elements.
List is collection of heterogeneous elements. 

For Array memory allocated is static and continuous.
For List memory allocated is dynamic and Random. 

Array: User need not have to keep in track of next memory allocation.
List: User has to keep in Track of next location where memory is allocated. 

Array uses direct access of stored members, list uses sequencial access for members.

//With Array you have direct access to memory position 5
Object x = a[5]; // x takes directly a reference to 5th element of array
//With the list you have to cross all previous nodes in order to get the 5th node:
list mylist;
list::iterator it;

for( it = list.begin() ; it != list.end() ; it++ ) {
	if( i==5) {
		x = *it;


Question 8 :

Define a constructor - What it is and how it might be called (2 methods)?

Answer :

constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized. 

Ways of calling constructor:
1) Implicitly: automatically by complier when an object is created.
2) Calling the constructors explicitly is possible, but it makes the code unverifiable.


class Point2D{
	int x; int y;
	public Point2D() : x(0) , y(0) {} //default (no argument) constructor

	Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the default constructor is implicitly called.
	Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory on HEAP we call the default constructor. 


Question 9 :

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 10 :

What is RTTI?

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 Interview Questions - Homegrown versions with a solid, consistent approach.