Register to reply

(C++) Not sure what the problem wants me to do.

by cpponyou
Tags: None
Share this thread:
cpponyou
#1
May10-10, 03:24 AM
P: 1
I have this HW problem, but I cannot figure out what it wants me to do. I don't want anyone to solve it for me, just point me in the right direction.

Suppose that a class hierarchy has a virtual method print
class C {
virtual void print( ostream& ) const;
};
that outputs C's members to the stream ostream. Write one overload of << so that the statement

out << p;
outputs p's members to the stream out, where p is any object that belongs to a class in the hierarchy.

What I don't understand is the out << p bit. Am I supposed to be able to do

out << p;
in my main() and have the overload call p.print() ?
Phys.Org News Partner Science news on Phys.org
Experts defend operational earthquake forecasting, counter critiques
EU urged to convert TV frequencies to mobile broadband
Sierra Nevada freshwater runoff could drop 26 percent by 2100
D H
#2
May10-10, 04:11 AM
Mentor
P: 15,173
That is exactly what you are supposed to do.

You should probably make your overloaded function obey the semantics of the << operator as used with streams so that given two such objects p and q the following works:
out << "Members of p:\n" << p << "\nMembers of q:\n" << q;
CompuChip
#3
May10-10, 04:12 AM
Sci Advisor
HW Helper
P: 4,300
Indeed. So they want you to override operator<<


Register to reply

Related Discussions
General solution of initial value problem -dont understand problem is asking me? Calculus & Beyond Homework 1
Pulley problem involves Friction static and Kinetic problem Introductory Physics Homework 1
Integral Word Problem EXACT PROBLEM Incluced Calculus & Beyond Homework 6
One Kinematic Problem, One Pendulum Problem, One Wave Problem Introductory Physics Homework 7
Classic E&M problem: point charge and a charged sphere, how to analyze this problem Advanced Physics Homework 1