Make a point, add two points, become a line

My code does not compile. There is something wrong with the overloading << under the class Line.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include <iostream>
using namespace std;

class Line {
private: 
	OnePoint onevalue; 
	OnePoint twovalue; 
public: 
	Line(OnePoint a, OnePoint b) {
		onevalue = a; 
		twovalue = b; 

	}

	/*OnePoint getonevalue() {
		return onevalue; 
	}

	OnePoint gettwovalue() {
	
		return twovalue; 
	}*/

	friend ostream& operator<<(ostream& print, Line& cLine){
		print << "{"<< cLine.onevalue << ',' << cLine.twovalue << "}"; 
		return print; 
	}
	
};

class OnePoint {
private: 
	double xvalue; 
	double yvalue; 

public:
	OnePoint(double x = 0.0, double y = 0.0) {
		xvalue = x;
		yvalue = y;

	}


	friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
		printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
		return printh;

	}

	void Plus(OnePoint a) {
		xvalue = xvalue + a.xvalue; 
		yvalue = yvalue + a.yvalue; 
	}

	void Minus(OnePoint b) {
	 
		xvalue = xvalue + b.xvalue;
		yvalue = yvalue + b.yvalue;

	}

	OnePoint Plustwo(OnePoint a) {
		return (xvalue + a.xvalue, yvalue - a.yvalue); 
	
	}

	void Change(double a, double b) {
		xvalue += a;
		yvalue += b; 
	}

	void Print(OnePoint b) {
	 
		cout << xvalue << "," << yvalue << endl; 
		
	}

	OnePoint operator+(OnePoint a) {
		OnePoint temp; 
		temp.xvalue = xvalue + a.xvalue; 
		temp.yvalue = yvalue + a.yvalue; 

		return temp; 

	}


};


int main(){

	OnePoint a(3.0, 3.0); 
	OnePoint b(1.0, 1.0);  
	
	a.Print(a); 

	cout << a; 
}
closed account (48T7M4Gy)
Cut and paste Onepoint class before Line class
Oh wow. Thank you. I was about to go crazy here. I am going to have that in mind next time. If one class is using parts of the other, it needs that part over itself.

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);

Line d(a, b);

cout << a << endl;

cout << d << endl;

Finally this code snippet will work.
closed account (48T7M4Gy)
:)
Could you give me an idea of how to add two points

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);

Line d;

d = a+b;

cout d;

becomes {(3.0,3.0),(1.0,1.0)}.

The reason I don't get it exactly is because I am not going to add them like the numbers. I am going to just put the numbers beside each other so it becomes a line.
How do I even write that i code.
Last edited on
That looks like an abuse of operator overloading.
Why? It's what I am going to do but I am having some troubles.

How can I make it so my operator function has access to both the Line class, and the One Point class?
closed account (48T7M4Gy)
How can I make it so my operator function has access to both the Line class, and the One Point class?


I don't think you can perform the overloaded + operation to perform PtA + PtB = LineC that you want because they are different types.

The Line constructor LineC = Line( ptA, ptB ) does that job.
Kemort. I did it. I manged to do it. You can't plus them but you can make them become

OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);

Line d;

d = a+b;

cout d;

becomes {(3.0,3.0),(1.0,1.0)}.
Last edited on
closed account (48T7M4Gy)
That makes sense. Is d in the way you display it a Line object?
Topic archived. No new replies allowed.