Saturday, October 22, 2016

Overloading Of Input/Output Operators of C++ :)

We know that in overloading we make the operator work on user defined datatype(Objects) same as on Built in Datatypes
So the Input Operator ">>"is overloaded for working on Objects and Output Operator "<<" also overloaded.

Following Program Illustrates the Concept:

#include<iostream>
using namespace std;
class time
{   int hours;
    int minutes;
    public:
    friend istream& operator>>(istream &,time &);  //for input in objects
    friend ostream& operator<<(ostream &,time &);  // for output from objects
};
istream& operator >>(istream &c,time &b)    //istream& for use in nested form like cin>>x>>y;
{   cout<<"\nEnter Time In Hours And Minutes\n";
c>>b.hours>>b.minutes;
return c;
}
ostream& operator <<(ostream &d,time &b)
{   cout<<"\nEntered Time";
d<<"\nHours: "<<b.hours<<" Minutes: "<<b.minutes;
return d;
}
main()
{    time t,n;
cin>>t>>n;
cout<<t<<n;

}

Output:





Special Thanks To Ms.Naina For This Program

Thursday, October 13, 2016

Arithmetic Operations Using Multiple Catch Statements:)

Program To Display Arithmetic Operations On Two Numbers Using Multiple Catch Statements:
#include<iostream>
using namespace std;
void operations()
{   int a,b;
    char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator ";
cin>>c;
try
{
         if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/':

if(b==0)
{
throw float(a);
}
else
{
cout<<"Quotient is "<<a/b; break;
}
             
}
}}
catch(float)
               {
                cout<<"Divide By Zero";
  }
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}
Output:

Arithmetic Operations Using Try Catch Statement :)

Program To Display Arithmetic Operations On Two Numbers Using Try Catch :

#include<iostream>
using namespace std;
void operations()
{   int a,b;
    char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator";
cin>>c;
try
{
         if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/': cout<<"Quotient is "<<a/b; break;
}
}
}
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}

Output:


Wednesday, October 12, 2016

Exception handling (Solution To E Balagurusamy Problem 13.5)

Solution:
#include<iostream>
using namespace std;
double x,y;
void read()
{
cout<<"\nEnter Two Double Type Values Except 0\n";
cin>>x>>y;
try
{
if(x==0||y==0)
{
throw 'x';
}
} catch(char)
{
cout<<"\n0 or Character Values Not Allowed";
}
}
void divide()
{
try
{
if(y==0)
{
throw 0;
}
else
{
cout<<"\nDivision Result is "<<x/y;
}
}
catch(int)
{
cout<<"\nDivide By Zero Exception Caught";
}

}
main()
{
read();
divide();

}
Output:


Catch All Exceptions Using Single Catch Statement

Program to illustrate the catching of all exceptions using single catch statement:

#include<iostream>
using namespace std;
void test()
{
int x;
cout<<"Enter Any Positive Value Except 0\n";
cin>>x;
if(x<0)
{
throw (float)x;
}
else if(x==0)
{
throw x;
}
cout<<"\nEntered Value is "<<x;
}
main()
{
try
{
test();
}
catch(...)
{
cout<<"\nException Caught";}
cout<<"\nEnd";
}
Output:


Multiple Catch Statements (Solution To E Balagurusamy Problem 13.2)

In Multiple Catch Statements The Type of Argument is Matched with all catch statements and after a match the statement after the last catch block is executed.

Problem:
Write A Program that illustrates the application of multiple catch statements.

Solution:
#include<iostream>
using namespace std;
void test()
{
int x;
cout<<"Enter Any Positive Value Except 0\n";
cin>>x;
if(x<0)
{
throw (float)x;
}
else if(x==0)
{
throw x;
}
cout<<"\nEntered Value is "<<x;
}
main()
{
try
{
test();
}
catch(float)
{
cout<<"\nNegative Values Not Allowed";
}
catch(int)
{
cout<<"\n0 Not Allowed";
}
cout<<"\nEnd";
}
Output:


Exception Handling(Solution To E Balagurusamy Problem 13.1)

Problem:
Write a program containing a possible exception.Use a try block to throw it and a catch block to handle it properly.

Solution:

#include<iostream>
using namespace std;
main()
{
int i,fact=1;
cout<<"Enter A Positive No To Calculate Factorial\n";
cin>>i;
try
{
if(i<0)
{
throw i;
}
else
{
for(int j=1;j<=i;j++)
fact=fact*j;
cout<<"\nFactorial="<<fact;
}
}
catch(int)
{
cout<<"Number Not Valid";
}
cout<<"\nEnd";
}
Output: