Index

C & C++ Programming - Operators

#pragma once

class CMyClass
{
private:
 double m_nA;
 double m_nB;

public:
 CMyClass(int x=0,double y=0)
  {
  m_nA = x,
  m_nB = y;
 }

 void read()
 {
  cout << "The values of a & b" << endl;
  cout << "1. = ";
  cin >> m_nA;
  cout << "2. = ";
  cin >> m_nB;
 }

 CMyClass operator+(CMyClass x);
 CMyClass operator-(CMyClass x);
 CMyClass operator*(CMyClass x);
 CMyClass operator/(CMyClass x);
 CMyClass operator+(int x);
 CMyClass operator-(int x);
 CMyClass operator*(int x);
 CMyClass operator/(int x);
 friend CMyClass operator+(int x,CMyClass y);
 friend CMyClass operator-(int y,CMyClass x);
 friend CMyClass operator*(int y,CMyClass x);
 friend CMyClass operator/(int y,CMyClass x);
  void operator=(int x);
 void operator=(CMyClass x);

 void display()
 {
  cout << "Entered values are" << endl << "a= " << m_nA << endl << "b= " <<    m_nB << endl;
 }
};

CMyClass CMyClass::operator+(CMyClass x)
{
 CMyClass t;
 t.m_nA=m_nA+x.m_nA;
 t.m_nB=m_nB+x.m_nB;
 return t;
}

CMyClass CMyClass:: operator+(int x)
{
 CMyClass t;
 t.m_nA=x+m_nA;
 t.m_nB=x+m_nB;
 return t;
}

CMyClass operator+(int x,CMyClass y)
{
 CMyClass t;
 t.m_nA=y.m_nA+x;
 t.m_nB=y.m_nB+x;
 return t;
}

CMyClass CMyClass:: operator-(CMyClass x)
{
 CMyClass t;
 t.m_nA=m_nA-x.m_nA;
 t.m_nB=m_nB-x.m_nB;
 return t;
}

CMyClass CMyClass:: operator-(int x)
{
 CMyClass t;
 t.m_nA=m_nA-x;
 t.m_nB=m_nB-x;
 return t;
}

CMyClass operator-(int y,CMyClass x)
{
 CMyClass t;
 t.m_nA=y-x.m_nA;
 t.m_nB=y-x.m_nB;
 return t;
}

CMyClass CMyClass:: operator*(CMyClass x)
{
 CMyClass t;
 t.m_nA=m_nA*x.m_nA;
 t.m_nB=m_nB*x.m_nB;
 return t;
}

CMyClass CMyClass:: operator*(int x)
{
 CMyClass t;
 t.m_nA=m_nA*x;
 t.m_nB=m_nB*x;
 return t;
}

CMyClass operator*(int y,CMyClass x)
{
 CMyClass t;
 t.m_nA=y*x.m_nA;
 t.m_nB=y*x.m_nB;
 return t;
}

CMyClass CMyClass:: operator/(CMyClass x)
{
 CMyClass t;
 t.m_nA=m_nA/x.m_nA;
 t.m_nB=m_nB/x.m_nB;
 return t;
}

CMyClass CMyClass:: operator/(int x)
{
 CMyClass t;
 t.m_nA=m_nA/x;
 t.m_nB=m_nB/x;
 return t;
}

CMyClass operator/(int y,CMyClass x)
{
 CMyClass t;
 t.m_nA=y/x.m_nA;
 t.m_nB=y/x.m_nB;
 return t;
}

void CMyClass:: operator=(int x)

{
 m_nA = x;
 m_nB = x;
}

void CMyClass:: operator=(CMyClass x)
{
 m_nA = x.m_nA;
 m_nB = x.m_nB;
}

//////////////////////////////////////////////////////////////////////
// Entry Function
void TestOverloading()
{
CMyClass a1(2,4), a2(5,3), a3;

 // a1.read();
 // a2.read();
 a3=(a2*3)*5/a1;
 a3.display();
}

#pragma once

class A
{
private:
 int a, b;

public:
 A( int x = 0, int y = 0 )
 {
  a = x;
  b = y;
 }

 void operator = (class B x);

 void display()
 {
  cout << endl << a <<" " << b << endl;
 }

 // friend void B::operator=(A x);
 friend B;
};

class B
{
public:
 B( int x = 0, int y = 0 )
 {
  a = x;
   b = y;
 }

 void operator = ( A x )
 {
  a = x.a;
  b = x.b;
 }

 void display()
 {
  cout << endl << a << "\t" << b << endl;
 }

 friend A;

 private:
 int a, b;
};

void A::operator = (class B x)
{
 a = x.a;
 b = x.b;
}

void TestAssignment()
{
 A a1(10, 10);
 B b1;

 a1 = b1;
 a1.display();
}
Index