Facing
---- Overload
Author: HolyFire
One thing to say is a matter of consideration. Sometimes our sentence is suitable for many aspects, and these areas are similar. For example, the addition, addition can be used for, integers and aligns. This goal can be implemented in C .
INT INTADD (int V1, INT V2)
{
RETURN V1 V2;
}
Float Floatadd (Float V1, Float V2)
{
RETURN V1 V2;
}
The way used is like this
INT IA = 1, IB = 2, IAB = 0;
FLOAT FA = 1.0, FB = 2.0, Fab = 0.0;
IAB = INTADD (IA, IB);
Fab = floatadd (fa, fb);
This looks very intuitive, the readability is not strong, and our purpose is to see if it is increasing.
Smart readers may find that the return values and parameters of the two functions are different, so they can distinguish two different behaviors. This feature can not be used, the answer is possible, C is intimate for us everything. The principle is very simple, turn the return value of the function into a string with a reversible encoding method, which is behind the function name, the compiler shields all of this, and the user can use the same name to use different functions.
That's it
Int Add (int V1, INT V2)
{
RETURN V1 V2;
}
Float Add (Float V1, Float V2)
{
RETURN V1 V2;
}
INT IA = 1, IB = 2, IAB = 0;
FLOAT FA = 1.0, FB = 2.0, Fab = 0.0;
IAB = Add (IA, IB);
FAB = Add (Fa, FB);
Oh ~~~~, the compilation passed, and the result is not wrong.
Such a code seems to be more than the original, we can skilful things we are not interested in those things.
Another aspect of overloading is an overloaded operator, and is different, it needs to use a keyword Operator. This can use the operator in its own way. So immediately use the operator to rewrite everything above it.
Int Operator (int V1, INT V2)
{
RETURN V1 V2;
}
Float Operator (Float V1, Float V2)
{
RETURN V1 V2;
}
INT IA = 1, IB = 2, IAB = 0;
FLOAT FA = 1.0, FB = 2.0, Fab = 0.0;
IAB = IA IB;
FAB = FA FB;
Oh ~~~, this code is really beautiful, but it is not practical, why, people who have used C know that Int, Float this basic type, you can use the operator directly, then Not a basic type. The complex class is not a basic type in C .
In this example, the Complex class operator overload is overloaded, and the << operator is overloaded, allowing Ostream to accept the output of Complex.
Change the original meaning of the operator is not a good idea. It should try to ensure the original meaning of the operator. We use its original meaning to make the program easy to understand, the writing is convenient, the << and >> in iostream is due to a lot Use the formation of a conventional product, their original meaning is to make a shift operation. #include
Unsing namespace std;
Class complex {
Private:
INT REAL;
Int image;
PUBLIC:
Complex (int _real = 0, int _image = 0): Real (_real), Image (_IMAGE) {}
INT & REAL () {Return Real;}
INT & image () {return image;}
Complex Operator (Complex Const & V)
{
Complex W (Real v.real, Image V.Image);
Return W;
}
}
Ostream & Operator << (Ostream & Out, Complex & V)
{
IF (v.real ())
OUT << v.real ();
IF (v.image ()> 0)
COUT << " ";
ELSE IF (v.image () == 0)
Return Cout;
Else
COUT << "-";
COUT << v.image () << "i";
Return Out;
}
void main ()
{
Complex W (1, 2), V (3, 4);
Cout << (w v) << Endl;
}
The code looks very simple and is readable.
We can see that << lost his original meaning, and turned into an OSTREAM class interface, in fact, the overload operator is in nature and overload function.
The purpose of using overload is to increase the readability of the code, and it can also improve the degree of reusability.
2001/8/22
Ding Ning