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

Overload the -- operator for the coord class. Create its both prefix and postfix forms

      

Overload the -- operator for the coord class. Create its both prefix and postfix forms.


  

Answers


Davis
//overload the -- relative to coord class.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord() {x=0; y=0;}
coord (int i, int j) {x=i; y=j;}
void get(int &i, int &j) {i=x}
coord operator--(int notused); //postfix
};
// Overload prefix -- for coord class.
coord coord::operator --()
{
x--;
y--;
return *this;
}
/erload postfix -- for coord class.
coord coord::operator--(int notused)
{
x--;
y--;
return *this;
}
int main()
{
coord o1(10, 10);
int x, y;
o1--;decrement an object
o1.get_xy(x, y);
cout << "(o1--) x: " << x << " y: " << y << "\n";
--o1; //decrement an object
o1.get_xy(x, y);
cout << "(--o1) x: << x << ", y: " << y << "\n";
return 0;

Githiari answered the question on June 8, 2018 at 18:35


Next: Overload the < and > operators relative to the coord class.
Previous: 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...

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


Learn High School English on YouTube

Related Questions