Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the overloading, inheritance, overriding and hiding of C++ member functions

Shulou Source: shulou.com Published: 2022-06-02 09:25:25 10月07日 Update

This article mainly introduces "what is the overloading, inheritance, overriding and hiding of C++ member functions". In daily operation, I believe many people have doubts about what is the overloading, inheritance, overwriting and hiding of C++ member functions. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation, hoping to help you answer the doubts about "what is the overloading, inheritance, coverage and hiding of C++ member functions". Next, please follow the editor to study!

I. overloading of C++ member functions

There are four kinds of member functions in C++, which are ordinary member functions, virtual virtual functions and const member functions.

(1) void func (int a)

(2) virtual void func (int a)

(3) void func (int a) const

If you declare these four functions in a class, what is the duplicate definition? Which are overloads?

Where (1) (2) is a duplicate definition, so the compilation cannot pass, while (3) and (1) (2) are different types of functions and are overloaded.

The characteristics of member functions being overloaded are:

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

(2) the function name is the same

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

(4) virtual keyword is optional.

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

Because functions in the class automatically add their own class pointer this, so

Void func (int a) = void func (Base * this, int a) virtual func (int a) = virtual func (Base * this, int a) void func (int a) const = = void func (const Base * this, int a) const

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

II. Inheritance of C++ member functions

# include using namespace std; class Base {public: void f (int a) {cout

Tags: Functions members B.F. subclasses Category arguments objects parameters same Type output different two b.gJournal d.g.h Learning normal keywords keywords Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Huawei Xiaomi Docker