Solved

insertion operator

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

729 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