10. C++ operator overloading

1. Operator + overloading

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
int x;
int y;

public:
Point() {}
Point(int x, int y) : x(x), y(y) {}

int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point & amp;p1, Point & amp;p2);
friend Point operator + (Point & amp;p1, Point & amp;p2);
};

Point add(Point & amp;p1, Point & amp;p2)
{
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

Point operator + (Point & amp;p1, Point & amp;p2)
{
cout<<"Point operator + (Point & amp;p1, Point & amp;p2)"<<endl;
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}


int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);

Point sum = p1 + p2;
sum.printInfo();

return 0;
}

2. Operator overloading i + + and + + i

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
int x;
int y;

public:
Point() {}
Point(int x, int y) : x(x), y(y) {}

int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point & amp;p1, Point & amp;p2);
friend Point operator + (Point & amp;p1, Point & amp;p2);
friend Point operator + + (Point & amp;p);
friend Point operator + + (Point & amp;p, int a);
};

Point add(Point & amp;p1, Point & amp;p2)
{
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

Point operator + (Point & amp;p1, Point & amp;p2)
{
cout<<"Point operator + (Point & amp;p1, Point & amp;p2)"<<endl;
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

/* Point p(1,2); + + p; */
Point operator + + (Point &p)
{
cout<<" + + p"<<endl;
p.x + = 1;
p.y + = 1;
return p;
}

/* Point p(1,2); p + + ; */
Point operator + + (Point & amp;p, int a)
{
cout<<"p + + "<<endl;
Point n;
n = p;
p.x + = 1;
p.y + = 1;
return n;
}

int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);

Point n = + + p1;
n.printInfo();
p1.printInfo();

cout << "******************"<<endl;

Point m = p2 + + ;
m.printInfo();
p2.printInfo();

return 0;
}

3. Return reference

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
int x;
int y;

public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}

Point(const Point & p)
{
cout<<"Point(const Point & amp; p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}

int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point & amp;p1, Point & amp;p2);
friend Point operator + (Point & amp;p1, Point & amp;p2);
friend Point operator + + (Point & amp;p);
friend Point operator + + (Point & amp;p, int a);
};

Point add(Point & amp;p1, Point & amp;p2)
{
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

Point operator + (Point & amp;p1, Point & amp;p2)
{
cout<<"Point operator + (Point & amp;p1, Point & amp;p2)"<<endl;
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

/* Point p(1,2); + + p; */
Point & amp; operator + + (Point & amp;p) //return reference
{
cout<<" + + p"<<endl;
p.x + = 1;
p.y + = 1;
return p;
}

/* Point p(1,2); p + + ; */
Point operator + + (Point & amp;p, int a) //Cannot return a reference to n, n is a local variable
{
cout<<"p + + "<<endl;
Point n;
n = p;
p.x + = 1;
p.y + = 1;
return n;
}

int main(int argc, char **argv)
{
Point p1(1, 2);

cout<<"begin"<<endl;
+ + p1;
cout << "******************"<<endl;

p1 + + ;
cout<<"end"<<endl;


return 0;
}

Value return: Returns a local variable defined within the function. This variable is created when the function is executed and is destroyed when the execution is completed. Only the value is returned, which is inefficient.

Reference return: high efficiency

Selection principle: does not affect the operation results, efficiency is given priority

4. Overload cout<<

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
int x;
int y;

public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}

Point(const Point & p)
{
cout<<"Point(const Point & amp; p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}

int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point & amp;p1, Point & amp;p2);
friend Point operator + (Point & amp;p1, Point & amp;p2);
friend Point & amp; operator + + (Point & amp;p);
friend Point operator + + (Point & amp;p, int a);
friend ostream & amp; operator<<(ostream & amp;o, Point p);
};

Point add(Point & amp;p1, Point & amp;p2)
{
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

Point operator + (Point & amp;p1, Point & amp;p2)
{
cout<<"Point operator + (Point & amp;p1, Point & amp;p2)"<<endl;
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}

/* Point p(1,2); + + p; */
Point & amp; operator + + (Point & amp;p)
{
cout<<" + + p"<<endl;
p.x + = 1;
p.y + = 1;
return p;
}

/* Point p(1,2); p + + ; */
Point operator + + (Point & amp;p, int a)
{
cout<<"p + + "<<endl;
Point n;
n = p;
p.x + = 1;
p.y + = 1;
return n;
}

ostream & amp; operator<<(ostream & amp;o, Point p) //Return the reference of cout
{
cout<<"("<<p.x<<", "<<p.y<<")";
return o;
}

int main(int argc, char **argv)
{
Point p1(1, 2);
Point m, n;

cout<<"begin"<<endl;
m = + + p1;
cout << "******************"<<endl;

n = p1 + + ;
cout<<"end"<<endl;

cout<<m<<" "<<n<<endl;

return 0;
}

5. Put it in the class and overload it

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
int x;
int y;

public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}

Point(const Point & p)
{
cout<<"Point(const Point & amp; p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}

int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}

Point operator + (Point &p)
{
cout<<"operator + "<<endl;
Point n;
n.x = this->x + p.x;
n.y = this->y + p.y;
return n;
}
\t
/* Point p(1,2); + + p; */
Point & operator + + (void)
{
cout<<"operator + + (void)"<<endl;
this->x + = 1;
this->y + = 1;
return *this;
}
\t
/* Point p(1,2); p + + ; */
Point operator + + (int a)
{
cout<<"operator + + (int a)"<<endl;
Point n;
n = *this;
this->x + = 1;
this->y + = 1;
return n;
}

friend Point add(Point & amp;p1, Point & amp;p2);
friend ostream & amp; operator<<(ostream & amp;o, Point p);
};

Point add(Point & amp;p1, Point & amp;p2)
{
Point n;
n.x = p1.x + p2.x;
n.y = p1.y + p2.y;
return n;
}


ostream & amp; operator<<(ostream & amp;o, Point p)
{
cout<<"("<<p.x<<", "<<p.y<<")";
return o;
}

int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point m, n;

m = p1 + p2; /* m = p1.operator + (p2); */
cout<<"add p1,p2 = "<<m<<endl;

cout<<"begin"<<endl;
m = + + p1; /* m = p1.operator + + (); */
cout<<"m = "<<m<<" "<<"p1 = "<<p1<<endl;
cout << "******************"<<endl;

n = p1 + + ; /* m = p1.operator + + (0); */
cout<<"n = "<<n<<" "<<"p1 = "<<p1<<endl;
cout<<"end"<<endl;

return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57578 people are learning the system