swapping two values with normal variables and user-defined variables:
#include <string.h>
#include <iostream.h>
class murugesandins
{
int a ;
int b ;
char name[100] ;
public:
murugesandins()
{
a = 8 ;
b = 12 ;
memset( name, '\0', 100) ;
strcpy( name, "defaultValue") ;
}
murugesandins(char *name, int a, int b):a(a),b(b)
{
strcpy( this->name, name) ;
}
murugesandins operator +(murugesandins next)
{
murugesandins temp(name, a, b) ;
temp.a = temp.a + next.a ;
temp.b = temp.b + next.b ;
return temp ;
}
murugesandins operator -(murugesandins next)
{
murugesandins temp(name, a, b) ;
temp.a = temp.a - next.a ;
temp.b = temp.b - next.b ;
return temp ;
}
void myValue()
{
cout << "murugesandins name:" << name << endl ;
cout << "murugesandins a:" << a << endl ;
cout << "murugesandins b:" << b << endl ;
}
};
int main()
{
int a = 6, b = 12 ;
cout << "Initial values\na:" << a << ": b:" << b << ":\n" ;
a = a + b ;
b = a - b ;
a = a - b ;
cout << "Final values\na:" << a << ": b:" << b << ":\n" ;
murugesandins one( "one", 1, 3) ;
murugesandins two( "two", 6, 12) ;
one.myValue() ;
two.myValue() ;
one = one + two ;
two = one - two ;
one = one - two ;
one.myValue() ;
two.myValue() ;
return 0 ;
}