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?
#include<iostream>
#include<cassert>
usingnamespace 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;
}
booloperator==(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
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.
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();