//Write a program to overload the operator < and >such that it compares two strings
//and returns the appropriate result .the string should be read and printed using the
//overlloaded stream operator
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class A{
public:
char s1[100],s2[100];
public:
int operator <(A &obj){
int n=strcmp(s1,obj.s2);
if(n<0) return 1;
else return 0;
}
int operator >(A &obj){
int n=strcmp(s1,obj.s2);
if(n>0) return 1;
else return 0;
}
friend istream& operator >> (istream &in,A &obj){
in>>obj.s1;
in>>obj.s2;
return in;
}
friend ostream& operator << (ostream &out,int n){
out<<n;
return out;
}
};
int main()
{
A a;
cin>>a;
cout<<(a<a)<<endl;
cout<<(a>a)<<endl;
return 0;
}
overloading less than(”) operator April 28, 2010
Operators that we can’t overload.Why? February 12, 2010
[..from Bruce Eckel c++]
There are certain operators that cannot be overloaded.The general reason for the restriction is safety.If these operators were overloadable ,it would somehow jeopardizez or break safety mechanisms,make things harder ,or confuse existing practise. (more…)



Recent Comments