Solved

# testing my class member functions

Posted on 2005-04-19

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;

}