Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 inheritence & overloaded operators
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Imajica




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Apr 23, 2004 7:29 pm   Post subject: (No 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!";
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: