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 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().
|