Thursday 11 April 2013

Question: Why is "this" not a reference?


Question: Why is "this" not a reference?

 Answer: Because "this" was introduced into C++ before references were added.

Question: Why doesn't C++ have an equivalent to realloc()?


Question: Why doesn't C++ have an equivalent to realloc()?

 Answer: If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occationally does copy its argument array.
In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

Question: Why do C++ compilers need name mangling?


Question: Why do C++ compilers need name mangling?

 Answer: Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name.

Question: What's the value of i++ + i++?


Question: What's the value of i++ + i++?

Answer: It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:
v[i] = i++;

Related example:
f(v[i],i++);

Here, the result is undefined because the order of evaluation of function arguments are undefined. Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are
typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.

Question: What is Pure Virtual Function? Why and when it is used ?


Question: What is Pure Virtual Function? Why and when it is used ?

Answer: The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error.

This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

Question: What is Memory Leak?


Question: What is Memory Leak?

Answer: Memory which has no pointer pointing to it and there is no way to delete or reuse this memory(object), it causes Memory leak.
{
Base *b = new Base();
}
Out of this scope b no longer exists, but the memory it was pointing to was not deleted. Pointer b itself was destroyed when it went out of scope.

Question: Write the hello world program . . . Without using any semi-colon's ";" ?


Question: Write the hello world program . . . Without using any semi-colon's ";" ?
Answer:
#include

void main()
{
if(printf("Hello World")) { }
}

Question: What is faster ++i or i++, where i is an interger variable?


Question: What is faster ++i or i++, where i is an interger variable?

 Answer: The answer to this lies in the fact, how they used. With ++i(PreIncrement) the variable is incremented and new value is returned. So it requires one instruction to increment the variable.

In case of i++(post Increment), the old value has to be returned or used in the expression and then the variable is incrememented after the expression is evaluated. Since you need one instruction to save the old value to be used in the expression and other instruction to increment the variable, its comparatively slower.

Question: What is an object?


Question: What is an object?

Answer: A region of storage with associated semantics. For example, int i; "i is an object of type int". In Object Oriented C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects.

Question: What about Virtual Destructor?


Question: What about Virtual Destructor?

 Answer: Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object pointer is pointing to , proper destructor will be called.

Question: How do you know that your class needs a virtual destructor?


Question: How do you know that your class needs a virtual destructor?

Answer: If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.

Question: Should I use NULL or 0?


Question: Should I use NULL or 0?

 Answer: In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided.
That's less common these days.

Question: What is problem with Runtime type identification?


Question: What is problem with Runtime type identification?

Answer: The run time type identification comes at a cost of performance penalty. Compiler maintains the class.

Question: Is there anything you can do in C++ that you cannot do in C?


Question: Is there anything you can do in C++ that you cannot do in C?

Answer: No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C.

Question: Is there any problem with the following: char *a=NULL; char& p = *a;?


Question: Is there any problem with the following: char *a=NULL; char& p = *a;?

Answer: The result is undefined. You should never do this. A reference must always refer to some object.

Question: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?


Question: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?

 Answer: Virtual Constructor doesn't exist.

Constructor can’t be virtual as the constructor is a code which is responsible for creating a instance of a class and it can’t be delegated to any other object by virtual keyword means.

Question: Is it necessary to already know another programming language before learning C++?


Question: Is it necessary to already know another programming language before learning C++? 

Answer: Not necessarily. C++ is a simple and clear language in its expressions. It is true that a piece of code written with C++ may be seen by a stranger of programming a bit more cryptic than some other languages due to the intensive use of special characters ({}[]*&!|...), but once one knows the meaning of such characters it can be even more schematic and clear than other languages that rely more on English words.
Also, the simplification of the input/output interface of C++ in comparison to C and the incorporation of the standard template library in the language, makes the communication and manipulation of data in a program written in C++ as simple as in other languages, without losing the power it offers.