Thursday, March 31, 2011

Operator Overloding sample program in C++

/* Write a function, fn(), to increment the values of two variables of type int that are passed by reference.
*/

# include "stdio.h"

# include "iostream"
using namespace std;

class Reference
{
private:
int nVar1;
int nVar2;

public:
// constructor

Reference()
{
nVar1=0;
nVar2=0;
}

//to display the content
void Assign()
{


printf("%d\t%d\n", nVar1,nVar2);

}

//Operator overloading
void operator++()
{

nVar1++;
nVar2++;
nVar2++;

}


};

int main()
{

Reference Obj;

Obj.Assign();

Obj++;

Obj.Assign();

return 0;

}

OUTPUT:

0 0
1 2
Press any key to continue . . .


No comments:

Post a Comment