-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplates_and_exception.cpp
51 lines (49 loc) · 1.24 KB
/
Templates_and_exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*Problem1 : Write a program to find the square root of given number using template.
Check all possible exceptions and handle those exceptions using try and catch block.*/
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
template <typename T> //class template
class square_root
{
public:
void input();
void display(int a);
};
template <typename T>
void square_root<T>::display(int a)
{
try //exception handling constructs
{
if (a >= 0)
cout << "Square root of the entered number is : " << sqrt(a) << endl;
else
throw(a);
}
catch (int a)
{
int x = abs(a);
cout << "Square root of the entered number is : " << sqrt(x) << "i" << endl;
}
};
int main()
{
long double a;
char b;
bool run = true;
while (run)
{
square_root<float> s1;
cout << "\nEnter the number to find square root: ";
cin >> a;
cout << "-------------------------------------------" << endl;
s1.display(a);
cout << "\nDo you still want to calculate?[Y/N]" << endl;
cin >> b;
if (b == 'N' || b == 'n')
{
run = false;
}
}
}