C++ problem with the function
Thank you.
Last edited on
Here is a simple example of a point class. Let us know if you have any further questions.
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
|
#include <iostream>
using namespace std;
class cartesianCoordinates {
private:
int x;
int y;
public:
// constructor using initializer list
cartesianCoordinates(int x, int y) : x(x), y(y) {}
int getX() const {
return x;
}
void setX(int x) {
cartesianCoordinates::x = x;
}
int getY() const {
return y;
}
void setY(int y) {
cartesianCoordinates::y = y;
}
friend ostream &operator<<(ostream &os, const cartesianCoordinates &coordinates);
};
ostream &operator<<(ostream &os, const cartesianCoordinates &coordinates) {
os << "(" << coordinates.x << "," << coordinates.y << ")";
return os;
}
int main() {
// declaring a point
cartesianCoordinates aPoint(1, 2);
// printing a point using overloaded << operator
cout << aPoint;
return 0;
}
|
(1,2)
Process finished with exit code 0
Last edited on
Thank you.
Topic archived. No new replies allowed.