Solved

insertion operator

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
How do I add zip/compression functionality to a C++ (Windows and Linux) application? 3 97
Find Visual Studio Tools 2 92
C++ question 3 62
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 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

12 Experts available now in Live!

Get 1:1 Help Now