Classes, objects, points

I have this homework question that reads as:
1. Write a Class called Point:
a. It should have a set and get for points (x,y).
b. This class should have a function that takes an (x,y) and returns the distance between the two points (i.e. sqrt( x^2 + y^2)).
c. It should also have a function that takes in two Points (as objects) and adds them together piecewise in a third point. E.g. Point object1 = (1,2), Point object2 = (6,13), Point object3.addPieceWise(object1, object2) = (7,15).

I'm not sure if I'm doing/done each set right so far and I would like some guidance.
for a. I set the class public and private, and made so what x and y is set to be seen by the class only. I then created the function that takes an x,y and return the distance between the two. Now with functions and classes do you put the whole function inside the class or just the prototype. Now for the the last part I'm stuck on what should be done, I need Point1 and Point2 to get the thirdpoint but how could I get the thirdpoint by using a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Point {
private:
	double x, y, point;
public:
	void setPoint(double x, double y);
	double getPoint() const { return point; };
	

	double pointDistance(int x, int y, double distance) {
		distance = sqrt((x ^ 2) + (y ^ 2));
		return distance;
	}

};
int main()
{
	Point Point1;
	Point Point2;
	double distance; 


    return 0;
}

void Point::setPoint(double x, double y)
{
	cout << "Give me an x and a y: \n";
	cin >> x, y;
}
Last edited on
Now with functions and classes do you put the whole function inside the class or just the prototype.

You can do it either way. Generally, it's better to put the prototype in the class definition in the header, and then put the actual function definition in the .cpp file. This minimises the amount of recompiling you have to do it you change the implementation of the function.

Your setPoint function doesn't make much sense. You pass in two values to the function, and then you overwrite them with values read in by the user. What is it you actually want this function to do?
You can do it either way. Generally, it's better to put the prototype in the class definition in the header, and then put the actual function definition in the .cpp file. This minimises the amount of recompiling you have to do it you change the implementation of the function.


okay thanks

Your setPoint function doesn't make much sense. You pass in two values to the function, and then you overwrite them with values read in by the user. What is it you actually want this function to do?

What do you mean, isn't the setPoint function getting the x, y from the user anyways. I wanted it to get the x, y coords
What do you mean, isn't the setPoint function getting the x, y from the user anyways.

Yes it is. So what's the point of passing x and y values in, if you're going to overwrite them with the values the user enters?

And what do you want to do with those values, once the user has entered them? Because at the moment, you're doing nothing with them; those values are lost when the function returns.

I'll ask again, since you completely ignored my question the first time:

What is it you actually want this function to do?
Last edited on
closed account (48T7M4Gy)
Here's a start to how you can set it out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>

class Point {
private:
    double x;
    double y;
public:
    Point (const double = 0, const double = 0);
    
    void setPoint(double, double);
    double getXCoordinate();
    double distance(const Point);
};
////////////////////////////////////////////////////////////////////////////////////////
Point::Point( const double aX, const double aY)
{
    x = aX;
    y = aY;
}

double Point::distance(const Point somePoint)
{
    double dx = somePoint.x - x;
    double dy = somePoint.y - y;
    
    return sqrt( dx * dx + dy * dy);
}

double Point::getXCoordinate()
{
    return x;
}

////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

int main()
{
    Point P1(1, 2);
    Point P2(4, 6);
    Point P3;
    
    std::cout << "X coordinate of point P3 = " << P3.getXCoordinate() << '\n';
    std::cout << "X coordinate of point P2 = " << P2.getXCoordinate() << '\n';
    std::cout << "Distance between P1 and P2 = " << P1.distance(P2) << '\n';
    
    return 0;
}
Last edited on
class Point {
//here we're setting the and getting the x,y cords and naming the functions
//used to get the distance, the double x, y are only used by the class Point.
private:
double x;
double y;
public:
Point (const double = 0, const double = 0);

void setPoint(double, double);
double getXCoordinate();
double distance(const Point);
};
//here we want the second point for the coordinates?
Point::Point( const double aX, const double aY)
{
x = aX;
y = aY;
}
//this will get the distance between the two points and get the return defined
double Point::distance(const Point somePoint)
{
double dx = somePoint.x - x;
double dy = somePoint.y - y;

return sqrt( dx * dx + dy * dy);
}
//I'm not sure what this is for if you could clarify that's be a help
//(or is this getting the third point )?
double Point::getXCoordinate()
{
return x;
}

///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

//in the main we're setting the 2 points P1 and P2, and p3 is going to come in
//play
int main()
{
Point P1(1, 2);
Point P2(4, 6);
Point P3;
// when you use the class in main you have to use the objects infront of the
//functions always?
std::cout << "X coordinate of point P3 = " << P3.getXCoordinate() << '\n';
std::cout << "X coordinate of point P2 = " << P2.getXCoordinate() << '\n';
std::cout << "Distance between P1 and P2 = " << P1.distance(P2) << '\n';

return 0;
}


just to be clear, and understand whats going on (commented on each part)
and I just realized you only got the x coordinates for them all, thank you its make it easier to know what I should be doing
Last edited on
closed account (48T7M4Gy)
Yeah, it's a start and not the complete deal. You need a getter for the Y coordinate, and a destructor.

P3 is there just to show you how the default constructor works.

line 16 etc is the constructor.

What you need to do is sit down with a textbook/notes and read up on Classes. It won't take you long.
Yeah, it's a start and not the complete deal. You need a getter for the Y coordinate, and a destructor.

P3 is there just to show you how the default constructor works.

line 16 etc is the constructor.

What you need to do is sit down with a textbook/notes and read up on Classes. It won't take you long.

thanks man and yeah I will do!
Topic archived. No new replies allowed.