Get premium membership and access questions with answers, video lessons as well as revision papers.

Using a friend, show how to overload the -- relave to the coord class. Define both the prefix and postfix forms.

      

Using a friend, show how to overload the -- relave to the coord class. Define both the prefix and postfix forms.

  

Answers


Davis
#include
using namespace std;
class coord {
int x, y; //coordinate valuespublic:
coord() {x=0; y=0;}
coord(int i, int j) {x=i; y=j;}
void get_xy(int &i, int &j) {i=x; j=y;}
friend coord operator--(coord &ob);//preiend coord operator--(coord &ob, int notused);//postfix
};
coord operator--(coord &ob)
{
ob.x--;
ob.y--;
return ob;
}
coord operator--(coord &obnt notused)
{
ob.x--;
ob.y--;
return ob;
}
int main()
{
coord o1(10, 10);
int x, y;
--o1; //decrement an object
o1.get_xy(x, y);
cout << "(--o1) "<< x <<" y: << y << "\n";
o1--; //decrement an object
o1.get_xy(x, y);
cout << "(o1--) x: "<return 0;
Githiari answered the question on June 8, 2018 at 18:44


Next: Overload the coord class so it can use coord object in operations in which an integer value can be multiplied by each coordinate. Allow the...
Previous: Given the following class declaration; class dynarray { int *p; int size; public: dynarray(int s); int &put(int i); int get(int i); //create operator=( function }; fill in all the details that will create a...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions