© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

5. When is a template a better solution than a base class?

Ans:

When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class

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

Ans:

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.

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

Ans:

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

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

Ans:

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

Ex Q4: What issue do auto_ptr objects address?

Ans:

If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown.

SLogix Student Projects
bottom