0% found this document useful (0 votes)
3 views

Notes Exception & Templates

The document discusses type conversion in C++, detailing three types: basic-to-class, class-to-basic, and class-to-class conversion, with examples for each. It also covers handling uncaught exceptions using methods like terminate(), set_terminate(), and catch(…). Additionally, it introduces templates in C++ for generic programming, explaining function and class templates with examples demonstrating their usage.

Uploaded by

Abhishek Rath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Notes Exception & Templates

The document discusses type conversion in C++, detailing three types: basic-to-class, class-to-basic, and class-to-class conversion, with examples for each. It also covers handling uncaught exceptions using methods like terminate(), set_terminate(), and catch(…). Additionally, it introduces templates in C++ for generic programming, explaining function and class templates with examples demonstrating their usage.

Uploaded by

Abhishek Rath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Type Conversion

(Conversion involving object of a class)


i.e the statements of the form:
objectname=value/variable;
variable=objectname;
object1=object2;

All contain an object used in assignment (= operation) for which we need to


write codes/ type conversion routines.
Type conversion is of 3 types
1) Basic-to-class conversion
2) Class-to-basic conversion
3) Class-to-class conversion
Basic-to-class conversion
Happens for the assignment: objectname=value/variable;
Here the R.H.S has a basic/predefined data type (variable/value) which is
copied to a class data type (object)
Here the class should contain a parametrized constructor with 1 argument as
basic data type.
Ex:
class A
{
int a,b;
Public:
A(){ a=20,b=30;}
A(int k){ a=b=k;}
void show(){ cout<<a<<b;}
};
void main()
{
A obj;
obj=50; // 50 is passed to parametrized constructor
obj.show(); //Result 50 50
}
Class-to-basic conversion
Happens for the assignment: variable=objectname;
Here the R.H.S has a class data type (object) which is copied to a basic data
type (variable)
Here the class should contain an operator function called typecast operator:
operator basic_data_type()
{
return value;
}
Ex:
class A
{
int a,b;
Public:
A(){ a=20,b=30;}
operator int()
{
return (a+b);
}
};
void main()
{
A obj;
int k=10;
k=obj;
cout<<k; // output 50 taken from operator function
}
Class-to-class conversion
Happens for the statement
obj1=obj2;
where obj1 and obj2 are objects of two different classes.
Let A is the classname for obj1 (called destination class)
And B= classname for obj2 (called source class)
B should contain an operator function and A should contain parametrized
constructor.
Example:
(Rupee to Dollar conversion)
class Dollar
{
float d;
public:
Dollar(){ d=0.0f;}
Dollar(float r){ d=r/85.4;}
void show()
{
cout<<”No of Dollars “<<d;
}
};
Class Rupee
{
float r;
Public:
Rupee(){ cout<<”Input rupees”; cin>>r;}
operator Dollar()
{
return Dollar(r);
}
};
Void main()
{
Rupee ob1;
Dollar ob2;
ob2=ob1;//class-to-class conversion
ob2.show();
}
Handling Uncaught Exceptions
3 methods to handle unknown/uncaught exceptions
1. terminate ()
2. set_terminate()
3. catch(…)
terminate()
 Method to kill any uncaught exception. (for which no catch block is found)
 Usually kept at end of program ( before } of main())
Example: terminate();
set_terminate()
 Method which calls another method for execution when any unknown/uncaught
exception happens.
Example
void myhandler()
{
cout << "Inside new terminate handler\n";
}
int main()
{
// set new terminate handler
set_terminate(myhandler); //myhandler is the function to be called
try {
cout << "Inside try block\n";
throw 100;
}
catch (char a) // won't catch an int exception
{
cout << "Inside catch block\n";
}
return 0;
}

catch(…)
Called as UNIVERSAL CATCH HANDLER which can handle any type of UNHANDLED
exception. Usually kept after all catch() handlers.
Example
Main()
{
try
{
throw 10;
throw ‘a’;
throw 2.5;
}
catch(int)
{
cout<<”exception caught”;
}
catch(…)
{
cout<<”all exceptions caught”;
}
}
Output:
Exception caught
All exceptions caught
All exceptions caught

Here throw 10; moves to catch(int) whereas for other throw statements respective catch() are
missing, so it calls catch(…) on remaining occasions.

Templates
Templates in C++ is an interesting feature that is used for generic programming and
templates in c++ is defined as a blueprint or formula for creating a generic class or a function.

Simply put, you can create a single function or single class to work with different data types
using templates.

Hence a class can create similar set of classes (family of classes)- generic classes

And a function can create a similar set of functions (family of functions)- generic functions

Accordingly, There are two types of templates in C++

 Function template
 Class templates

Function template consists of multiple template functions (generic functions), similarly


class template consists of multiple template classes (generic classes)
Function Templates:
Example:
In this example A template data type (also called generic data type) T is created by user which
is replaced by int, char with values (3,7) and (‘g’, ‘e’) for comparison and produces result:
7 and g respectively.
Overloading in function templates:
Example:
template <class T>
void swap(T a, T b)
{
T c; c=a; a=b; b=c;
cout<<”Results”<<a<<b;
}
void swap(int a, int b)
{
int c; c=a; a=b; b=c;
cout<<”Results”<<a<<b;
}
void main()
{
swap(10,20);// calls swap(int, int)
swap(‘a’,’b’);// calls swap(char, char) replaces T by char
swap(1.5,2.5);// calls swap(float, float) replaces T by float
}
In this example, function template consists of 2 functions, swap(T,T) and swap(int,int).
Hence overloading happens between them.
Class Template
template <class T>
class Test
{
private:
// A variable (answer) of type T so that it can store results of
various types.
T answer;

public:
// Constructor of Test class.
Test(T n) : answer(n)
{
cout << "Inside constructor" << endl;
}

T getNumber()
{
return answer;
}
};

// Main function
int main()
{
// Creating an object with an integer type.
Test<int> numberInt(60);

// Creating an object with double type.


Test<double> numberDouble(17.27);

// Calling the class method getNumber with different data types:


cout << "Integer Number is: " << numberInt.getNumber() << endl;
cout << "Double Number = " << numberDouble.getNumber() << endl;

return 0;
}

Output:

Inside constructor
Inside constructor
Integer Number is: 60
Double Number = 17.27

In this example generic data type T is replaced by int and double inputs to create multiple
copies of the class with T replaced by int and double respectively.

You might also like