Search This Blog

Monday, 12 November 2012

Operator overloading using friend

#include<iostream.h>
#include<conio.h>
class MARK{
private:
int mark;
public:
MARK(){}
MARK(int x){mark=x;}
void getMARK()
{
cout<<"Get Mark";
cin>>mark;
}
void putMark()
{
cout<<"Mark="<<mark<<"\n";
}
friend MARK operator+(MARK t1.MARK t2);
friend MARK operator++(MARK &t);
friend MARK operator+(int x, MARK t);
};
MARK operator+(MARK t1,MARK t2)
{
MARK temp;
temp.mark=t1.mark+t2.mark;
return temp;
}
MARK operator++(MARK &t)
{
t.mark++;
return t;
}
void main()
{
MARK p(5),q(3),r;
r=p+q;
r.putMark();
++r;
r.putMark();
getch();
}

Output:
mark=8
mark=9


No comments:

Post a Comment