Overwrite, hide, and reload.

Source: Internet
Author: User

Article from pengzhixi
(Click the name to go to the original site)

1. Reload, overwrite, and hide

1). Overload: "overload" occurs when a member function has the following features"
A. Same range
(In the same class)
B. The function name is the same.
C. Different parameter types (implicit type conversion is not allowed)
D. Virtual keywords are optional
 
2). Overwrite (also called "inherit"): Refers to a function of a derived class that overwrites a base class function. The features are as follows:
A. different scopes (located in the base class and derived class respectively)
B. The function name is the same.
 
C. The parameters are the same
D. Basic functions must have virtual keywords.
3). Hide: the function of the derived class shields the base class functions with the same name. The rules are as follows:
 
A. if the function of the derived class has the same name as the function of the base class, but the parameter is different, the function of the base class will be hidden no matter whether there is a virtual keyword. Be careful not to confuse the function with the overload)
B.
If the function of the derived class has the same name as the function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden (Be sure not to confuse it with overwrite)
  2.
See the following example code:


# Include
Using STD: cout;
Using
STD: Endl;
Class base
{
Public:
Virtual void F (float
X) {cout <"base: F (float)" <x <Endl ;}
  
Void g (float X) {STD: cout <"base: G (float)" <X
<STD: Endl ;}
Void H (float X) {STD: cout <
"Base: H (float)" <x <};
Class derived: public Base
 
{
Public:
Virtual void F (float X) {STD: cout <
"Derived: F (float)" <x <STD: Endl ;}
Void g (int x ){
STD: cout <"derived: G (INT)" <x <STD: Endl ;}
 
Void H (float X) {STD: cout <"derived: H (float)" <X
<STD: Endl ;}
};
Void main (void)
{
Derived D;
 
Base * pb = & D;
Derived * Pd = & D;
  
Pb-> F (3.14f); // derived: F (float) 3.14
  
Pd-> F (3.14f); // derived: F (float) 3.14
  
Pb-> G (3.14f); // base: G (float) 3.14
  
Pd-> G (3.14f); // derived: G (INT) 3
Pb-> H (3.14f); // base: H (float)
3.14
Pd-> H (3.14f); // derived: H (float) 3.14
}
  3.
Explanation


In Rows 27 and 28, the derived: F (float
X) inherits (overwrites) The base: F (float) of the base class through the Virtual keyword.
X) method, so no matter whether the base class pointer or the derived class pointer is used here, the final call is actually the derived: F (float X) method. This is what we generally expect.
 
In row 30, because the base: G () of the base class is not declared with the virtual keyword, it will not be overwritten by the derived: G () method of the derived class. Therefore, the base class
When the needle is accessed, it can only access base: G (float X). When 31 rows pass the pointer of a derived class, the methods that can be accessed include base: G (float
X) and derived: G (int
X), although the method names are the same and the parameters are different (it seems) to comply with the overload standard, they are classified into different "domains", so the overload will not occur. In this case, derived :: G (int
X) You can only hide the base: G (float X.
Same as above, only base: H (float) can be accessed through the base class pointer in row 33rd.
X), because this method is not declared by the virtual keyword, It is not declared by the derived method derived: H (float
X) "replace", so base: H (float X) is called ). The methods that can be accessed through the pointer of the derived class in Row 3 also include base: H (float
X) Conflicts with derived: H (float X). In this case, the "hidden" rule of C ++ works, so the derived method derived: H (float
X) The base class method base: H (float X) "hidden", so derived: H (float X) is called.
  4. Summary

 
The "overload", "inheritance", and "hidden" mechanisms of C ++ are more complex than expected, and this highlights the importance of virtual keywords. Therefore, when a derived class exists
Methods that may be implemented in the derived class are declared with the virtual keyword. Unless in special cases, such as when you need to check the pointer type.
# Include
 
Using STD: cout;
Using STD: Endl;
Class base
{
  
Public:
Void checktype (void) {cout <"This's base PTR"
<Endl ;}
};
Class derived: public Base
{
  
Public:
Void checktype (void) {cout <"This; s derived PTR"
<Endl ;}
};
Void main (void)
{
Derived D;
 
Base * pb = & D;
Derived * Pd = & D;
  
Pb-> checktype (); // This's base PTR
Pd-> checktype (); // This's
Derived PTR
}

Read master blogs, learn, and make progress.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.