#include "conio.h"
class test
{
private:int a;
int b;
public:void getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
void operator-()
{
a=-a;
b=-b;
};
};
main()
{
test t;
t.getdata();
-t;
t.putdata();
getch();
}
Ans:- -10 -10
Discussion:-Here –(Ve) operator is overloading. And it changes the sign of a and b.
Q2)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
private:int a;
int b;
public:void getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
void operator::()
{
a=-a;
b=-b;
};
};
main()
{
test t;
t.getdata();
::t;
t.putdata();
getch();
}
Ans:-Compile time error.
Discussion:-Here :: (Scope resolution) operator is trying to overload. But it not possible to overload scope resolution operator.
Q3)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
private:int a;
int b;
public:void getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
test operator+(test t)
{
test temp;
temp.a=a+t.a;
temp.b=b+t.b;
return temp;
};
};
main()
{
test t1,t2,t3;
t1.getdata();
t2.getdata();
t3=t1+t2;
t3.putdata();
getch();
}
Ans:-20 20
Discussion:-Here binary (+) operator is overloaded. And It adding two object t 1 and t2.
No comments:
Post a Comment