Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Coord {
- float x, y;
- public:
- Coord() {}
- Coord(float a, float b) : x(a), y(b) {}
- friend Coord operator + (const Coord &a, const Coord &b);
- friend Coord operator + (const Coord &a, const float floatB);
- friend Coord operator - (const Coord &a, const Coord &b);
- friend Coord operator - (const Coord &a, const float floatB);
- bool operator == (const Coord &a) { return (this -> x == a.x && this -> y == a.y); }
- bool operator == (const float floatA) { return (this -> x == floatA && this -> y == floatA); }
- Coord& operator = (const Coord &a) {
- x = a.x; y = a.y;
- return *this;
- }
- Coord& operator = (const float a) {
- x = y = a;
- return *this;
- }
- void set(char c) {
- cout << endl << "Enter x & y of coordinate " << c << ": ";
- cin >> x >> y;
- return;
- }
- void get(char c) {
- cout << endl << "x & y of coordinate " << c << ": " << x << " " << y << endl;
- return;
- }
- };
- int main() {
- Coord a, b, c;
- float x;
- a.set('a');
- b.set('b');
- cout << "Enter number x: ";
- cin >> x;
- cout << endl << "==== operator + ====" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "x = " << x << endl;
- c = a + b;
- cout << endl << "c = a + b" << endl;
- c.get('c');
- c = a + x;
- cout << endl << "c = a + x" << endl;
- c.get('c');
- //
- cout << endl << "==== operator - ====" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "x = " << x << endl;
- c = a - b;
- cout << endl << "c = a - b" << endl;
- c.get('c');
- c = a - x;
- cout << endl << "c = a - x" << endl;
- c.get('c');
- //
- cout << endl << "==== operator == ====" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "x = " << x << endl;
- c = a - b;
- cout << endl << "a == b = " << (a == b) << endl;
- cout << endl << "a == x = " << (a == x) << endl;
- //
- cout << endl << "==== operator = ====" << endl;
- a.get('a');
- cout << endl << "x = " << x << endl;
- c = a;
- cout << endl << "c = a" << endl;
- c.get('c');
- c = x;
- cout << endl << "c = x" << endl;
- c.get('c');
- return 0;
- }
- Coord operator + (const Coord &a, const Coord &b) {
- Coord c(a.x + b.x, a.y + b.y);
- return c;
- }
- Coord operator + (const Coord &a, const float floatB) {
- Coord c(a.x + floatB, a.y + floatB);
- return c;
- }
- Coord operator - (const Coord &a, const Coord &b) {
- Coord c(a.x - b.x, a.y - b.y);
- return c;
- }
- Coord operator - (const Coord &a, const float floatB) {
- Coord c(a.x - floatB, a.y - floatB);
- return c;
- }
Add Comment
Please, Sign In to add comment