Composition

any clus why its not working (the colmpiler pointing me at ostream problem)
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
 #include<iostream>
using namespace std;
class Point {
	int x;
	int y;
public:
	Point() :x(0), y(0) {}
	Point(int a, int b) :x(a), y(b) {}
	Point(const Point& p) {
		x = p.x;
		y = p.y;
	}
	Point operator=(const Point& p) {
		x = p.x;
		y = p.y;
		return *this;
	}
	void print() {
		cout << "(" << x << "," << y << ")" << endl;
	}
};
class Cpoint {
	int size;
	Point *array;
public:
	Cpoint(int s) :size(s) {
		if(size>0)
		array = new Point[size];
	}
	void set(int id, Point &p) {
		if (size > id) {
			array[id] = p;
		}
	}
	friend ostream &operator<<(ostream &out, const Cpoint &p) {
		for (int i = 0; i < p.size; i++) {
			out << p.array[i].print() << " ";
			return out;
		}
	}



};


};
int main() {
	Point p1(2, 3);
	Point p2(1, 4);
	Cpoint cp(2);
	cp.set(0, p1);
	cp.set(1, p2);
	cout << cp;
	system("pause");
	return 0;



}
The print function has return type void which means it doesn't return anything. Passing void to the << operator (or any other function for that matter) doesn't make sense.

Instead of having a print function, why not overload the << operator for the Point class, as you're doing with the Cpoint class?
Last edited on

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
#include<iostream>
#include<cassert>
using namespace std;
class Point {
	int x;
	int y;
public:
	Point() :x(0), y(0) {}
	Point(int a, int b) :x(a), y(b) {}
	Point(const Point& p) {
		x = p.x;
		y = p.y;
	}
	Point operator=(const Point& p) {
		x = p.x;
		y = p.y;
		return *this;
	}

	Point operator+=(const Point &p) {
		x = p.x;
		y = p.y;
		return *this;

	}
	Point operator+(const Point &p) {
		Point temp;
		temp.x = x + p.y;
		temp.y = y + p.y;
		return temp;

	}
	Point operator++() {
		x++;
		y++;
		return *this;

	}
	bool operator==(const Point &p) {
		return (x == p.x && y == p.y);

	}
	friend ostream &operator<<(ostream &out, const Point & p) {
		cout << "(" << p.x << "," << p.y << ")" << " ";
		return out;
	}


};
class Cpoint {
	int size;
	Point *data;
public:
	Cpoint(int s) :size(s) {
		if (size > 0)
			data = new Point[size];

}
	void set(int index, const Point &p) {
		if (size > index) {
			data[index] = p;
		}
	}
	friend ostream &operator<<(ostream &out, const Cpoint &p) {
		for (int i = 0; i < p.size; i++) {
			out << p.data[i] << " ";
			
		}
		return out;
	}





};


int main() {
	Point p1(2, 3);
	Point p2(1, 4);
	Cpoint cp(2);
	cp.set(0, p1);
	cp.set(1, p2);
	cout << cp;
	
	system("pause");
	return 0;



}

i succssed to do it thanks alot
if you can help me with another quastion i be happy (:
if in cPoint i had double pointer to data
1
2
3
4
5
6
7
8
9
10
11
class Cpoint {
	int size;
	Point **array;
public:
	Cpoint(int s) :size(s) {
		if(size>0)
		array = new Point*[size];
for(int i=0;i<size;i++){
data[i]= new point?// how can i put here point in for loop or call constractur of point

	}

i saw someone asked it in this forum today and i didnt understand what hes trying to do.
and i wanted to make in main like point(2,p1,p2)
2- is the size and p1 and p2 is points
Last edited on
You should write the output to out instead of cout in Point's operator<< otherwise it will not work correctly when outputting to anything other than cout.

As for using double pointers, if you're not knowing why you are doing it, then I have to ask why are you doing it? Pointers complicate the code so if you don't have a good reason for using them I would advice you to avoid them.
Last edited on
i saw alot of quastion with composition with double pointers .
so i wanted to ask you .

and yea i fixed it was some mistake i done with cout and out.
like if i wanted to co cp(p1,p2);
how i manage to do it ..
with out using set like cp.set();
Last edited on
Topic archived. No new replies allowed.