Friday, August 6, 2010

Cpp Theory Questions 3

When do use "const" reference arguments in function?

 

a) Using const protects you against programming errors that inadvertently alter data.

b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.

c) Using a const reference allows the function to generate and use a temporary variable appropriately.

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

 

When are temporary variables created by C++ compiler?

 

Provided that function parameter is a "const reference", compiler generates temporary variable in following 2 ways.

 

a) The actual argument is the correct type, but it isn't Lvalue

 

double Cuberoot ( const double & num )

{

num = num * num * num;

return num;

 

}

 

double temp = 2.0;

double value = cuberoot ( 3.0 + temp ); // argument is a expression and not a Lvalue;

 

b) The actual argument is of the wrong type, but of a type that can be converted to the correct type

 

long temp = 3L;

double value = cuberoot ( temp); // long to double conversion

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

 

What is virtual function?

 

When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.

 

class parent

{

void Show()

{

cout << "i'm parent" << endl;

}

};

 

class child: public parent

{

void Show()

{

cout << "i'm child" << endl;

}

 

};

 

parent * parent_object_ptr = new child;

 

parent_object_ptr->show() // calls parent->show() i

 

now we goto virtual world...

 

class parent

{

virtual void Show()

{

cout << "i'm parent" << endl;

}

};

 

class child: public parent

{

void Show()

{

cout << "i'm child" << endl;

}

 

};

 

parent * parent_object_ptr = new child;

 

parent_object_ptr->show() // calls child->show()

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

 

What is pure virtual function? or what is abstract class?

 

When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class.

 

You can make a pure virtual function or abstract class this way..

 

class Boo

{

void foo() = 0;

}

 

Boo MyBoo; // compilation error

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

 

What is Memory alignment?

 

The term alignment primarily means the tendency of an address pointer value to be a multiple of some power of two. So a pointer with two byte alignment has a zero in the least significant bit. And a pointer with four byte alignment has a zero in both the two least significant bits. And so on. More alignment means a longer sequence of zero bits in the lowest bits of a pointer.

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

 

What problem does the namespace feature solve?

 

Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library's external declarations with a unique namespace that eliminates the potential for those collisions.

 

namespace [identifier] { namespace-body }

 

A namespace declaration identifies and assigns a name to a declarative region.

The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members.

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

 

What is the use of 'using' declaration?

 

A using declaration makes it possible to use a name from a namespace without the scope operator.

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

 

What is an Iterator class?

 

A class that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.

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

 

What is a dangling pointer?

 

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

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

No comments:

Post a Comment