prowessapps.in Exception and Templates Programs in C++

#include< iostream>
using namespace std ;
class Test {
 public :
 void add(int a , int b) {
  if(b==0) {
   throw "Can't Divide by Zero ";
  }
  else {
   int c= a/b;
  cout<< c<< endl;
  }
 }
};
int main() {
 Test t;
 int x,y;
 cout<<"Enter two number ";
 cin>>x>>y;
 try { 
    t.add(x,y);
  }
 catch(const char *a) {
  cout<< a<< endl;
  }
return 0;
}
OUTPUT
Enter two number 5 0
Can't Divide by Zero

#include< iostream>
using namespace std;
class Test { 
 public :
 void show() {
 cout<<"Wrong input ";
 }
};
class Demo {
 public :
 void disp(int a, int b) {
  if(b==0 && a==0) {
    throw b;
  }
 else if(b < 0) {
  throw "2nd variable must be +ve ";
 }
 else if(a < 0) {
  Test t;
  throw t;
 }
 else {
  cout<<"Fair = "<< a<<"."<< b;
 }
 }  
};
int main() {
Demo d;
int x,y;
cout<<"Enter Fair ";
cin>>x>>y;
 try {
  d.disp(x,y);
 }
 catch(int i) { 
  cout<<"Fair can't be = " << i;
 }
 catch(const char *a) {
  cout<< a;
 }
 catch(Test t) {
  t.show();
 }
 return 0;
}

OUTPUT
Enter Fair 0 0
Fair can't be = 0

#include< iostream>
using namespace std;
class Test {
 public:
 void show() {
  try {
   throw "hello";
  }
  catch (const char*){
   cout <<"Caught IN Function\n";
   throw; 
   }
}
};
int main(){
 Test t;
 try{
  t.show();
  }
 catch(const char*) {
  cout <<"Caught IN Main\n";
 }
 return 0;
}
OUTPUT
Caught IN Function
Caught IN Main

#include< iostream>
using namespace std;
class Test {
 public :
 void find(int x) {
  int arr[5] = {1,2,3,4,5};
  if(x>=5){
   throw "Index out of Bounds ";
  }
  cout<<"Element= "<< arr[x]<< endl;
 }
};
int main() {
 Test t;
 int x;
 cout<<"Enter Index no. to search ";
 cin>>x;
 try{
  t.find(x);
 }
 catch(const char *str) {
  cout<< str;
 }
 return 0;
}
OUTPUT
Enter Index no. to search 6
Index out of Bounds

#include< iostream> 
using namespace std; 
class Time { 
 int hr, min, sec; 
 public:  
 void input() { 
  cout<<"Enter Hour ";  
  cin>>hr;  
  if(hr >12 || hr < 0){   
   throw "Invalid Input";  
  }  
  cout<<"Enter Minute";  
  cin>>min;  
  if(hr >60 || hr < 0){   
   throw "Invalid Input";  
  }  
  cout<<"Enter Second ";  
  cin>>sec;  
  if(sec >60 || sec < 0){   
   throw "Invalid Input";  
  }      
 } 
 void disp(){ 
  cout<< hr<<":"<< min<<":"<< sec;  
 } 
}; 
int main() {  
 Time t;  
 try {   
  t.input();   
  t.disp();  
 }  
 catch(const char* st){   
  cerr<< st;  
 }  
 return 0; 
}
OUTPUT
Enter Hour 13
Invalid Input

#include< iostream>
using namespace std;
template< class T>
class Test {
 public :
 void add(T a, T b) {
  T c;
  c = a+b;
  cout<<"Sum = "<< c<< endl;
 }
};
int main() {
 Test< double > t;
 t.add(5,6);
 t.add(45.32,34.34);
 return 0;
 }
OUTPUT
Sum = 11
Sum = 79.66

#include
using namespace std;
template 
class Test{
 public:
 T arr[5];
 void input() {
   T x;
  cout<<"Enter 5 Elements ";
  for(int i =0; i<5; i++) {
  
   cin>>x;
   arr[i] = x;
  }
 }
 void search() {
  T a;
  int flag = 0;
  int i=0;
  cout<<"Enter Element to search ";
  cin>>a;
  for(i = 0; i<5; i++) {
   if(arr[i]==a) {
    flag = 1;
    break;
   }
  }
 if(flag ==1 ) {
  cout<<"Found At "<< i<< endl;
  }
 else {
  cout<<"Element NOT found "<< endl;
  }
 }
 void sum() {
  T s =0;
  for(int i=0; i<5; i++) {
   s = s+ arr[i];
  }
  cout<<"Sum = "<< s<< endl;
 }
 void largest() {
 T max= arr[0];
 for(int i = 0;i<5; i++) {
  if(max< arr[i]) {
   max = arr[i];
  }
 }
 cout<<"Maximum = "<< max<< endl;
}
};
int main() {
 Test t;
 t.input();
 t.search();
 t.sum();
 t.largest();
 return 0;
}

OUTPUT
Enter 5 Elements 1 4 8 6 4
Enter Element to Search 5
Element NOT found
Sum = 23
Maximum = 8


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved