?
Solved

Help with output operator

Posted on 2003-10-29
6
Medium Priority
?
166 Views
Last Modified: 2010-04-02
I am trying to take the following header and implementation files and changed them so that an output operator, <<, can be used. The code Point P(14.3, 4.2);
                       cout<<P;
should produce an utput of (14.3, 4.2). What modifications would be necessary to make this happen?
here is my code so far:

header:
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <conio>

using namespace std;

class Point
{
public:
Point (); //constructor
Point (double, double); //constructor

double GetX () const;
double GetY () const;

private:
double x;
double y;
};
#endif

and the implementation file:
#pragma hdrstop
#include "point.h"
Point::Point ()
{
     x=0;
     y=0;
}

Point::Point ( double InputX, double InputY)
{
x=InputX;
y=InputY;
}

double Point::GetX () const
{
      return x;
}

double Point::GetY () const
{
     return y;
}

please help me out on this
0
Comment
Question by:jba6511
[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
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 750 total points
ID: 9645613
You'll need a global 'operator<<()', as you cannot add that functionality to 'cout', e.g.

ostream& operator << ( ostream& r, Point& l) {

  r << "(" << l.GetX() << ", " << l.GetY() << ")";

  return r;
}

As the above only uses public members of 'Point', you won't even need to make it a 'friend'.
0
 

Author Comment

by:jba6511
ID: 9645942
what would need to be done if i was to make it a friend then?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9645962
>>what would need to be done if i was to make it a friend then?

You'd use

class Point
{
public:

friend ostream& operator << ( ostream& r, Point& l);

//...
};

in your class definition - but, this is not even necessary...

0
 
LVL 86

Expert Comment

by:jkr
ID: 9671377
Do you have any further problems with this one?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546303
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9645613}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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