# testing my class member functions

I have written the following code for a class structure used to compute the distance between two points as well as add the coordinates.  I know this is rather involved for such a simple prograsm but I am trying to learn how to implement classes and their functions.  It compiles fine but I have no idea how to test the values.  My function that computes the distance returns a value but I can't seem to display it.  I've tried cout but it does not allow the display of my class function return value.  Any ideas?
#ifndef _POINT_H_
#define _POINT_H_

class Point
{
public:
// Constructors
Point(int x = 0, int y =0): _x(y), _y(y)
{}
Point(const Point& p): _x(p._x), _y(p._y)
{}
~Point()
{}

// Accessors
int x1()const { return _x; }
int y1()const { return _y; }

// Modifiers
void x1(int x) { _x = x; }
void y1(int y) { _y = y; }
void set_xy(int x, int y) { _x = x, _y = y; }
// Member Functions
void increment_x() { _x++; }
void increment_y() { _y++; }
Point distance(const Point&) const;
Point display(const Point&) const;

private:
int _x, _y;
};
#include "Point[1].h"//function file
#include <cmath>

{
Point add((this->_x + p._x) + (this->_y + p._y));

return add;// how do I retrieve this result?
}
Point Point::distance(const Point& p) const
{
Point dist(sqrt(pow((this->_x - p._x),2) +
pow((this->_y - p._y),2)));

return dist;//how do I retrieve this result?
}
#include "Point[1].h"//My test program
#include <iostream>

using namespace std;

int main ()

{
const Point p1(1,2);
Point p2(2,4);
Point p3(-5, 6);
Point p4;
p4 = p2.distance(p1);

return 0;
}
###### Who is Participating?

Commented:
I'd start by outputting the results, e.g.

#include "Point[1].h"//My test program
#include <iostream>

using namespace std;

int main ()

{
const Point p1(1,2);
Point p2(2,4);
Point p3(-5, 6);
Point p4;
p4 = p2.distance(p1);

cout << "p1 is " << p1.x1() << "/" p1.y1() << endl;
cout << "p2 is " << p2.x1() << "/" p2.y1() << endl;
cout << "p3 is " << p3.x1() << "/" p3.y1() << endl;

cout << "The resulting p4 is:" << endl;
cout <<  p4.x1() << "/" p4.y1() << endl;

return 0;
}

0

Commented:

Class Point
{

friend ostream& operator<< (ostream& ostr, Point& pt);
}

above main:

ostream& operator<<(ostream& ostr, Point& pt)
{
ostr << "X: " << pt._x << " Y: " << pt._y << endl;
return ostr;
}

main()
{
const Point p1(1,2);
Point p2(2,4);
Point p3(-5, 6);
Point p4;
p4 = p2.distance(p1);

cout << p4;
}

The reason we can access the Point's private data members is because we made the ostream a friend of the class Point. Normally, you do not want to make other classes or functions friends of classes, but making an ostream a friend is a well known old tactic and for this partical case, it is usually deemed appropriate.

I do agree with jkr's answer as well. I only wish to provide an alternative, however, in my opinion, overloading the << operator would be a better choice here. Jkr, would you not agree?

Hope this helps,

Mactep.

0

Commented:
try this.
Also there was an error in that code
Point(int x = 0, int y =0): _x(y), _y(y); // y is used in both cases.

#ifndef _POINT_H_
#define _POINT_H_

class Point
{
public:
// Constructors
Point(int x = 0, int y =0): _x(x), _y(y){}
Point(const Point& p): _x(p._x), _y(p._y){}

~Point(){}

// Accessors
int x1()const { return _x; }
int y1()const { return _y; }

// Modifiers
void x1(int x) { _x = x; }
void y1(int y) { _y = y; }
void set_xy(int x, int y) { _x = x, _y = y; }
// Member Functions
void increment_x() { _x++; }
void increment_y() { _y++; }

Point operator +(const Point&) const;
Point distance(const Point& p) const;

friend ostream & operator << (ostream & os, Point & p);

private:
int _x;
int _y;
};
#include "Point[1].h"//function file
#include <cmath>

Point Point::operator + (const Point& p) const//function to add points
{
Point add((this->_x + p._x) + (this->_y + p._y));

return add;// how do I retrieve this result?
}
Point Point::distance(const Point& p) const
{
Point dist(sqrt(pow((this->_x - p._x),2) +
pow((this->_y - p._y),2)));

return dist;//how do I retrieve this result?
}

ostream & operator << (ostream & os, Point & p)
{
os<< "x1=" << p._x <<",y1=" << p._y << endl;
return os;
}
#include "Point[1].h"//My test program
#include <iostream>

using namespace std;

int main ()

{
const Point p1(1,2);
Point p2(2,4);
Point p3(-5, 6);

cout << p2;

Point p4 = p1 + p2;
cout << p4;
p4 = p2.distance(p1);
cout << p4;
return 0;
}

_novi_
0

Author Commented:
JKR- Thank you very much.  Great help.  I now understand the accessor function!  As I understand the accessor functions are needed in classes to access the private data members.  When I was trying to cout my private members directly they were not accessible(obviously :D).  Duh, this makes total sense now.

Mactep13-Thank you for the alternate solution.  I haven't learned how to use the friend specifier yet, but it looks like a useful option.  I wish there was a way to split solution points between the experts.  Maybe there is let me know.

Novitiate-thank you for that overloaded varaible catch-that also totally helped.

You guys all rule as usual.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.