Link to home
Start Free TrialLog in
Avatar of dauler
dauler

asked on

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 add(const Point&) const;
            Point distance(const Point&) const;
            Point display(const Point&) const;
      
            

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

Point Point::add (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?
}
#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;
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mactep13
mactep13

Try overloading the << operator for cout to display the value:

Class Point
{
 // Your typical stuff

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.
     
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_
Avatar of dauler

ASKER

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.