OOPs With C - CSE2001 - Unit 4 - Exception and Template
OOPs With C - CSE2001 - Unit 4 - Exception and Template
By:
Dr. Ramraj Dangi
● Introduction to object oriented
approach
● Classes and objects
Chapters ● Polymorphism and Inheritance
● Exception handling and Templates
● IOstreams and Files
Exception handling (user-defined
exception),
A C++ template is a powerful feature added to C++. It allows you to define the generic classes
and generic functions and thus provides support for generic programming.
Generic programming is a technique where generic types are used as parameters in algorithms so
that they can work for a variety of data types.
● Generic functions use the concept of a function template. Generic functions define a set of
operations that can be applied to the various types of data.
● The type of the data that the function will operate on depends on the type of the data passed
as a parameter.
● A Generic function is created by using the keyword template. The template defines what
function will do.
● For example, if we have an add() function, we can create versions of the add function for
adding the int, float or double type values.
Function Template
Where Ttype: It is a placeholder name for a data type used by the function. It is used within the
function definition. It is only a placeholder that the compiler will automatically replace this
placeholder with the actual data type.
class: A class keyword is used to specify a generic type in a template declaration.
Example:
#include<iostream>
using namespace std;
int Big(int a, int b)
{
if(a>b)
return a;
else
return b;
}
double Big(double a, double b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
cout<<Big(10,20)<<endl; //20
cout<<Big(10.5,11.7)<<endl; //11.7
return 0;
}
Example: With Template
#include<iostream>
using namespace std;
template <class T>
T Big(T a, T b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
cout<<Big(10,20)<<endl; //20
cout<<Big(10.5,11.7)<<endl; //11.7
return 0;
}
Function Template: Example
#include <iostream>
using namespace std;
template<class T> T add(T &a, T &b)
{
T result = a+b;
return result;
}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2; Output:
cout<<"Addition of i and j is :"<<add(i,j)<<endl; Addition of i and j is :5
cout<<"Addition of m and n is :"<<add(m,n); Addition of m and n is :3.5
return 0;
}
Function Template with Multiple Parameters
Syntax:
template<class T1, class T2,.....>
return_type function_name (arguments of type T1, T2....)
{
// body of function.
}
Example: With Multiple parameters
#include<iostream>
using namespace std;
template <class T, class X>
X Big(T a, X b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
cout<<Big(10,20.5)<<endl;
cout<<Big(10.5,11.7)<<endl;
return 0;
}
Function Template: Example
#include <iostream>
using namespace std;
template<class X,class Y>
void fun(X a,Y b)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(15,12.3);
Output:
return 0; Value of a is : 15
} Value of b is : 12.3
Function Template: Example
Class Template can also be defined similarly to the Function Template. When a class uses the concept of
Template, then the class is known as generic class.
Syntax
template<class Ttype>
class class_name
{ ….
}
Ttype is a placeholder name which will be determined when the class is instantiated. We can define more than
one generic data type using a comma-separated list. The Ttype can be used inside the class body.
Now, we create an instance of a class
class_name<type> ob;
class_name: It is the name of the class.
type: It is the type of the data that the class is operating on.
ob: It is the name of the object.
Class Template: Example
#include <iostream>
using namespace std;
template<class T>
class A
{
public:
T num1 = 5;
T num2 = 6;
void add()
{
cout << "Addition of num1 and num2 : " << num1+num2<<endl;
}
};
int main()
{
Output:
A<int> d;
d.add(); Addition of num1 and num2 : 11
return 0;
}
Class Template with Multiple Parameters
Syntax
template<class T1, class T2, ......>
class class_name
{
// Body of the class.
}
Class Template: Example
#include <iostream>
using namespace std;
There maybe situations where you want to generate some user/program specific exceptions
which are not pre-defined in C++. In such cases C++ provided us with the mechanism to create
our own exceptions by inheriting the exception class in C++ and overriding its functionality
according to our needs.
Example 1: User Defined Exceptions
#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception
{
public:
const char * what() const throw()
{
return "Attempted to divide by zero and infinity form\n";
}
};
int main()
{
try else
{ {
int x, y;
cout << "x / y = " << x/y << endl;
cout << "Enter the two numbers : \n";
cin >> x >> y; }
if (y == 0) }
{ catch(exception& e)
MyException z; {
throw z; cout << e.what();
} }
}