#include<iostream.h>
#include<conio.h>
class test
{
private:
int a;
public:
{
a=x;
}
void operator ++()
{
a++;
}
void display()
{
cout<<"\n Answer a= "<<a;
}
}
main()
{
clrscr();
test t1;
int p;
cout<<"Enter p=";
cin>>p;
t1.getdata(p);
++t1;
++t1;
++t1;
t1.display();
getch();
}
--------------------------------------------------------------------------
In C++ the overloading principle applies not only to functions, but to operators too. That is, of operators can be extended to work not just with built-in types but also classes. A programmer can provide his or her own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used on objects of that class. Is operator overloading really useful in real world implementations? It certainlly can be, making it very easy to write code that feels natural (we'll see some examples soon). On the other hand, operator overloading, like any advanced C++ feature, makes the language more complicated. In addition, operators tend to have very specific meaning, and most programmers don't expect operators to do a lot of work, so overloading operators can be abused to make code unreadable. But we won't do that.
No comments:
Post a Comment