Technical Interview Questions and Answers :: C++
21 / 187
• Templates allow implementing the concept of generic programming.
• Template allows us to generate a family of classes or a family of functions to handle different data types.
• Template classes and functions eliminate code duplication for different types and thus make the program development easier and more manageable.
• We can use multiple parameters in both the class templates and function templates.
• Like other functions, templates can be overloaded.
22 / 187
Some peculiar problems other than logic or syntax errors, they are known as exceptions. Exceptions are run-time anomalies or unusual conditions that a program may encounter while executing. Anomalies might include the condition such a division by zero, array outside of bound etc.
23 / 187
Exception handling mechanism is basically built upon through keywords namely try, catch, and throw. The keyword try is used to preface a block of statements which may generate an exception. This block of statements is known as try block. When an exception is detected, it is thrown using a throw statement in the try block. A catch statement is defined by the keyword catch “catches†the exception thrown by the throw statement in the try block.
24 / 187
The purpose of exception handling mechanism is to provide means to detect and report an “exception circumstanceâ€. So that appropriate action can be taken. The mechanism suggests a separate error handling code that performs the following tasks.
1. Find the problem (hit the exception).
2. Inform than an error has occurred (throw the exception).
3. Receives the error information (catch the exception).
4. Take corrective action (handle the exception).
25 / 187
When an exception that is desired to be handled is detected, it is thrown using throw statement in one of the following forms:
Throw(exception)
Throw exception;
Throw; // used for re-throwing an exception.
26 / 187
It is possible that a program segment has more than one condition to throw an exception. In such case we can associate more than one catch statement with a try. For eg:
Try
{
//try block;
}
catch (type 1 arg)
{
// catch block1;
}
catch (type 2 arg)
{
// catch block 2;
}
……………….
……………….
Catch (type n arg)
{
// catch block n;
}
27 / 187
A handler may decide to rethrow the exception caught without processing it. In such situation, we may simply invoke throw without any argument.
Eg: throw;
This cause the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
28 / 187
A collection of generic classes and function is called the standard template library (STL). STL components are part of c++ standard library. STL consist of three main components. They are
• Containers
• Algorithms
• Iterators
29 / 187
A container is an object that actually stores data. It is a way data is organized in memory, the STL containers are implemented by template classes and therefore can be easily customized to hold different types of data.
30 / 187
There are three major categories of containers:
• Sequence container
1) Vectors
2) Dequeue
3) list
• Associative container
1. set
2. multiset
3. map
4. multimap
• Derived container
1. stack
2. queue
3. priority queue