C ++ member functions are overloaded, inherited, overwritten, and hidden.

Source: Internet
Author: User
Tags types of functions

I. overload of C ++ member functions

There are four types of member functions in C ++: common member functions, virtual functions, and const member functions.

(1) void func (int );

(2) virtual void func (int );

(3) void func (int A) const;

If you declare these four functions in a class, which is repeated? What are heavy loads?

(1) (2) indicates repeated definitions, so compilation fails, while (3) and (1) (2) indicate different types of functions and are overloaded.

Member functions are overloaded with the following features:

(1) with the same scope (that is, in the same class definition );

(2) functions with the same name

(3) parameter types, with different order or number (including const parameters and non-const functions)

(4) virtual keywords are optional.

From the overload feature of the member function, we can know that (1) (2) is a repeated definition. So why is (3) different from (1) (2?

Because the function in the class will automatically add a class pointer this, so

Void func (int A) === void func (base * This, int)

Virtual func (int A) === virtual func (base * This, int)

Void func (int A) const = void func (const base * This, int A) const

Therefore, (3) can be overloaded with (1) (2) because the parameter has a const.

Ii. Inheritance of C ++ member functions

#include <iostream>using namespace std;class Base {public:void f(int a){cout << "Base::f(int a)" << endl;}virtual void g(int a) {cout << "virtual Base::g(int a)" << endl;}};class Derived : public Base{public:void h(int a) {cout << "Derivd::h(int a)" << endl;}};int main(){Base b;b.f(3);b.g(4);Derived d;d.f(3);d.g(4);d.h(3);}

Object Model of Base B:

Object Model of derived D:

Then, the subclass derived D inherits the virtual void g (int A); void F (int );

The running result is:

Iii. Coverage of C ++ member functions

Overwrite refers to the member function of the base class re-implemented (or rewritten) by the derived class. Its features are as follows:

(1) different scopes (not in the derived class and the base class );

(2) Same Function Name

(3) The parameter list is identical;

(4) basic functions must be virtual functions.

We learned from (4) That overwrite is only for virtual functions.

#include <iostream>using namespace std;class Base {public:void f(int a){cout << "Base::f(int a)" << endl;}virtual void g(int a) {cout << "virtual Base::g(int a)" << endl;}};class Derived : public Base{public:void h(int a) {cout << "Derivd::h(int a)" << endl;}virtual void g(int a) {cout << "virtual Derived::g(int a)" << endl;}};int main(){Base b;b.f(3);b.g(4);Derived d;d.f(3);d.g(4);d.h(3);}

The derived D object model is as follows:

The virtual void g (int A) function of the base class is redefined in derived );

Iv. Hide C ++ member functions

Hiding means that the member functions of a derived class mask the base class member functions with the same name. The specific rules are as follows:

(1) The function of the derived class has the same name as the function of the base class, but the parameter list is different. In this case, functions of the base class will be hidden in the derived class regardless of whether there is a virtual keyword. (Do not mix with heavy load)

(2) The function of the derived class has the same name as the function of the base class, and the parameter list is the same, but the base class function does not have the virtual keyword. In this case, the function of the base class will be chanted in the derived class. (Do not mix with overwrite)

Determine which of the following functions are covered and which functions are hidden?

#include <iostream>using namespace std;class Base {public:virtual void f(float x) {cout << "virtual Base::f(float) " << x << endl;}void g(float x) {cout << "Base::g(float) " << x << endl;}void h(float x) {cout << "Base::h(float) " << x << endl;}};class Derived : public Base{public:virtual void f(float x) {cout << "virtual Derived::f(float) " << x << endl;}void g(int x) {cout << "Derived::g(int) " << x << endl;}void h(float x) {cout << "Derived::h(float) " << x << endl;}};int main(void){Derived d;Base *pb = &d;Derived *pd = &d;pb->f(3.14f);pd->f(3.14f);pb->g(3.14f);pd->g(3.14f);pb->h(3.14f);pd->h(3.14f);}

In the subclass derived, vitual void F (float X) is overwritten, while void g (int x) and void H (float X) are hidden.

Running result:

Let's look at another example:

#include <iostream>using namespace std;class Base{public:virtual void f(int a) {cout << "virtual Base::f(int a)" << endl;}void f(double d) {cout << "Base::f(double d)" << endl; }};class Derived : public Base{public:void f(double d) {cout << "Derivd::f(double d)" << endl;}};int main(){Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);Base b;b.f(5);b.f(3.5);Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);}

The void F (double D) in the parent class hides the virtual void F (int A) and void F (double D) Functions of the subclass.

So in the main function

Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);

If you use the derived object or the derived pointer to execute the F () function, only void derived: F (double D) is executed.

Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);

When calling pbase-> F (5), first find the function to be executed in the pbase class, because the base class has two functions: Virtual void F (int) and void F (double) overload, because the real parameter is 5, int type, so you need to call the virtual void F (int A) function, because the F (int) is a virtual function, so you can judge the specific object pointed to by pbase. The specific object is the derived subclass, and find the void F (int A) function in the virtual function table of the derived subclass. Because the derived subclass inherits the virtual function vitural of the parent class base.
Void F (int A), so the output is virtual base: F (int );

When calling pbase-> F (3.5), first find the function to be executed in the pbase class, becauseThe base class has two functions: Virtual void F (int A) and void F (double). Because the real parameter is 3.5 and is a double class, void F (double D) must be called) function, because this function is a common member function, it is directly output. Void base: F (double D );



Another example:

#include <iostream>using namespace std;class Base{public:virtual void f(int a) {cout << "virtual Base::f(int a)" << endl;}void f(double d) {cout << "Base::f(double d)" << endl; }};class Derived : public Base{public:void f(int a) {cout << "virtual Derived::f(int a)" << endl;}};int main(){Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);Base b;b.f(5);b.f(3.5);Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);}

The void F (int A) in the subclass derived not only overrides the virtual void F (int A) function of the base class, but also hides the virtual void F (int A) of the base class ), void F (double D) function.

Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);

Similarly, when the F () function is called using subclass objects or subclass pointers, only virtual void F (int A) is executed, and virtual derived: F (int A) is output)

Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);

Pbase-> F (5), first go to the base class to find the corresponding function. Likewise, the two functions in the base class are virtual void F (int A) and void F (double D) is a overload function. Because the real parameter is 5 and the int type, we need to call the virtual void F (int A) virtual function because the function is a virtual function, therefore, determine the specific object to which pbase points. Because pbase points to a subclass object, you need to find the virtual void F (int A) function in the virtual function table of the subclass, after finding and executing this function, the output is virtual
Derived: F (int ).

Pbase-> F (3.5), first go to the base class to find the corresponding function. Likewise, the two functions in the base class are virtual void F (int A) and void F (double D) is an overloaded function. Because the real parameter is 3.5 and is of the double type, we need to call void F (double D). Because this function is a common member function, the output is void base :: F (double D );

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.