Computer Science Canada

inheritence & overloaded operators

Author:  Imajica [ Wed Apr 14, 2004 5:17 am ]
Post subject:  inheritence & overloaded operators

I have to write a program using inheritence & the overloaded << operator, how do you call the base class functions output stream from the derived class?

For example, to do a print from class base via a derived class

void derived::print()
{
base::print()
}

works, the same syntax with overloaded << does not.

I am clueless on this.

Author:  wtd [ Fri Apr 23, 2004 7:29 pm ]
Post subject: 

Operators can be called in one of two ways.

If for instance, I have a class A that overloads << for output streams, like so:

code:
#include <iostream>

class A
{
   public:
   friend std::ostream& operator<<(std::ostream&, const A&);
};

int main()
{
   A myVar;

   std::cout << myVar;
   // is the same as...
   operator<<(std::cout, myVar);
}

std::ostream& operator<<(std::ostream& out, const A& a)
{
   return out << "hello world!";
}


: