Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator overloading question

operator overloading question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    civicrico23
    wrote on last edited by
    #1

    How do I go about starting this? Any help will be greatly appreciated. Using the existing point class below, add overloaded operator methods to perform the following: 1. Binary addition 2. Binary substraction 3. Binary multiplication 4. Binary division - division by zero should result in a point with >coordinates (0,0) returned. The overloaded operator method functions should perform under the >following conditions: 1. point operator point; i.e. p+p, p-p, p*p, p/p 2. point operator int; i.e. p+i, p-i, p*i, p/i 3. int operator point; i.e. i+p, i-p, i*p, i/p Point Class //Class Point //File: Point.h #ifndef _POINT_H_ #define _POINT_H_ using namespace std; class point { private: int _x; int _y; public: point() //default constructor } _x=0; _y=0; } point(int x, int y) { _x=x; _y=y; } ~point() //destructor {} void print(); void set(int,int); }; #endif >/*-------------------------------Point.cpp-------------------------*/ // Class Point Member Definitions // File: Point.cpp #include #include "point.h" void point::print() { cout << "Point (" << _x << "," << _y << ")\n"; } void point::set(int x, int y) { _x = x; _y = y; } >/*---------------- TestPoint.cpp ------------------------*/ // Class Point Test Program // File: TestPoint.cpp #include #include "point.h" using namespace std; void main (void) { point p1; point p2(5,6); point p3; int x; int y; cout << "Input p3 x coordinate: "; cin >> x; cout << "Input p3 y coordinate: "; cin >> y; p3.set(x,y); p1.print(); p2.print(); p3.print();

    M 1 Reply Last reply
    0
    • C civicrico23

      How do I go about starting this? Any help will be greatly appreciated. Using the existing point class below, add overloaded operator methods to perform the following: 1. Binary addition 2. Binary substraction 3. Binary multiplication 4. Binary division - division by zero should result in a point with >coordinates (0,0) returned. The overloaded operator method functions should perform under the >following conditions: 1. point operator point; i.e. p+p, p-p, p*p, p/p 2. point operator int; i.e. p+i, p-i, p*i, p/i 3. int operator point; i.e. i+p, i-p, i*p, i/p Point Class //Class Point //File: Point.h #ifndef _POINT_H_ #define _POINT_H_ using namespace std; class point { private: int _x; int _y; public: point() //default constructor } _x=0; _y=0; } point(int x, int y) { _x=x; _y=y; } ~point() //destructor {} void print(); void set(int,int); }; #endif >/*-------------------------------Point.cpp-------------------------*/ // Class Point Member Definitions // File: Point.cpp #include #include "point.h" void point::print() { cout << "Point (" << _x << "," << _y << ")\n"; } void point::set(int x, int y) { _x = x; _y = y; } >/*---------------- TestPoint.cpp ------------------------*/ // Class Point Test Program // File: TestPoint.cpp #include #include "point.h" using namespace std; void main (void) { point p1; point p2(5,6); point p3; int x; int y; cout << "Input p3 x coordinate: "; cin >> x; cout << "Input p3 y coordinate: "; cin >> y; p3.set(x,y); p1.print(); p2.print(); p3.print();

      M Offline
      M Offline
      mylzw
      wrote on last edited by
      #2

      homework ??? add these code in the class point... class point { //old code..... // // my add constructor... int. point(int single) { _x=single; _y=single; } // overloaded operator methods .... friend point operator +(const point& p1,const point& p2) { return point(p1._x+p2._x , p1._y+p2._y); } friend point operator -(const point& p1,const point& p2) { return point(p1._x-p2._x , p1._y-p2._y); } friend point operator *(const point& p1,const point& p2) { return point(p1._x+p2._x , p1._y*p2._y); } friend point operator /(const point& p1,const point& p2) { if (p2._x==0 || p2._y==0) return point(0 , 0); else return point(p1._x/p2._x , p1._y/p2._y); } //~.. }; //then you can using like.... p3=p1+p2; p3=p1-p2; p3=p1*p2; p3=p1+1; p3=p1/100; p3=p1/0; .......... ......

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups