Unit 5 Oops
Unit 5 Oops
Example 1:
Example 1
#include <iostream>
using namespace std;
// template function
template <class T1>
void display (T1 n1)
{
cout<<n1<<"\n";
}
int main()
{
display("Program");
display("4");
display(5.6);
display('A');
return 0;
}
Output:
Program
4
5.6
A
Example 2:
// If two characters are passed to function template, character with larger ASCII
value is displayed.
#include <iostream>
using namespace std;
// template function
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1=10, i2=20;
float f1=98.55, f2=87.34;
char c1='a', c2='b';
cout << Large(i1, i2) <<" is larger." << endl;
cout << Large(f1, f2) <<" is larger." << endl;
cout << Large(c1, c2) <<" larger ASCII value.";
return 0;
}
Output:
20 is larger.
98.55 is larger.
b larger ASCII value.
#include <iostream>
using namespace std;
// template function
template <class T1,class T2>
void display (T1 n1, T2 n2)
{
cout<<n1<<"\t"<<n2;
}
int main()
{
display("Program",2);
return 0;
}
Output:
Program 2
}
template <class T>
void display(T n1)
{
cout<<n1<<endl;
}
int main()
{
display("Program",2);
display("Hello world");
return 0;
}
Output:
Program 2
Hello world
Example2
#include <iostream>
using namespace std;
// template function
template <class T1,class T2>
void display (T1 n1, T2 n2)
{
cout<<n1<<"\t"<<n2<<endl;
}
template <class T>
void display(T n1)
{
cout<<n1<<endl;
}
void display(int x)
{
cout<<"ordinary function "<<x;
}
int main()
{
display("Program",2);
display("Hello world");
display(100);
return 0;
}
Program 2
Hello world
ordinary function 100Int results:
Example 3:
#include <iostream>
using namespace std;
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
return 0;
}
Example1
#include <iostream>
using namespace std;
}
void add()
{
cout<<"Sum is "<<a+b<<endl;
}
};
int main()
{
//className<dataType> classObject;
cal<int> c(2,3);
cal<float> c1(2.5,3.9);
c.display();
c1.display();
return 0;
}
Example2
//calculator class template
#include <iostream>
using namespace std;
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;
}
int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
return 0;
}
Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2
#include <iostream>
using namespace std;
template <class T1=int,class T2=int>
class Test
{
private:
T1 a;
T2 b;
public:
Test(T1 n1, T2 n2)
{
a = n1;
b = n2;
}
void show()
{
cout<<a<<"\t"<<b<<endl;
}
};
int main()
{
Test<float,int> te1(5.6,6);
te1.show();
Test<int,char> te2(4,'r');
te2.show();
Test<> te3(4,'a');
te3.show();
return 0;
}
Output:
5.6 6
4 r
4 97
try
{
throw 4;
}
catch(int a)
{
cout<<"exception caught";
}
return 0;
}
Output:
exception caught
Example2:
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter values \n";
cin>>a>>b;
try
{
if(b==0)
throw 2;
else
cout<<"The value is"<<a/b;
}
catch(int i)
{
cout<<"Exception caught :DIVIDE BY ZERO";
}
cout<<"\nEnd of the program";
return 0;
}
Output1
Enter values
63
The value is2
End of the program
Output
Enter values
40
Exception caught :DIVIDE BY ZERO
End of the program
try
{
//statements;
}
catch(…)
{
//statements;
}
Example1:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Type any number 0 1 -1 or more than 1";
cin>>a;
try
{
if(a==0)
throw 67;
else if(a==-1)
throw 'a';
else if(a==1)
throw 5.6;
else
cout<<"The value of A "<<a;
}
catch(...)
{
cout<<"caught";
}
return 0;
}
Output:
Type any number 0 1 -1 or more than 1
1
caught
Syntax:
type function(arglist) //function with exception
{
throw(object);//throws exception
}
try
{
invoke function here
}
catch(type arg) //catches exception
{
handles exception here
}
Example 1:
#include <iostream>
using namespace std;
void divide(int x,int y)
{
if(y==0)
{
throw 0;
}
else
{
cout<<"Result"<<x/y<<endl;
}
}
int main()
{
try
{
divide(4,2);
divide(5,0);
}
catch(int i)
{
cout<<"Exception caught"<<endl;
}
return 0;
}
Result 2
Exception caught
3. Exception Specification:
It is possible to restrict a function to throw only certain specified
exception.
Syntax:
type function(arglist) throw (type-list)
{
//function body;
}
type-list specifies the type of exception that may be thrown.
Throwing any other exception will cause abnormal termination.
To prevent a function from throwing any exception from throwing any
exception,make type-list empty.
throw();
Example:
#include <iostream>
using namespace std;
// only throw ints, chars, and doubles
void f(int val) throw(int,char, double)
{
if(val==0)
throw val;
if(val==1)
throw 'a';
if(val==2)
throw 123.23;
}
int main()
{
try
{
f(0); // also, try passing 1 and 2 to f()
}
catch(int i)
{
cout << "Caught an integer\n";
}
catch(char c)
{
cout << "Caught char\n";
}
catch(double d)
{
cout << "Caught double\n";
}
return 0;
}
output
Caught an integer
4. Uncaught Exception: Terminate and Unexpected Functions
terminate():
In some cases, the exception handling mechanism fails and a call to void
terminate() is made.
This terminate() call occurs in any of the following situations.
- When terminate() is explicitly called.
- When no catch can be matched to a thrown object.
- When the stack becomes corrupted during the exception-
handling process.
- When a system defined unexpected() is called.
The function terminate() is invoked when an exception is raised and
handler is not found.
The default action for terminate is to invoke abort().
Such a default action causes immediate termination of program
execution.
unexpected()
When a function with an exception specification throws an exception that
is not listed in its exception specification, the function void unexpected()
is called.
By default, unexpected() calls the function terminate().
The terminate() function calls a function specified by the set_terminate()
function. By default, terminate calls abort() , which exits from the
program.
A terminate function cannot return to its caller, either by using return or
by throwing an exception
set_terminate():
The set_terminate() function allows the user to install a function that
defines program actions to be taken to terminate the program when a
handler for an exception cannot be found.
Example:
#include <iostream>
using namespace std;
int main()
{
try
{
throw 4.5;
}
catch(int a)
{
cout<<"exception caught";
}
return 0;
}
set_terminate:
#include <iostream>
using namespace std;
void myter()
{
cout<<"myterminate invoked";
exit(1);
}
int main()
{
set_terminate(myter);
try
{
throw 4.5;
}
catch(int a)
{
cout<<"exception caught";
}
return 0;
}
Output:
myterminate invoked Output:
myterminate invoked
unexpected()
The unexpected() function is called when a function throws an exception
not listed in its exception specification.
The program calls unexpected() which calls any user-defined function
registered by set_unexpected.
If no function is registered with set_unexpected,the unexpected() function
then invokes the terminate() function.
set_unexpected()
The function set_unexpected() lets the user to install a function that
defines the program actions to be taken when a function throws an
exception not listed in its exception specification.
The actions are defined in unexpected_fun() library function.
By default, an unexpected exception causes unexpected() to be called
,which in turn calls unexpected_fun
The unexpected_func() can invoke abort(),exit() or terminate() functions.
Uncaught exception-cont: