Wednesday, July 20, 2016
Saturday, April 23, 2016
Monday, February 29, 2016
Sunday, February 14, 2016
- Sunday, February 14, 2016
- Unknown
- No comments
To Show exception handling.
//Try block throwing exception
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter value of A and B"<<endl;
cin>>a>>b;
try
{
if(b==0)
throw b;
else
cout<<"Result="<<(float)a/b<<endl;
}
catch(int i)
{
cout<<"Divide by zero exception: B="<<i<<endl;
}
cout<<"END"<<endl;
getch();
return 0;
}
#include <conio.h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter value of A and B"<<endl;
cin>>a>>b;
try
{
if(b==0)
throw b;
else
cout<<"Result="<<(float)a/b<<endl;
}
catch(int i)
{
cout<<"Divide by zero exception: B="<<i<<endl;
}
cout<<"END"<<endl;
getch();
return 0;
}
Output:-
First Execution
Second Execution
- Sunday, February 14, 2016
- Unknown
- No comments
To sort 10 number in asscending order using function template.
Program:-
#include <conio.h>
using namespace std;
template <class T>
void Sort(T a[],int n)
{
T hold;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
}
}
int main()
{
int i,j;
int x[10]={1,8,7,4,6,7,4,2,5,7};
float y[10]={0.2,0.5,1.1,2.1,0.33,5.6,9.7,1.59,8.8,0.001};
Sort(x,10);
Sort(y,10);
cout<<"------Sorted X array-----------"<<endl;
for(i=0;i<10;i++)
{
cout<<x[i]<<endl;
}
cout<<"------Sorted Y array-----------"<<endl;
for(j=0;j<10;j++)
{
cout<<y[j]<<endl;
}
cout<<"!!!!!!!!!!!Thank You!!!!!!!!!!!"<<endl;
getch();
return 0;
}
Output:-
Monday, February 8, 2016
- Monday, February 08, 2016
- Unknown
- No comments
//Adding two number using class template...
#include <iostream>#include<conio.h>
using namespace std;
template <class T>
class mytemp
{
private:
T a,b,c;
public:
void getdata()
{
cout<<"Enter first number:";
cin>>a;
cout<<"Enter Second number:";
cin>>b;
cout<<endl;
}
void total()
{
c=a+b;
cout<<"Total values is:"<<c<<endl;
cout<<endl;
}
};
int main()
{
mytemp<int>obj1;
mytemp<float>obj2;
cout<<"Enter two integer:"<<endl;
obj1.getdata();
obj1.total();
cout<<"Enter two float:"<<endl;
obj2.getdata();
obj2.total();
cout<<"********Thank You********"<<endl;
getch();
return 0;
}
OutPut
- Monday, February 08, 2016
- Unknown
- No comments
// Program to Swap Two Numbers Using Function Template.
#include <iostream>
#include<conio.h>>
using namespace std;
template <class T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main()
{
int i1=8, i2=2;
float f1=7.1, f2=5.2;
char c1='I', c2='T';
cout<<"Before Swaping data to function template."<<endl;
cout<<"i1 = "<<i1<<"\ni2="<<i2;
Swap(i1, i2);
cout<<endl<<"After swaping data to function template."<<endl;
cout<<"i1 = "<<i1<<"\ni2="<<i2;
cout<<endl<<"Before swaping data to function template."<<endl;
cout<<"\nf1 = "<<f1<<"\nf2="<<f2;
Swap(f1, f2);
cout<<endl<<"After swaping data to function template."<<endl;
cout<<"\nf1 = "<<f1<<"\nf2="<<f2;
cout<<endl<<"Before swaping data to function template."<<endl;
cout<<"\nc1 = "<<c1<<"\nc2="<<c2;
Swap(c1, c2);
cout<<endl<<"After swaping data to function template."<<endl;
cout<<"\nc1 = "<<c1<<"\nc2="<<c2;
getch();
return 0;
}
OutPut
Subscribe to:
Posts (Atom)