Wednesday 10 April 2013

Question: How many ways are there to initialize an int with a constant?


Question: How many ways are there to initialize an int with a constant?
Answer: Two:-

1. int foo = 123;
2. int bar(123);

Question: What happens when a function throws an exception that was not specified by an exception specification for this function?


Question: What happens when a function throws an exception that was not specified by an exception specification for this function?

 Answer: Unexpected() is called, which, by default, will eventually trigger abort().

Question: Explain the scope resolution operator?


Question: Explain the scope resolution operator?

Answer: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

Question: What are the differences between a C++ struct and C++ class?


Question: What are the differences between a C++ struct and C++ class?

 Answer: The default member and base-class access specifies are different.
This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.

Question: What is the difference between new/delete and malloc/free?


Question: What is the difference between new/delete and malloc/free?

 Answer: Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.

Question: What is the difference between a copy constructor and an overloaded assignment operator?


Question: What is the difference between a copy constructor and an overloaded assignment operator? 

Answer: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

Question: What is your reaction to this line of code? delete this;


Question: What is your reaction to this line of code? delete this;
 Answer: It is not a good programming Practice.

A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious.

The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the pointer can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.
  

Question: Can inline functions have a recursion?


Question: Can inline functions have a recursion?
Answer: No.

Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.

Question: Can I mix C-style and C++ style allocation and deallocation?


Question: Can I mix C-style and C++ style allocation and deallocation?
Answer: Yes, in the sense that you can use malloc() and new in the same program. No, in the sense that you cannot allocate an object with malloc() and free it using delete. Nor can you allocate with new and delete with free () or use realloc() on an array allocated by new.

The C++ operators new and delete guarantee proper construction and destruction; where constructors or destructors need to be invoked, they are. The C-style functions malloc(), calloc(), free(), and realloc() doesn't ensure that. Furthermore, there is no guarantee that the mechanism use by new and delete to acquire and release raw memory is compatible with malloc() and free(). If mixing styles works on your system, you were simply "lucky" - for now.

If you feel the need for realloc() - and many do - then consider using a standard library vector. For example

// read words from input into a vector of strings:

vector words;
string s;
while (cin>>s && s!=".") words.push_back(s);

The vector expands as needed.

Question: Can a constructor throw a exception? How to handle the error when the constructor fails?


Question: Can a constructor throw a exception? How to handle the error when the constructor fails? 

Answer: The constructor never throws a error.