Point Class Doubt

Hello everybody, I m currently taking the object oriented C++ class, and i m involved in my first project, this is so far what I have, I would appreciate any help thank you:

this is the header file:
#ifndef POINT_H
#define POINT_H

class Point
{
public:
Point(double newX, double newY);

void setNewX(double newX);

void setNewY(double newX);

double getnewX();

double getnewY();

void showPoint();

double calcDistance(Point);

private:
double newX;
double newY;



};

#endif // POINT_H

this is the implementation file
#include "Point.h"

Point::Point(double newX, double newY)
{

}

double Point::getnewX()const{

return this->newX;

}

double Point::getnewY()const
{

return this->newY();
}

void Point::setNewX(double newX);

void Point::setNewY(double newY);


closed account (S6k9GNh0)
Some personal suggestions others might disagree with:

Unless you want to prevent setting or fetching, or unless you want to do something in addition to setting and getting of a member variable, there's little point to a setter/getter. You're code above could be simplified to the following:

1
2
3
4
5
6
7
8
9
struct Point {
    Point(double _x = 0.0, double _y = 0.0)
    : x(_x), y(_y) { }

    double calcDistance(Point&);

    double x;
    double y;
};
Last edited on
First a coding style comment. Variables should have meaningful names. I'm not sure why you use the word "new" with all your data member names and accessor functions. A point has an x and a y value. If you are changing the values, you might pass newX and/or newY as arguments to the appropriate function(s), but the values in the point object should probably just be called x and y. The newX and newY names make it a little bit more difficult to read and understand.

And I don't necessarily agree with @computerquip. It's obvious that you are just learning how to use classes, so getter and setter functions are completely appropriate here.

Now on to substance.

Your constructor doesn't do anything with the arguments it receives. You need to store those values into the objects data members. See http://www.cplusplus.com/doc/tutorial/classes/ . You can also use an initialization list to initialize the data members, but I could not find a tutorial article on that.

The name of the arguments to your functions are the same as the names of your data members. Technically you can do this, but there are some subtleties involved that you don't need to learn quite yet, and I don't think it's generally a good idea to do it anyway. For now, make sure that your data members and your function arguments have different names so you can keep them straight when you are working with your code.
Topic archived. No new replies allowed.