MC logo

Operator Overloading

  Practice Questions

Given the declarations


  #include <iostream.h>
  class Gromitz
  {
       int g;
  public:
       Gromitz(int i=0) { g = i; }
       Gromitz operator+(const Gromitz& gr) const
       {
            return Gromitz(g + gr.g);
       }
       bool operator==(const Gromitz& x) const
       {
            return g == x.g;
       }
       ostream& operator<<(ostream &str) const
       {
            str << g;
            return str;
       }
       int val() const { return g; }
  };
  Gromitz a,b(5);
  void f(Gromitz g);

Say which are valid (will compile), and which are invalid (will not compile).

  1. a = 10;

  2. a += b;

  3. cout << b;

  4. a == 7;

  5. f(b);

<<Function Overloading Answer Construction and Destruction I>>