Friday, August 6, 2010

Cpp Theory Questions 2

What is conversion constructor?

 

constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.

 

for example:

 

class Boo

{

public:

Boo( int i );

};

 

Boo BooObject = 10 ; // assigning int 10 Boo object

 

What is diff between malloc()/free() and new/delete?

 

malloc allocates memory for object in heap but doesn't invoke object's constructor to initiallize the object.

 

new allocates memory and also invokes constructor to initialize the object.

 

malloc() and free() do not support object semantics

Does not construct and destruct objects

string * ptr = (string *)(malloc (sizeof(string)))

Are not safe

Does not calculate the size of the objects that it construct

Returns a pointer to void

int *p = (int *) (malloc(sizeof(int)));

int *p = new int;

Are not extensible

new and delete can be overloaded in a class

 

"delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-

 

Int_t *my_ints = new Int_t[10];

 

...

 

delete []my_ints;

---------------------------------------------------------------------------------------------------------------------

 

what is the diff between "new" and "operator new" ?

 

"operator new" works like malloc.

 

What is difference between template and macro?

 

There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.

 

If macro parameter has a postincremented variable ( like c++ ), the increment is performed two times.

 

Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.

 

for example:

 

Macro:

 

#define min(i, j) (i < j ? i : j)

 

template:

template

T min (T i, T j)

{

return i < j ? i : j;

}

 

What are C++ storage classes?

 

auto

register

static

extern

 

auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block

 

register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance

 

static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution

 

extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

---------------------------------------------------------------------------------------------------------------------

 

What is reference ??

 

reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.

 

prepending variable with "&" symbol makes it as reference.

 

for example:

 

int a;

int &b = a;

---------------------------------------------------------------------------------------------------------------------

 

What is passing by reference?

 

Method of passing arguments to a function which takes parameter of type reference.

 

for example:

 

void swap( int & x, int & y )

{

int temp = x;

x = y;

y = x;

}

 

int a=2, b=3;

 

swap( a, b );

 

Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they refer to original variables a and b. so no extra memory needed to pass arguments and it is more efficient.

---------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment