Part of the text is taken from high-quality C ++/C programming.
Link: http://man.chinaunix.net/develop/c&c++/c/c.htm#_Toc520634042 1. Reload overload 1) the same range is in the same class. 2) function names are the same and parameters are different. 3) virtual is optional. This is usually used in the design. For example, a role in the game can be designed to speak to: void senddialog (const char * content); // default void senddialog (const char * content, char color); // content color void senddialog (const char * content, char effect); // special effect... 2. Overwrite override1) different scopes are located in the base class and derived class respectively. 2) The function names and parameters are the same. 3) The base class contains the virtual keyword. This is also widely used. Now the override keyword is added, which means that the derived class tells the base class. I don't use the method you provided, but I want to implement it myself. It is said to be quite vulgar, and I have not seen any examples: calculation area: Class shape {public: Virtual float area (float X, float y) {return 0 ;} //...} class rectangle: Public shape {public: Virtual float area (float X, float y) {return x * y ;}} class triangle: Public shape {public: virtual float area (float X, float y) {return (x * y * 0.5 );}} 3 hide 1) functions in the derived class have the same name as those in the base class. If the parameters are different, functions with the same name in the base class are hidden. 2) the function in the derived class has the same name and parameters as the function in the base class. The base class does not have the virtual keyword and the function with the same name is hidden. (If there is a keyword with the virtual keyword, It is covered.) the so-called hidden, in other words, cannot be called. For example 1): Class rectangle
{
Public:
Void setwith (double W) {m_width = W ;}
Void setheight (double H) {m_height = H ;}
PRIVATE:
Double m_width;
Double m_height;
};
Class square: Public rectangle
{
Public:
Void setwith (double W, bool module)
{
Rectangle: setwith (w );
Rectangle: setheight (w );
}
Void setheight (double H)
{
Rectangle: setwith (h );
Rectangle: setheight (h );
}
}; Void func () {square s; // compilation fails because the derived class hides the setwith function of the base class, And // cannot call the setwith function of the base class. S. setwith (32);} (2): For the vulgar point, the derived class has the same base class. Why do we need to call the base class.
Special attention should be paid to hiding data.Class rectangle
{
Public:
Void setwith (double W) {m_width = W ;}
Void setheight (double H) {m_height = H ;}
PRIVATE:
Double m_width;
Double m_height;
};
Class square: Public rectangle
{
Public:
Void setwith (double W)
{
Rectangle: setwith (w );
Rectangle: setheight (w );
}
Void setheight (double H)
{
Rectangle: setwith (h );
Rectangle: setheight (h );
}
}; Void func (rectangle & R) {R. setwith (32);} void main () {square s; // In func, rectangle: setwith, // indicates that the height of a square is an arbitrary value func (s ); return 0 ;}