Tuesday, July 12, 2011

Operator overloading << >>

#include <iostream>
#include <cstring>

using namespace std;

class Base
{
char strVal[100];

public:
Base(){ strcpy(strVal,"");}
Base(char *val){ strcpy(strVal,val);}

~Base(){*strVal = '\0';}

friend istream& operator >>(istream &is,Base &obj);
friend ostream& operator <<(ostream &os,const Base &obj);
};

istream& operator >>(istream &is,Base &obj)
{
is>>obj.strVal;
return is;
}

ostream& operator <<(ostream &os,const Base &obj)
{
os<<obj.strVal;
return os;
}
void main()
{
Base b;
cin>>b;
cout<<"Printing the value\n";
cout<<b<<endl;
}