Yurttas/PL/OOL/Cplusplus/F/03/02/00/01.html

From ZCubes Wiki
Revision as of 22:05, 6 November 2013 by MassBot1 (talk | contribs) (Created page with "<div class="divone"><span class="h4"> [http://wiki.zcubes.com/yurttas/ Dr.Salih Yurttas ] - [mailto:yurttas@zcubes.com yurttas@zcubes.com] </span></div><div class="divone"><...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
programming languages - design principles, implementation constructs
c++

  1. abstraction mechanisms: classes and operator overloading
    1. operator overloading
      1. basic rules -
        • operator functions - example

class Complex {
public:
  Complex(const double r,
          const double i) {
    re = r;
    im = i;
  }

  Complex& operator+=(const Complex& a) {
    re += a.re;
    im += a.im;
    return *this;
  }

  friend Complex operator+(const Complex& a,
                           const Complex& b) {
    return Complex(a.re+b.re,
                   a.im+b.im);
  }

private:
  double re,
         im;
};

void f(const Complex& a,
       const Complex& b) {
  Complex p = a + b;           // short hand
  Complex q = a.operator+(b);  // oo way
  Complex r = operator+(a,b);  // explicit call
}

<< | >>


1 | basic facilities << 3 >> class derivation | 5 | 6 | 7 | 8