--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Mobile No : 7600044051
E-Mail : rupareleducation@gmail.com
Chapter 11
Que. Exception
Handling:
Ans. A program execution may return an abnormal
unexpected error at run time which cause the program to fail such errors are
known as exceptions. Exceptions are different from logical errors. Which occurs
due to poor understanding. Of problems and solution procedure. Exceptions are
only checked at run time only.
Following are some exceptions which
occurs during the program executions are:
1.
Divide by zero.
2.
NULL pointer assignment.
3.
Stack overflow.
4.
Low memory space.
5.
Array out of bound.
6.
Arithmetic overflow etc…
Are
such errors which return and exception in a program. The error handling
mechanism of C++ is generally refer to as a exception handling. Exception
handling is the part of ANSI/ISO. C++ provide a building language features to
detect and handle exception occurs a specially at run time. Exceptions are
classified into.
Synchronize
Exceptions and Asynchronies Exceptions.
The
synchronize exceptions are occurs during the program execution due to some
fault in the part of input data. While asynchronies exceptions occurs by events
or some external program which work inside the program execution it may occurs
also hardware failure, system failure etc… Exception handling mechanism provide
a way to handle run time error. The exception handling mechanism perform
following task. That is…
1.
Find the problem.
2.
Inform that an error has occurs.
3.
Receive the error information.
4.
Take corrective action.
Que. Exception Handling model.
Ans. When program encounter and abnormal
situation at run time when it transfer control to some other part of the
program that is… design to deal with the program and this is done by throwing
an exception. The exception handling mechanism use three blocks. That is…
(1) try (2) throw (3)
catch
Following is a
model of error handling.
try
throw
catch
The try block
must be follow immediately by the handler. The catch block is working as a
exception handler. Try block contain all the code which may occur error during
program running and may generate run time errors and if error occurred then
catch block handle error and program going
to be very smooth.
Chapter 12.
STL Standard Template Library
Que. Introduction
of Template
Ans. C++ support many kind of
datatype that may be basic datatype, derived datatype, user define datatype and
there are many requirements in which we have to write the same function with
different datatypes.
The
main benefit of object oriented programming is reusability of code which
eliminate the redundant code.
The
advantage features of C++ call template allow defining a single function for
different datatypes.
Templates
are mechanism that makes it possible to use one function or a class to handle
many different datatype.
Template
support generic programming where designs a single class or function that
operates on data of many types. Instant of creating a separate class or
function for each type. When template use with function they are known as
function template where as when use with class they are call class template.
Template
is serve as a one kind of macro. The template definition for the specific class
is replace with the require datatype.
When
template define as a parameterize template give the ability to create generic
class and pass datatype as a parameter to that class.
Que. Function Template
Ans. The function template define a general set
of operation that will be apply to various types of data it is also known as
generic function. Through a generic function a single general procedure can be
applied to different datatype in a function. Following is syntax of function
template:
Template<class
t1, classt2, ...>
return_type function_name(argument
list)
{
body of the
template function.
}
According to
above syntax: Function template is
prefix with the keyword template with the list of template parameter. This
template type argument are call generic datatype. The syntax of function
template is similar to normal function. But the variable in the function
template declare not know until a call to it.
Que. Class template
Ans. Class can also be declare to operate on
different datatype such class are call class template. It specify a generic
class which use logic that can be generalize.
For ex: The same class that maintain int type data,
float type data etc... it allow to create class template. The syntax to create
class template is as follow.
Template<class
t1, t2, ... >
Class class_name
{
Datatype t1,t2,
... ;
Body
of the class template
}
According to
above syntax: The keyword template is
use as the beginning of every declaration and definition of template class. The
parameter of the template are written after the template keyword. It will
change with each instance to call by template class. To create object using
class template use following syntax:
class_name<datatype>
object_name;
According to
above syntax: here class name is the
name of the class specification in a template class. Which has all the members
with type datatype and associated by object name.
Que. Class template with multiple parameter.
Ans. It is possible to use more than one generic
datatype in a class template. For that we have to declare a class template with
comma separated list within the template specification.
Que. Member function template.
Ans. When design any member function inside
class specification it became inline function to design any member function
outside the class. The member function of the template allow to declare a
parameterize by the type argument and it implicitly treated as a template
function.
Que. Overloading template function.
Ans. Similar to normal function template
function can also be overloaded. It may be overloaded either by other function
or its name or by other template function of the same. In overloaded template
function number of parameters and types of parameter should be different.
Que. Non Type template argument.
Ans. A template can have multiple arguments in
the template specification for a generic class. It is also possible to specify
non-type template arguments. It is addition to the type argument to another
non-type argument may be either or following.
int, string,
function_name, constant expression and buil in type.
Que. Primary and partial specialization.
Ans. When you create a class template the compiler
a create a definition based on the template arguments that you have pass.
Alternatively if all those template arguments match with an explicit argument
then compiler use the definition of explicit specialization. A partial
specialization generalization of an explicit specialization. An explicit
specialization only has a template argument list. A partial specialization has
both a template argument list and non-template argument list. At that time
compiler will generate a new definition for template argument list.
Que. Intro to STL (Standard Template Library).
Ans. STL means Standard Template Library. The
standard library is fundamental part of the C++. It provide C++ programs with a
comprehensive set of effectively and efficient implemented tools and facility
that can be use for most types of applications. Standard template library contain
mainly three components that is...
1.
Algorithms
2.
Containers
3.
Iterators(Looping)
STL is a generic
library it means container or algorithm can be operated on any datatype. You don’t
have to be define. The same algorithm for different type of elements.
1.
Algorithm:
STL provide
numbers of algorithm that can be use of any container or their types. Algorithm
library contain build in functions that perform complex algorithm on data
structure.
2.
Container:
Container
library in SIL provide containers that are use to create data structure like.
Array, linklist, tree etc...
This containers are generic they can hold elements of any datatype.
3.
Iterators:
It is used to point the container. It is act as a bridge between
containers and algorithms.