■ Overload: The same function name is used in the same scope, but the number of function parameters or functions of different types are used.
For example:
# Include <iostream>
Using namespace std;
Class printer {
Public:
Void print (int data ){};
Void print (float data ){};
Void print (const char & pStr, size_t size ){};
// Other code
};
Class stringPrinter: public printer {
Public:
/* Using printer: print; * // introduce the print function declaration in the base class to the scope of the derived class.
// Function definition without print (int) in the scope of the derived class (hidden by print (const string &) in the derived class)
Void print (const string & str ){};
// Other code
};
Int main (){
StringPrinter myPrinter;
MyPrinter. print (2011 );
Return 0;
}
■ Rewrite: Re-implement the virtual functions in the base class in the derived class, that is, customize the base class virtual functions in the derived class.
Note:
1) function rewriting has nothing to do with the access level (public, private, and protected) (but generally, the rewrite function of a derived class should have the same access level as the corresponding function of the base class)
For example:
Class CWorker {
Public:
Virtual void Working (){...}
};
------------------------------------
Class CDriver: public CWorker {
Private:
Virtual void Working (){......}
};
2) const may be the rewriting of the virtual member function (the constant attribute is part of the function signature)
For example:
Class CDriver: public CWorker {
Private:
Virtual void Working () const {...} // The Working () function of the base class is not overwritten.
};
3) The rewrite function must have the same return type as the original function (the return type of the function is not part of the function signature, and the return type must be the same)
For example:
Class CDriver: public CWorker {
Private:
Virtual bool Working () const {...} // error: the return type of the rewrite virtual function is different.
};
■ Hide: a function in a derived class shields non-virtual functions with the same name in the base class. When calling a member function of the class, the compiler looks up the function definition step by step along the inheritance chain of the class, if it is found, it will stop, that is, the compiler's function search will not be able to reach the base class, implementing the hidden)