?
Solved

insertion operator

Posted on 1998-11-08
2
Medium Priority
?
209 Views
Last Modified: 2010-04-02
I have a class funtion that displays output.
display()
{
  cout<<num<<"/"<<dem<<endl;
}
now I have to overload << in this function


num and dem are private data members.
This function is in a class fraction.
how do I set this up.
before I would call this function like:
fraction f1(2,4);  //sets num=2 dem=4
f1.display;      //this would display 2/4

Please point me in the right direction.
0
Comment
Question by:strmtrpr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
willemnel earned 80 total points
ID: 1177270
#include <iostream.h>
#include <stdlib.h>

class fraction
{
 // Your stuff here

 friend ostream &operator<<( ostream &stream, const fraction &frac);

};

ostream &operator<<( ostream &stream, const fraction &frac)
{
 stream << frac.num << '/' << frac.dem << endl;
 return stream;
}

void main(void)
{
 fraction f1(2,4);

 cout << f1;
}

OUTPUT: 2/4


PS. Delete the other 3 Q that are the same as this one
0
 

Author Comment

by:strmtrpr
ID: 1177271
thanks for the help I had it right,
I had missed spelled const.
Thanks again
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question