insertion operator

I have a class funtion that displays output.
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.
Who is Participating?
willemnelConnect With a Mentor Commented:
#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;


PS. Delete the other 3 Q that are the same as this one
strmtrprAuthor Commented:
thanks for the help I had it right,
I had missed spelled const.
Thanks again
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.