473,414 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Accessing inherited operator<< in base class from derived class

Hello,
I am trying to figure out how to call an overloaded operator<<
inherited from a base class.

#ifndef PHONECALL
#define PHONECALL

#include "time.h"
#include "interval.h"

enum CallType {LOCAL, LONG_DISTANCE};

class PhoneCall
{
public:
friend std::istream& operator>>(std::istream& in, PhoneCall&
phoneCall);
friend std::ostream& operator<<(std::ostream& out, const
PhoneCall& phoneCall);
CallType getCallType() const;
int callLength() const;

private:
CallType callType;
Interval<Time> interval;
};

#endif
Here is the inherited class:
#include "phonecall.h"
#include "money.h"
#include <iostream>

class CostedCall:public PhoneCall
{
public:
CostedCall();
CostedCall(PhoneCall phoneCall, Money cost);
friend ostream& operator<<(ostream& out, const CostedCall&
phonecall);
private:
Money cost;
};

The output on the base class PhoneCall works correctly. What I need to
know is how to call the operator<< in the base class PhoneCall from
the derived class CostedCall. I tried something like this:

ostream& operator<<(ostream& out,const CostedCall& phonecall)
{

PhoneCall::operator<<(out,phonecall);

return out;
}

I am completely lost. Any help would be appreciated.

Alicia
Jul 22 '05 #1
3 1877
Alicia wrote in news:78**************************@posting.google.c om in
comp.lang.c++:

ostream& operator<<(ostream& out,const CostedCall& phonecall)
{

It isn't a member so you can't do this:
PhoneCall::operator<<(out,phonecall);
out << static_cast< Phoncall const & >( phonecall );
return out;
}


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Alicia wrote:
Hello,
I am trying to figure out how to call an overloaded operator<<
inherited from a base class. *snip* I am completely lost. Any help would be appreciated.

Alicia


Nice question :)

So, let me simplify your problem:

class A {
public:
friend ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

class B : public A {
public:
// code
friend ostream& operator<<(ostream& out, const B& b){
// code
}
}

So, what you want to do is something like that:

A *a=new B();
cout << *a;
and you want the operator<< in class B to be called, ok?
but you can not redefine a function that is not virtual, so you want to
write:

class A { // version 1.1
public:
virtual friend ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

Ops, you can not make friends virtual, and now?

class A { // version 1.2
public:
virual ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

Ops! operator<< now cannot access private members of A!
The question is: how to make a virtual and friend function? The answer
is: separate them!!!

class A { // version 2.0
public:
friend ostream& operator<<(ostream& out, const A& a){
return a.print(out);
}
virtual ostream& print(ostream& out) const {
out << "A::operator<<" << endl;
return out;
}
};

And now the class B!

class B : public A {
public:
// code
ostream& print(ostream& out) const {
out << "B::operator<<" << endl;
return out;
}
}

Now, what happens when you do:

A *a=new B();
cout << *a;

1) operator<< (ostream&, A&) is called.
2) a.print(out) is called, but the type of a is B, so B::print(ostream&)
is called.

If you need, you can complete the code adding the operator<< to B too.

class B : public A {
public:
// code
friend ostream& operator<<(ostream& out, const B& b){
return b.print(out);
}
ostream& print(ostream& out) const {
out << "B::operator<<" << endl;
return out;
}
}

This tecnique is called multiple dispatching and it's explained well in
"thinking in C++".

Bye!

P.S.: I have not compiled the code, so i apologize for errors...
Jul 22 '05 #3
A better way to do this is in section 21.2.3.1 "Virtual
Output Functions" in Stroustrup, 3rd Edition.

He suggests using a virtual put function which in turn
used in an operator<< function defined for the base class.

Here's his example with some added comments:

// base class header file
//
class My_base {
public:
virtual ostream& put(ostream& s) const=0
};

ostream& operator<<(ostream& s, const My_base& r)
{
// uses the right put()
return r.put(s);
}

// derived class header file
//
class Sometype : public My_base {
public:
// override My_base::put()
ostream& put(ostreasm& s) const;
};
// some other .cpp file
//
void f(const My_base& r, Sometype& s)
{
// use << which calls the right put()
std::cout << r << s;
}
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
8
by: Christopher Benson-Manica | last post by:
I know I keep asking similar questions, but I really want to do this at least sort of right. Not to mention I got no on-group replies to my previous post :( I desperately want an interface that...
6
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
6
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.