0% found this document useful (0 votes)
12 views32 pages

OOPs With C - CSE2001 - Unit 4 - Exception and Template

The document outlines a course on Object Oriented Programming with C++, covering topics such as classes, objects, polymorphism, inheritance, templates, and exception handling. It provides detailed explanations of function and class templates, including syntax and examples for generic programming. Additionally, it discusses user-defined exceptions and how to create them in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views32 pages

OOPs With C - CSE2001 - Unit 4 - Exception and Template

The document outlines a course on Object Oriented Programming with C++, covering topics such as classes, objects, polymorphism, inheritance, templates, and exception handling. It provides detailed explanations of function and class templates, including syntax and examples for generic programming. Additionally, it discusses user-defined exceptions and how to create them in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

VIT Bhopal University

Bhopal-Indore Highway, Kothri Kalan, Sehore, Madhya Pradesh – 466114.

Object Oriented Programming with C++


Course Code: CSE 2001

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),

Syllabus Function Template,


Class Template,
Template with inheritance,
STL: Container, Algorithm,
Iterator; vector, list, stack, map.
Template
Definition

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.

Templates can be represented in two ways:


● Function templates
● Class templates
Function Template

● 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

Syntax of Function Template:


template < class Ttype> ret_type func_name(parameter_list)
{
// body of function.
}

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

Function Templates with Multiple Parameters


We can use more than one generic type in the template function by using the comma to separate the list.

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

#include <iostream> Overloading a Function


using namespace std; Template:
template<class X> void fun(X a, X b)
We can overload the generic
{
function means that the
std::cout << "Value of a is : " <<a<< std::endl;
} overloaded template functions
template<class X, class Y> void fun(X b ,Y c) can differ in the parameter list.
{
std::cout << "Value of b is : " <<b<< std::endl;
std::cout << "Value of c is : " <<c<< std::endl;
}
int main()
{
fun(10); Output:
fun(20,30.5); Value of a is : 10
return 0; Value of b is : 20
} Value of c is : 30.5
Function Template: Example
#include <iostream>
using namespace std; Restrictions of Generic Functions
void fun(double a) Generic functions perform the same
{ operation for all the versions of a
cout<<"value of a is : "<<a<<'\n'; function except the data type differs.
}
Let's see a simple example of an
void fun(int b) {
overloaded function which cannot be
if(b%2==0)
{
replaced by the generic function as both
cout<<"Number is even"; the functions have different
} functionalities.
else
{ NOTE: In the above example, we
cout<<"Number is odd";
overload the ordinary functions. We
} }
cannot overload the generic functions as
int main()
{
both the functions have different
fun(4.6); Output: functionalities. First one is displaying the
fun(6); value of a is : 4.6 value and the second one determines
return 0; Number is even whether the number is even or not.
}
Class Template

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

Class Template With Multiple Parameters


We can use more than one generic data type in a class template, and each generic data type is separated
by the comma.

Syntax
template<class T1, class T2, ......>
class class_name
{
// Body of the class.
}
Class Template: Example

#include <iostream> int main()


using namespace std; {
template<class T1, class T2> A<int,float> d(5,6.5);
class A d.display();
{ return 0;
T1 a; //private data members }
T2 b;
public:
A(T1 x,T2 y) //constructor
{
a = x; Output:
b = y; Values of a and b are : 5,6.5
}
void display() //member function
{
cout << "Values of a and b are : " << a<<" ,"<<b<<endl;
}
};
Class Template: C++ Program to build Simple calculator using Class template

#include <iostream>
using namespace std;

template <class T>


class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}
Class Template: C++ Program to build Simple calculator using Class template

T add() { return num1 + num2; }

T subtract() { return num1 - num2; }


OUTPUT:
T multiply() { return num1 * num2; }
Int results:
T divide() { return num1 / num2; } Numbers are: 2 and 1.
}; Addition is: 3
int main()
Subtraction is: 1
{ Product is: 2
Calculator<int> intCalc(2, 1); Division is: 2
Calculator<float> floatCalc(2.4, 1.2);
Float results:
cout << "Int results:" << endl; Numbers are: 2.4 and 1.2.
intCalc.displayResult();
Addition is: 3.6
cout << endl << "Float results:" << endl; Subtraction is: 1.2
floatCalc.displayResult(); Product is: 2.88
return 0; Division is: 2
}
Exception Handling
Exception Handling (User Defined)

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> We can define our own exceptions


#include <exception>
by inheriting and overriding
using namespace std;
exception class functionality.
class MyException : public exception
{ Here, what() is a public method
public:
const char * what () const throw ()
provided by exception class and it
{ has been overridden by all the
return "C++ Exception"; child exception classes. This
}
}; returns the cause of an exception.
int main() {
try{
MyException e ;
throw e;
}
catch(exception& e1){
cout << "MyException caught" <<endl;
cout << e1.what() <<endl;
}
Example 2: 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();
} }
}

You might also like