I have a class called Person.
I also have a class called Truck, inherited from class Vehicle.

I want to overload the iostream operators for all three classes so that I can do statements such as:

Person a ;
Vehicle b ;
Truck c ;

cin >> a ;
cin >> b ;
cin >> c ;

Is this possible? If so, how?

Recommended Answers

All 3 Replies

>Is this possible?
Yes.

>If so, how?
Just like you would overload the >> operator for any normal class. It doesn't change with inheritance.

but when redefining the << and >>, how do I specify which class the overloading pertains to?

all of them start with

ostream & operator<<(ostream & os, const Complex & c)

edit] funny how when you post questions you answer them yourself after re-reading it. Man I feel dumb. Thanks.

Complex is the name of your class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.