[C/C ++] Reload, overwrite, hide, and virtual Keywords of C ++ class member functions

Source: Internet
Author: User

Reload, overwrite, hide, and virtual Keywords of C ++ class member functions

Phoenix (phoenix8848@gmail.com)

1. Reload, overwrite, and hide

1). Overload: "overload" occurs when a member function has the following features"

A. the 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 will be hidden (be careful not to confuse with overwriting)

 

2. See the following example.Code:

# Include
<Iostream>

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 :< span style =" color: #030003 "> cout " base: G (float) " x STD : Endl ;}< br>

Void
H(Float
X){STD::Cout<"Base: H (float )"<X<STD::Endl;}

};

 

Class
Derived:Public
Base

{

Public:

Virtual
Void
F(Float
X){STD::Cout<"Derived: F (float )"<X<STD::Endl;}

void
G ( int
x ) { STD :< span style =" color: #030003 "> cout " derived: G (INT) " x STD : Endl ;}< br>

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) of the derived class inherits (overwrites) The base: F (float X) method of the base class through the Virtual keyword, therefore, no matter whether a base class pointer or a derived class pointer is used, 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, you can only access base: G (float X) when accessing through the base class pointer. The methods that can be accessed when 31 rows pass the pointer of the derived class are base: G (float X) and derived: G (int x), although the two methods have the same method name and different parameters (it seems) comply with the overload standard, however, they are classified into different "domains", so the overload will not occur. In this case, derived: G (int x) can only hide base: G (float X.

Same as above, the method that can be accessed through the base class pointer in row 33rd is only base: H (float X). Because this method is not declared by the virtual keyword, it will not be derived :: H (float X) "replace", so base: H (float X) is called ). While the methods that can be accessed through the pointer of the derived class in row 34th also include base: H (float X) and derived: H (float X), which seems to conflict with each other, in this case, the C ++ "hide" rule takes effect, so the derived method derived: H (float X) hides the base class method base: H (float X ", therefore, 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, you must declare the methods that may be implemented in the base class using the virtual keyword. Unless in special cases, such as when you need to check the pointer type.

 

# Include
<Iostream>

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

}

Related Article

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.