1. In C + + the compiler distinguishes between a predecessor or a post operation by inserting the keyword int into the operator's overloaded function parameter table. Like what:
1 #include "stdafx.h"
2 #include <iostream>
3
4 class tdpoint//three-dimensional coordinates
5 {
6 Private:
7 int x;
8 int y;
9 int z;
Public:
Tdpoint (int x=0,int y=0,int z=0)
12 {
this->x=x;
this->y=y;
this->z=z;
16}
Tdpoint operator++ ();//member function overloaded pre-operator + +
Tdpoint operator++ (int);//member function overloaded post operator + +
* Friend Tdpoint operator++ (tdpoint& point);//friend function overloaded pre-operator + +
Friend Tdpoint operator++ (tdpoint& point,int);//friend function overloaded post operator + +
+ void Showpoint ();
22};
23
Tdpoint tdpoint::operator++ ()
25 {
++this->x;
++this->y;
++this->z;
return*this;//returns the object after increment
30}
31
Tdpoint tdpoint::operator++ (int)
33 {
Tdpoint Point (*this);
this->x++;
Approx. this->y++;
Panax Notoginseng this->z++;
Return point;//returns the object before the increment
39}
40
Tdpoint operator++ (tdpoint& point)
42 {
++point.x;
++point.y;
++point.z;
Return point;//returns the object after increment
47}
48
Tdpoint operator++ (tdpoint& point,int)
50 {
Wuyi Tdpoint point1 (point);
point.x++;
point.y++;
point.z++;
Return point1;//returns the object before the increment
56}
57
Tdpoint::showpoint void ()
59 {
std::cout<< "(" <<x<< "," <<y<< "," <<z<< ")" <<std::endl;
61}
62
int main ()
64 {
Tdpoint Point (1,1,1);
point.operator++ ();//or ++point
Point.showpoint ();//Pre + + operation results
68
point=point.operator++ (0);//or point=point++
Point.showpoint ();//post + + operation results
71
operator++ (point);//or ++point;
Point.showpoint ();//Pre + + operation results
74
point=operator++ (point,0);//or point=point++;
Point.showpoint ();//post + + operation results
77
Return0;
79}
Results:
It is clear from the example code that the Post operator overload function has an int type parameter than the predecessor operator overload function, which is just to differentiate the predecessor and post operators, and it has no effect. Therefore, an argument of type int can take any value when the post-operator overload function is called.
2. In C + +, the operator "<<" and ">>" are defined as the left and right displacement operators. Because they are overloaded in the iostream header file, they can be used for the output and input of basic data.
#include "stdafx.h"
#include <iostream>
int main ()
{
int a=10;
std::cout<< "a=" <<a<<std::endl;//operator "<<" for output after overloading
a=a>>2;//Right Shift operator
std::cout<< "Shift right 2 bit: a=" <<a<<std::endl;
std::cout<< "Please enter an integer a:";
std::cin>>a;//operator ">>" after overloading for input
a=a<<2;//left shift operator
std::cout<< "Shift left 2 bit: a=" <<a<<std::endl;
Return0;
}
Results:
The insert operator "<<" is the binocular operator, the left operand is the object of the output stream class Ostream, and the right operand is the system-predefined base type data. Header file Iostrem Its overloaded function prototype is ostream& operator<< (ostream&, type name); The type name refers to the basic type data. However, if you want to output user-defined type data, you need to overload the operator "<<" because the operator's left operand must be an object of the Ostream class, so the insert operator "<<" can only be a friend or normal function of a class, and cannot be a member function of another class. General definition Format:
ostream& operator<< (ostream&, custom class name &);
The extraction operator ">>" is also the case, the left operand is an object of the IStream class, and the right operand is the base type data. Header file Iostrem Its overloaded function prototype is istream& operator>> (istream&, type name), and the extract operator cannot be a member function of another class, either a friend function or a normal function. Its general definition format is:
istream& operator>> (istream&, custom class name &);
I still use the complex class (plural Class) in the previous section to give an example:
#include "stdafx.h"
#include <iostream>
Class Complex//plural
{
private://Private
Double real;//Real number
Double imag;//Imaginary
Public
Complex (double real=0,double imag=0)
{
this->real=real;
this->imag=imag;
}
Friend std::ostream&operator<< (std::ostream& o,complex& com);//friend function Overload extraction operator "<<"
Friend std::istream&operator>> (std::istream& i,complex& com);//friend function overloaded insert operator ">>"
};
std::ostream&operator<< (std::ostream& o,complex& com)
{
std::cout<< "The plural of the input:";
o<<com.real;
if (com.imag>0)
o<< "+";
if (com.imag!=0)
o<<com.imag<< "I" <<std::endl;
return o;
}
std::istream&operator>> (std::istream& i,complex& com)
{
std::cout<< "Please enter a plural:" <<std::endl;
std::cout<< "Real (real number):";
i>>com.real;
std::cout<< "Imag (imaginary):";
i>>com.imag;
return i;
}
int main ()
{
Complex com;
std::cin>>com;
std::cout<<com;
Return0;
}
Results:
Source: http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/13/2137033.html
Go Operator overloading of C + + (2)