Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Help with output operator

Posted on 2003-10-29
Medium Priority
167 Views
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:

#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;
}

0
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
• 3

LVL 86

Accepted Solution

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

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

LVL 86

Expert Comment

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

ID: 9671377
Do you have any further problems with this one?
0

LVL 9

Expert Comment

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}

Tinchos
EE Cleanup Volunteer
0

Featured Post

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll