Operator Overloading

OPERATOR OVERLOADING

Operator overloading is a way to enable c++'s operators to work with class objects (p.432, Dietel/Dietel)

Introduction

/* The example below will show you, lots of overloaded operators for class Complex. Declaration of this class is at the bottom of the page and is not really needed for you to lok at right now, since you already know what complex number is.*/

main () {
  Complex X,Y(3,4),Z(5,6);
  cout << "X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  if (!X)
    cout << "X is zero\n";
  else
    cout << "X is not zero\n";
  if (!Y)
    cout << "Y is zero\n";
  else
    cout << "Y is not zero\n";
  cout << "Y + Z = " << Y + Z << '\n';
  cout << "Y * Z = " << Y * Z << '\n';
  cout << "Y - Z = " << Y - Z << '\n';
  X = Y + Y;
  cout << "X = Y + Y = " << X << '\n';
  cout << "Y = " << Y << '\n';
  Y += Z;
  cout << "Y += Z : Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  cin >> Z;
  cout << "After >>, Z = " << Z << '\n';
  X = Y + (Z++);
  cout << "X = Y + Z++ : X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  X = Y + (++Z);
  cout << "X = Y + ++Z : X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
}

Operator overload:
Allows use of operators for class types
May be implemented in member function or in non-member function
Declaration (BNF for OPERATOR OVERLOAD)
Format
<ftype> operator <operator> (<arglist>);
<arglist> ::= |<arg>|(<arg>, <arg>)
<ftype>::= <type>
<operator> ::= <C++ operator except :: .* ?. . sizeof
Examples
complex operator + ();
complex operator + (complex A);
complex operator + (complex A, complex B);
Use
Binary operator
Examples
complex X,Y,Z;
.....
X = Y;
X = Y + Z;
Format
<leftoperand><operator><rightoperand>
<member> <non-mem>
<leftop> *this arg1
<rightop> arg arg2
<expvalue> return value return value
For member function, leftop must be object of class type. If left operand is of different type, then function cannot be member function.
Assignment (=), subscript([]), call (()), and member selection (->) must be member functions.

Unary operator
Examples
X++;
-Y;
Format
<operator> <operand> OR <operand><operator>
<member> <non-mem>
<operand> *this <arg>
<expvalue> return value return value

 

Notes
Predefined meaning for built-in types may not be overridden.
Operators may be defined only for class types.
The predefined precedence, associativity, and arity of operators must be preserved.
I/O operator overloading
Consider: OS << A;
<leftop> OS
<rightop> A
<retvalue> new OS
  1. << is a binary operator.
  2. This function may not be written as a member function since the left operand is not of the class type, and, therefore, may not be "this.
  3. This function must be written as non-member function with first argument left operand (iostream) and second argument right operand.
  4. First argument must be reference argument since it will be changed in function.

ostream& operator << (ostream &OS, complex A)

Two approaches to providing access to private members of class:
Make overloaded function a friend of class.
Use member functions such as getreal and getimag to access needed values.

Example- (The Complex class)

#include <iostream.h>

//complex.h

class Complex {
  friend ostream& operator << (ostream& OS, Complex A);
  friend istream& operator >> (istream& IS, Complex &A);
  int Real, Imag;
  public:
    Complex ();
    // C := 0 + 0i
    Complex (int R, int I);
    // C := R + Ii
    Complex operator + (Complex A);
    // Sum = C + A
    Complex& operator += (Complex A);
    // C = C + A
    Complex operator * (Complex A);
    // Product = C * A
    Complex operator - (Complex A);
    // Difference = C - A
    int operator !();
    // return (Real==0 && Imag==0)
   Complex& operator ++ ();
    // preincrement of real part
    Complex operator ++ (int);
    // postincrement of real part
};

//complex.cc

Complex::Complex () {
  Real = Imag = 0;
}

Complex::Complex (int R, int I) {
  Real = R;
  Imag = I;
}

Complex Complex::operator + (Complex A) {
  Complex S;
  S.Real = A.Real + Real;
  S.Imag = A.Imag + Imag;
  return S;
}

Complex& Complex::operator += (Complex A) {
  Real = A.Real + Real;
  Imag = A.Imag + Imag;
  return *this;
}

Complex Complex::operator * (Complex A) {
  Complex P;
  P.Real = A.Real * Real - A.Imag * Imag;
  P.Imag = Real * A.Imag + A.Real * Imag;
  return P;
}

Complex Complex::operator - (Complex A) {
  Complex D;
  D.Real = Real - A.Real;
  D.Imag = Imag - A.Imag;
  return D;
}

int Complex::operator ! () {
  return (Real==0 && Imag==0);;
}

ostream& operator << (ostream& OS, Complex A) {
  // output = output ~ A
  OS << A.Real << '+' << A.Imag << 'i';
  return OS;
}

istream& operator >> (istream& IS, Complex &A) {
  IS >> A.Real >> A.Imag;
  return IS;
}

Complex& Complex::operator ++ () {
  Real++;
  return *this;
}

Complex Complex::operator ++ (int) {
  Complex c;
  c = *this;
  Real++;
  return c;
}

main () {
  Complex X,Y(3,4),Z(5,6);
  cout << "X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  if (!X)
    cout << "X is zero\n";
  else
    cout << "X is not zero\n";
  if (!Y)
    cout << "Y is zero\n";
  else
    cout << "Y is not zero\n";
  cout << "Y + Z = " << Y + Z << '\n';
  cout << "Y * Z = " << Y * Z << '\n';
  cout << "Y - Z = " << Y - Z << '\n';
  X = Y + Y;
  cout << "X = Y + Y = " << X << '\n';
  cout << "Y = " << Y << '\n';
  Y += Z;
  cout << "Y += Z : Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  cin >> Z;
  cout << "After >>, Z = " << Z << '\n';
  X = Y + (Z++);
  cout << "X = Y + Z++ : X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
  X = Y + (++Z);
  cout << "X = Y + ++Z : X = " << X << '\n';
  cout << "Y = " << Y << '\n';
  cout << "Z = " << Z << '\n';
}

INPUT:
1 2

OUTPUT:
X = 0+0i
Y = 3+4i
Z = 5+6i
X is zero
Y is not zero
Y + Z = 8+10i
Y * Z = -9+38i
Y - Z = -2+-2i
X = Y + Y = 6+8i
Y = 3+4i
Y += Z : Y = 8+10i
Z = 5+6i
After >>, Z = 1+2i
X = Y + Z++ : X = 9+12i
Y = 8+10i
Z = 2+2i
X = Y + ++Z : X = 11+12i
Y = 8+10i
Z = 3+2i

Back Home Up