UNIT 5 TEMPLATES AND EXCEPTION HANDLING
Function Templates and Class Templates –
Name spaces – Standard Template Library -
Casting – Exception Handling – case study.
CASE STUDY
• Write a C++ program to calculate the percentage of a student using
multi-level inheritance. Accept the marks of three subjects in base
class. A class will derived from the above mentioned class which
includes a function to find the total marks obtained and another
class derived from this class which calculates and displays the
percentage of student.
Multilevel inheritance represents a type of inheritance when a
derived class is a base class for another class.
Following program calculates the percentage of a student using
multi-level inheritance.
Type Conversion in
C++a conversion from one type to another.
A type cast is basically
There are two types of type conversion:
Implicit Type Conversion
Also known as ‘automatic type conversion’.
Done by the compiler on its own, without any external trigger from the user.
Generally takes place when in an expression more than one data type is
present. In such condition type conversion (type promotion) takes place to
avoid lose of data.
All the data types of the variables are upgraded to the data type of the
variable with largest data type.
bool -> char -> short int -> int -> unsigned int
-> long -> unsigned -> long long -> float ->
double -> long double
It is possible for implicit conversions to lose information, signs can be lost
(when signed is implicitly converted to unsigned), and overflow can occur
(when long long is implicitly converted to float).
// An example of implicit conversion
#include <iostream>
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float Output:
float z = x + 1.0;
x = 107
cout << "x = " << x << endl y = a
<< "y = " << y << endl
<< "z = " << z << endl; z = 108
return 0;
}
Explicit Type Conversion: This process is also called type casting and it
is user-defined. Here the user can typecast the result to make it of a particular data
type. In C++, it can be done by two ways: Converting by assignment and
Conversion using Cast operator
Converting by assignment: This is done by explicitly defining the required
type in front of the expression in parenthesis. This can be also considered as
forceful casting.
where type indicates the data type
Syntax: (type) expression to which the final result is converted.
// C++ program to demonstrate
// explicit type casting
#include <iostream>
using namespace std;
int main()
{
Output:
double x = 1.2; Sum = 2
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0;
}
Conversion using Cast operator:
A Cast operator is an unary operator which forces one data type to be
converted into another data type.
C++ supports four types of casting:
Static Cast
Dynamic Cast
Const Cast
Reinterpret Cast
#include <iostream>
using namespace std; Output:
int main() 3
{
float f = 3.5;
// using cast operator
int b = static_cast<int>(f);
cout << b;
}
Static Cast:
This is the simplest type of cast which can be used.
It is a compile time cast.
It does things like implicit conversions between types (such as int to float, or
pointer toSyntax : it can also call explicit conversion functions (or implicit
void*), and
ones). static_cast < new_type > ( expressio
n)
Dynamic casting is, primarily, used to safely downcast; i.e., cast a base class pointer
(or reference) to a derived class pointer (or reference). It can also be used for
upcasting; i.e., casting a derived class pointer (or reference) to a base class pointer
(or reference).
Dynamic casting checks consistency at runtime; hence, it is slower than static cast.
const_cast is one of the type casting operators. It is used to change the constant
value of any object or we can say it is used to remove the constant nature of any
object.
Syntax: const_cast<type name>(expression)
reinterpret_cast
It is a type of casting operator used in C++.
It is used to convert one pointer to another pointer of any type, no matter either the class is
related to each other or not.
It does not check if the pointer type and data pointed by the pointer is same or not.
Syntax:
data_type *var_name = reinterpret_cast <data_type*>(pointer_variable);
C++ Standard Template Libraries (STL)
STL was developed by Alexander Stepanov and Meng
Lee at Hewlett-Packard Lab as proof-of-concept for so-
called generic programming.
It was released in 1994 and subsequently adopted into
the C++98.
The Standard Template Library (STL) is a set of
C++ template classes to provide common
programming data structures and functions such as
lists, stacks, arrays, etc.
It is a library of container classes, algorithms, and
iterators.
It is a generalized library and so, its components
are parameterized.
STL has four components
•Algorithms
•Containers
•Functions
•Iterators
Mutating Algorithms:
These algorithms are
modifying algorithms that are designed
to work on the container elements and
perform operations like shuffle, rotation,
changing the order and more.
Non-mutating Algorithms:
These algorithms do not change the
order of elements. They use iterators for
a particular operation