About overloading, overwriting, hiding, and virtual keywords for C + + class member functions

Source: Internet
Author: User

1. Overload, overlay and hide

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

A Same range (in same class)

B The function has the same name

C parameter types are different (implicit type conversions are not possible)

D Virtual keyword is optional

2). Overlay (also called "Inheritance"): Refers to a derived class function that overrides a base class function, characterized by:

A Different scopes (in base classes and derived classes, respectively)

B Same function name

C Parameters are the same

D The base class function must have the virtual keyword

3). Shadowing: Refers to functions of derived classes that mask a base class function with the same name as the following rules:

A If the function of the derived class has the same name as the function of the base class, but the arguments are different, the function of the base class will be hidden, regardless of the virtual keyword, to be confused with the overload.

B If the function of the derived class has the same name as the function of the base class, and the arguments are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden (note that you are confused with overrides)

2. Look at 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::cout << "base::g (float)" << x << Std::endl;}
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::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
}

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.