Resolution of overwrite and hiding and calling of base class and parent class pointers

Source: Internet
Author: User

I haven't written an article for a long time. I have been reading other people's blogs. I also wrote an article today. One is to reward the knowledge of netizens, and the other is to improve myself.

Disclaimer: Due to my limited knowledge, if any, direct correction to avoid misleading you!

Overload: The function name is the same, and the return type and parameter can be different. When the compiler links, the parameter type check will be added (C does not check the function parameter type, so C does not overload)

Overwrite: When a class is inherited, the base class function does not work. Note that the base class pointer or reference is used to access the derived class (an important feature of the virtual function, the embodiment of polymorphism, rtti Technology)

Hide: It appears in function overload. This can happen if a non-virtual function is used.

 

Check the code and run the following code:

Class firstbase
{
Public:
Virtual void fun1 () {cout <"base: fun1" <Endl ;}
Void fun2 () {cout <"base: fun2" <Endl ;}
// Void fun3 {cout <"base: fun3" <Endl ;}

};

Class drivedfrombase: Public firstbase
{
Public:
Virtual void fun1 () {cout <"drivedfrombase: fun1" <Endl ;}
Void fun2 () {cout <"drivedfrombase: fun2" <Endl ;}
Void fun2 (int x) {cout <"drivedfrombase: fun2" <x <Endl ;}
 
Void fun3 () {cout <"drivedfrombase: fun3" <Endl ;}

};

Class secondbase
{Public:
// Void fun1 () {cout <"secondbase: fun1" <Endl ;}
// Void fun2 {cout <"secondbase: fun2" <Endl ;}
Void fun3 () {cout <"secondbase: fun3" <Endl ;}

};

Class derivedfromsecondandfirstbase: Public drivedfrombase, public secondbase
{
Public:
Void g () {cout <"derivedfromsecondandfirstbase: fun1" <Endl ;}
Void H () {cout <"derivedfromsecondandfirstbase: fun2" <Endl ;}
// Void fun3 {cout <"derivedfromsecondandfirstbase: fun3" <Endl ;}

};

The inheritance relationship is:

Firstbase-> drivedfrombase->

Derivedfromsecondandfirstbase.

Secondbase->

Int main ()

{
// Ambiguity, non-Overload
Derivedfromsecondandfirstbase * P = new derivedfromsecondandfirstbase ();
// P-> fun3 (); // This sentence will return an error, which may be mistaken for function overloading. However, fun3 is of a different class, and therefore cannot be overloaded, resulting in ambiguity.
// Overwrite
Drivedfrombase * DB = new drivedfrombase ();
DB-> fun1 (); // use a virtual function to overwrite the fun1 of the base class.
// Hide
DB-> fun2 (2 );
Firstbase * fp = dB;
FP-> fun2 (); // hide a function that can be accessed through pointer transformation.

// Unable to call
// FP-> fun3 (); // If a function is not a virtual function, you cannot use the base class pointer to access a part that is not a base class, such as fun3.
Return 0;
}

Of course, this part is still very complicated and cannot be explained from the bottom layer for the time being.

 

Running result:

 

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.