C++ exam subject

Anyone of you know how to do this? I got stuck at the distance thing.
Its c++
>Define a class called Point with x,y int coordinates, private variables of the class.
>Define 2 constructors, one without parameters that initiates with 0 the variables and another that lets the variables be initiated.
>Define a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.
>Define an array of n

Other urls found in this thread:

cplusplus.com/forum/general/7950/
stackoverflow.com/a/24398366
twitter.com/SFWRedditVideos

What about it confused you? Just use the distance formula.

sure, all you have to do is aefine a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.

Did I note this wrong cause it sounds so fucked up and maybe that's what I don't know what to do.

I know you have to use that but I don't know how to do the parameters and objects thing. I'm still learning and watching tutorials on OOP.

double distance(Point other) {
return sqrt((pow((this.x-other.x),2) + pow((this.y-other.y),2)));
}


this can't work however, because 1) says that x and y are private. they need to be public for this to work

Does it matter if it's happening in the same class?

also "Point other" should be "Point & other" because you need to pass a reference

yes, this is an instance method so you're calculating a distance between the object that has the method (coordinates are its fields -- this.x and this.y) and another object passed as a parameter

What do I put in the header file?
public:
Point *other;

And in the point.cpp

double Point::Distance(Point &other){}

Also how do I make that array and assign to an object? Does it have to be random values?

It's worded like shit, but this is still really easy. You need to write accessor function since the coordinates are private data members.

I'm not sure about the exact syntax because I don't do C++ at all. just google it, I guarantee you'll find the answer

>Does it have to be random values?
no. just make up some simple values so that you'll be able to verify the calculations by hand

I write accessor_x and accessor_y which return x and y right? How do I use those functions?

These are public members of the class that will return the values of the private members when called.

Well I need the x and y for the distance formula. How do i implement those accesors so the formula can use x and y.

You can have a separate function for that. The return values of your accessor functions can be used as parameters for your distance function.

The whole point of OOP is to split things into modular pieces.

Okay let's get back to basics. I don't understand what the distance function does. Do i input 2 points on 2 different objects? ob1(5,2) ob2(6,3)?

No, in C++ a class can access the private members of other classes if they are both the same class.

Point p1(5,2);
Point p2(6,3);
p1.Distance(p2);

>>Define a class called Point with x,y int coordinates, private variables of the class.
class Point {
private:
double x;
double y;
};

>>Define 2 constructors, one without parameters that initiates with 0 the variables and another that lets the variables be initiated.
public:
Point(double xx, double yy) : x(xx), y(yy) {}
Point() : Point(0,0) {}

>>Define a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.
public:
double Distance(Point &other)
{
double dx = x - other.x, dy = y - other.y;
return sqrt(dx*dx+dy*dy);
}

>>Define an array of n

what the fuck, it works like that in java too. either I'm only learning about this now which would be ridiculous or I'm having a serious stroke

that for loop goes out of bounds

yes... I changed the n to 5 before I pressed send....
Also a good reason not to use c style arrays.

What do I put in the header file when initialising the distance method? I've put this but it doesn't work:
>double distance(); .
It give an error in the cpp file:
>prototype for 'double Point::distance(Point&)' does not match any in class 'Point'

Include the parameters type in the paranthesi:
Point&

double Distance(Point&);

Oops i thought i tried that.
How do i create that vector as an object to Point?

std::vector points {{0,0},{1,2},{3,4},{5,6}};

I wrote public so you could see it should be a public function, as in part of the class.
If you want to split the code, replace the curly brackets with a semicolon in the header and put Point:: in front of the function names in the source file.
What kind of course have you taken which doesn't cover this?

Also what is the last question?

Do I make it like this: cplusplus.com/forum/general/7950/ ?
It seems overdone

Codeblocks won't let me use this? It says something about c++11 and c++98. I;ve included too.

I didn't study during the semester and I'm doing it now. What last question? About the alignment? I'll figure that out.

Since we are spoonfeeding you everything anyway....

I think the question is:
How do I create a vector of Points?
It could be like this This will create the points described.
In you create a bunch of identical Points.
Since the task was to create a few, a initializer list is pretty good because you can write it all in a single line.
When you have more, you need better methods.

You need C++11 for { } initialization.
stackoverflow.com/a/24398366

But all these things are stuff mentioned in the first lecture, I can see how they would come up during the 2nd lecture as questions, why would they come up in a written exam?
It doesn't make sense.
If you don't understand it by now, do yourself a favor and fail the class.
Hand in a blank piece of paper and go watch netflix or something.
And if you still want to get that education tomorrow, apply yourself and catch up with the work you lost.
If you don't want to get the education, drop out while you haven't invested too much time and do something else.

Well thank you all I wasn't expecting this much spoonfeeding. My tummy is full. I'll end it here.

Write it in python and use cython to convert it to C++

Words of wisdom right here and I had a lot of thoughts about this. I've already failed the exam and now I'm taking it again and honestly I don't think I can learn enough in 4 days. I still don't know what I'm gonna do about education. I'm not decided if to apply myself or not yet so I'm doing the minimum to pass the year. I really don't know what to do because I don't enjoy college because it's exactly like highschool, a lot of subjects which very few I enjoy. I would like to apply myself to only one. Honestly I would drop out and go to shit tier college just for degree and get into programming.