I want to know details about swaping in c or c++ language and give me some example code on it....please..
what is swaping ?????
Collapse
X
-
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, "defaultVal ue") ;
}
murugesandins(c har *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 << "murugesand ins name:" << name << endl ;
cout << "murugesand ins a:" << a << endl ;
cout << "murugesand ins 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 ;
}Comment
Comment