Wednesday, October 26, 2016

Overloading Function Template :)

When we overload a function template by defining function with same signature but with any particular type then if called function is an exact match of the normal function then it is called.
else the template function is called.
Following Program Illustrates The Concept:

#include<iostream>
using namespace std;
template<class T>
void display(T x)
{
cout<<"\nTemplate Function\n";
cout<<"Value Passed: "<<x;
}
void display(int x)
{
cout<<"\nNormal Function\n";
cout<<"Value Passed: "<<x;
}
main()
{
display(5);
display('N');
}

Output:


Template Class With Multiple Parameters and Default Data Types :)

Following Program Illustrate The Use Of Template Class With Multiple Parameters:

#include<iostream>
using namespace std;
template<class T1=int,class T2=int>
class sample
{
T1 a;
T2 b;
public:
sample(T1 x,T2 y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
cout<<"\n********************";
}
};
main()
{
sample<int,float>a(12,76.23);//passing int and float
    a.display();
sample<float,int>b(56.7,76);//passing float and int
b.display();
sample<float>c(15.7,12);//passing one type and other is default
c.display();
sample<>d('a',13);//default types used
d.display();
}

Output:


Insertion Sort

Following Program Sort The Array With Insertion Sort

Insertion Sort: In this the elements are inserted in same array by comparing it with previously inserted elements.

#include<iostream>
using namespace std;
main()
{
int a[100],n,i;
cout<<"Enter The No Of Elements in Array ";
cin>>n;
cout<<"Enter The Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
int k;
for(k=1;k<n;k++)
{ int j=k-1;    //for comparing all elements with present element
int temp=a[k];
while(j>0 && temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j]=temp;
}
cout<<"Sorted List Is ";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Special Thank To Ms.Naina For This Program

Output:


Tuesday, October 25, 2016

Dot Product Of Vectors With Template Class

By using Template Class We Can do Generic Programming that is we can avoid the writing of same code for different datatypes.
Following Program Illustrate This Concept:
#include<iostream>
using namespace std;
template<class T>
class vector
{ T v[3];
public:
vector()
{ cout<<"Enter The Components of vector\n";
for(int i=0;i<3;i++)
cin>>v[i];
}
T operator*(vector &b)
{ T sum=0;
for(int i=0;i<3;i++)
sum+=v[i]*b.v[i];
return sum;
}
void display()
{ cout<<"Components of Vector Are ";
for(int i=0;i<3;i++)
cout<<v[i]<<"\t";
cout<<"\n";
}
};
main()
{ vector<typename>v1,v2;   //type name any type for which we want to do product
v1.display();
v2.display();
cout<<"V1*V2="<<v1*v2;
}

Output:


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: