Você está na página 1de 27

SRI RAMAKRISHNA

INSTITUTE OF TECHNOLOGY

Programming and Data


Structures - II
UNIT III
Abstract class Exception handling - Standard libraries - Generic Programming templates class template - function template STL containers iterators
function adaptors allocators - Parameterizing the class - File handling concepts.

1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
An abstract class is one is not used to create objects.
A abstract class is designed only to act as a base class.
Example
class vehicle
{
data-type -1;
public :
virtual void spec()=0 //Pure virtual function
};
class LMV : public vehicle
{
public :
void spec();{

.. }

}
1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
class HMV : public vehicle
{
public :
void spec();{

.. }

1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
An exception is a problem that arises during the execution of a program.
A C++ exception is a response to an exceptional circumstance that arises
while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program
to another. C++ exception handling is built upon three keywords: try,
catch, and throw.
throw: A program throws an exception when a problem shows up. This is done
using a throw keyword.
catch: A program catches an exception with an exception handler at the place in a
program where you want to handle the problem. The catch keyword indicates the
catching of an exception.
try: A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
try
{
// protected code
}
catch( ExceptionName e1 )
{
// catch block
}
catch( ExceptionName e2 )
{
// catch block
}
catch( ExceptionName eN )
{
// catch block
}
1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Try - Catch
Try Block
Detects and throws an exception

Catch Block
Catches and handles the exception

1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Try - Catch - Throw

Throw Point
Function that causes an exception
Try Block
Invokes a function that contains an
exception
Catch Block
Catches and handles the exception

1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
#include<iostream.h>
main()
{
int a, b;
cout<<Enter the values of a and b;
cin>>a; cin >> b;
int x = a-b;
try
{
if(x!=0)
{
cout<<Result(a/x)= << a/x << \n;
}
else
{
throw(x);
}
}
1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
catch (int i)
{
cout<<Exception caught: Divide By zero;
}
cout<<end;
}
OUTPUT
Enter a value of a and b : 10 10
Exception caught: Divide By zero
1/7/17

R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
# include<iostream.h>
void test(int x)
{
try
{
if(x==1) throw x;
else
if (x==0) throw x;
else
if(x==-1) throw 1.0;
cout<< End of try Block\n;
}
1/7/17

R.Nagendran

10

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
catch(char c)
{
cout<<Caught a character \n;
}
catch(int m)
{
cout<<Caught a integer \n;
}
catch(float d)
{
cout<<Caught a float \n;
}
cout<<End of Caught Block \n;
}

1/7/17

R.Nagendran

11

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
main()
{
cout<< Testing Multiple catches;
test(1);
test(0);
test(-1);
test(2)
}
OUTPUT
Testing Multiple catches
Caught an integer
End of Caught Block
Caught a character
End of Caught Block
Caught a float
End of Caught Block
End of try Block
End of Caught Block
1/7/17

R.Nagendran

12

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
class error
{
int errcode;
char *errdesc;
public:
error(int c , char *d)
{
errcode =c;
errdesc =new char[strlen(d)];
strcpy(errdesc,d);
}
void err_display(void)
{
cout<<errcode<<errdesc;
}
};
main()
{
1/7/17

try
{
cout<<Press any key to throw exception;
getch();
throw error(99,Test Exception);
}
Catch(error e)
{
cout<<Exception Caught;
e.err_display();
}
getch();
}
OUTPUT
Press any key to throw exception
Exception Caught
99 Test Exception
R.Nagendran

13

SRI RAMAKRISHNA
TEMPLATE

INSTITUTE OF TECHNOLOGY

Template is one of the concept added to C++.


It is a new concept which enable us to define generic classes and functions and thus provides support for generic
programming.
Generic Programming is an approach where generic types are used as parameters in algorithms so that they work for a
variety of suitable data types and data structures.
General Syntax
template<class T>
class classname
{
//.
// class member specification
// with anonymous type T wherever appropriate
// .
};
1/7/17

R.Nagendran

14

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
class vector
class vector

int *v;

T *v;

int size;

int size;
public:

public:

vector(int m)

vector(int m)

{
v=new T[size=m];

for (int i=0;i<size; i++)

v=new int[size=m];

v[i]=0;

for (int i=0;i<size; i++)


}

v[i]=0;
}
1/7/17

R.Nagendran

15

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
vector (int *a)

vector (T *a)

for(int i=0;i<size; i++)

for(int i=0;i<size; i++)

v[i]=a[i];

v[i]=a[i];
}

}
int operator *(vector &y)

T operator *(vector &y)


{

T sum =0;

int sum =0;

for(int i=0;i<size; i++)


sum += this -> v[i] * y.v[i];

for(int i=0;i<size; i++)

return sum;

sum += this -> v[i] * y.v[i];


return sum;

}
};

}
};
1/7/17

R.Nagendran

16

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
main()
{

main()

int x[3] = {1,2,3};

{
int x[3] = {1,2,3};

int y[3] = { 4,5,6];

int y[3] = { 4,5,6];


vector <int>v1;

vector v1(3);

vector <int>v2;

vector v2(3);

v1 =x;

v1 =x;

v2 =y;
int R = v1 * v2;

v2 =y;
int R = v1 * v2;

cout<<R = << R;
}

cout<<R = << R;
}
1/7/17

R.Nagendran

17

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Function Template
template<class T>
returntype
functionname(arguments of type
T)
{
//..
//Body of function with type T
}
1/7/17

R.Nagendran

18

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
#include<iostream.h>
template<class T>
void swap( T &x, T &y)
{
T temp =x;
x=y;
y=temp;
}
1/7/17

R.Nagendran

19

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
void fun(int m, int n, float a, float b)
{
cout<<m and n before swap: << m << << n <<\n;
swap(m,n);
cout<<m and n after swap: << m << << n <<\n;
cout<<a and b before swap: << a << << b<<\n;
swap(a,b);
cout<<a and b after swap: << a << << b <<\n;
}
main()
{
fun(100,200,11.22,33.44);
}

1/7/17

R.Nagendran

20

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Standard Libraries
Utility Library
Header file

Purpose

<cstdlib>

For general purpose utilities such as random number


generations, sorting and searching

<typeinfo>

For runtime type information utilities

<ctime>

For date and time utility functions

Dynamic memory management


Header file

Purpose

<new>

For allocating the lower memory management

<memory>

For allocating the higher level memory

1/7/17

R.Nagendran

21

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Error Handling
Header file

Purpose

<string>

For handling string class functionalities

Container Library

1/7/17

Header file

Purpose

<array>

For using array container

<vector>

For using vector container

<list>

For using list container

<dequeue>

For using the doubly ended queue container

<algorithm>

The algorithm operates on container

<iterator>

For using container iterator

R.Nagendran

22

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
STANDARD TEMPLATE LIBRARY
Algorithm 1

Iterator
1

Algorithm 2

obje
ct1

obje
ct2

Iterator
2

obje
ct3

Algorithm 3

1/7/17

Iterator
3
R.Nagendran

23

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
STL contains 3 key components
Containers
Algorithms
Iterators
Algorithms employ iterators to perform operations storedmin containers.
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.
1/7/17

R.Nagendran

24

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
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.
An algorithm is a procedure that is used to process the data contained in
the containers. The STL includes many different kinds of algorithms to
provide support to tasks such as initializing, searching, copying, sorting
and merging. Algorithms are implemented by template functions.
An iterator is an object(like a pointer) that points to an element in a
container. We can use iterators to move through the contents of
containers. Iterators are handled just like pointers. We can increment
and decrement the iterator.
1/7/17

R.Nagendran

25

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Containers

Sequence Containers

Associative Containers

Derived Containers

Vector

set

Stack

deque

multiset

queue

list

map

Priority_queue

multimap

1/7/17

R.Nagendran

26

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

1/7/17

R.Nagendran

27

Você também pode gostar