Sunday, 11 November 2012

Generic Classes in C++

//Program illustrates the working of generic classes in C++.
#include<iostream>
#include<fstream>
using namespace std;

template <class x,class y> void swap(x *a,y *b)
{
    x temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

int main()
{
    int n1=10,n2=20;
    float f1=11.11,f2=22.22;
    cout<<"\nn1 is: "<<n1<<"\nn2 is: "<<n2;
    cout<<"\nf1 is: "<<f1<<"\nn2 is: "<<f2;
    swap(&n1,&n2);
    swap(&f1,&f2);
    cout<<"\nn1 is: "<<n1<<"\nn2 is: "<<n2;
    cout<<"\nf1 is: "<<f1<<"\nn2 is: "<<f2;
    return 0;
}


Click here to download the C++ Program.

No comments:

Post a Comment