Solved

insertion operator

Posted on 1998-11-08
2
207 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 20 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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