Needed explanation with comments

Apr 19, 2016 at 10:01am
Hello, i am trying to figure out what does this code mean

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
#include<iostream>
using namespace std;
class Point {
	float x;
	float y;
public:
	Point(float x=0, float y=0) {this->x = x; this->y = y;}
	Point (const Point& p) { x = p.x; y = p.y;}
	~Point() {	}
	void Print() const { cout << "(" << x << ", " << y <<")"; }
	
	Point operator-() {
		Point result (-x, -y);
		return result;
	}
	
	Point operator*(float number) {
		Point result (number*x, number*y);
		return result;
	}
	
	friend Point operator*(float number, const Point& p);
};

Point operator*(float number, const Point& p) {
	Point result (number*p.x, number*p.y);
	return result;
}


int main () {
	
	Point P(1,3);
	P.Print();
	Point Q = -P;
	Q.Print();
	Point R = 2*P;
	R.Print();
	Point T = P*2;
	T.Print();
	return 0;
}

I am very new to object oriented programming, so if someone could edit this code with explanation comments in every line it would be very helpful, i need to know more about pointers and destructors and how do they work. Thank you.
Last edited on Apr 19, 2016 at 10:03am
Apr 19, 2016 at 10:08am
There are no pointers used anywhere in the code you've posted (except for this which is only used because whoever wrote the code didn't think more carefully about using helpful names for their variables).

The destructor in the Point class does absolutely nothing.

If you want to learn about pointers and destructors, this isn't really a helpful example of code to learn about them from!

Can you be specific about which bits of the code you don't understand?
Last edited on Apr 19, 2016 at 10:10am
Topic archived. No new replies allowed.