Chapter 12 Object-Oriented Programming (12)

Source: Internet
Author: User

15.5.3 scope and member functions
When a member function with the same name is used in the base class and derived class, its behavior is the same as that of the data member. In the scope of the derived class, the member of the derived class will be blocked. The base class members are blocked even if the function prototype is different.

Class Base
{
Public:
Int Get (){
Return 0;
}
};
 
Class Child: public Base
{
Public:
Int Get (int I ){
Return I;
}
};

Child c = Child ();
Cout <c. Get (10) <endl; // Get (int) hide Base: Get ()
Recall that the function declared in the local scope does not reload the function defined in the global scope. Similarly, the function defined in the derived class does not reload the Members defined in the base class. When calling a function through a derived class object, the real parameter must match the version defined in the derived class. The base class function is considered only when the derived class does not define the function.
Overload Functions
Like any other function, member functions (whether virtual or non-virtual) can also be reloaded. A derived class can redefine zero or more inherited versions.
If an overloaded member is redefined in a derived class, only the Members that are redefined in the derived class can be accessed through the derived type.

Class Base
{
Public:
Int Get (){
Return 0;
}
Double Get (double I ){
Return I;
}
};
 
Class Child: public Base
{
Public:
Using Base: Get;
Int Get (int I ){
Return I;
}
};

Child c = Child ();
Cout <c. Get () <endl; // 0
Cout <c. Get (10.2) <endl; // 10.2
If a derived class wants to use all the overloaded versions of its own type, the derived class must either redefine all the overloaded versions or one of them.
A derived class does not need to redefine each inherited base class version. It can provide a using Declaration for overload members. A using Declaration can only specify one name, but cannot specify a form parameter table.
15.5.4 virtual functions and scopes
To obtain dynamic binding, the virtual function must be called through the reference or pointer of the base class.

Class Base
{
Public:
Virtual int Get () {// must be virtual func.
Return 0;
}
};
 
Class Child: public Base
{
Public:
Int Get (){
Return 1;
}
};

Base c1 = Child ();
Cout <c1.Get () <endl; // 0
Base * c2 = new Child ();
Cout <c2-> Get () <endl; // 1
Use a base class to call blocked virtual functions
When a function is called through a reference or pointer of the base class type, the compiler searches for the function in the base class and ignores the derived class.
To determine whether a function is called, follow these four steps:
(1) first, determine the static type of the object, reference, or pointer for the function call.
(2) Search for functions in the class. If the function cannot be found, search for the function in the base class directly. In this way, search for the function based on the inheritance chain of the class, wait until you find the function or find the last class. If the name cannot be found in the class or related base class, the call is incorrect.
(3) once the name is found, perform a regular type check to check whether the function call is valid if the defined value is specified.
(4) assuming that the function call is valid, the compiler generates the code. If a function is a virtual function and called by reference or pointer, the compiler generates code to determine which version of the function is run based on the object's dynamic type. Otherwise, the compiler generates code to directly call the function.

From xufei96's column
 

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.