Solved

insertion operator

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now