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


[Html]
# Include <iostream>
Using namespace std;
 
Class Base {
Public:
Void f (int ){
Cout <"Base: f (int a)" <endl;
}
Virtual void g (int ){
Cout <"virtual Base: g (int a)" <endl;
}
};
 
Class Derived: public Base
{
Public:
Void h (int ){
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 );
}

# Include <iostream>
Using namespace std;

Class Base {
Public:
Void f (int ){
Cout <"Base: f (int a)" <endl;
}
Virtual void g (int ){
Cout <"virtual Base: g (int a)" <endl;
}
};

Class Derived: public Base
{
Public:
Void h (int ){
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.


[Html]
# Include <iostream>
Using namespace std;
 
Class Base {
Public:
Void f (int ){
Cout <"Base: f (int a)" <endl;
}
Virtual void g (int ){
Cout <"virtual Base: g (int a)" <endl;
}
};
 
Class Derived: public Base
{
Public:
Void h (int ){
Cout <"Derivd: h (int a)" <endl;
}
Virtual void g (int ){
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 );
}

# Include <iostream>
Using namespace std;

Class Base {
Public:
Void f (int ){
Cout <"Base: f (int a)" <endl;
}
Virtual void g (int ){
Cout <"virtual Base: g (int a)" <endl;
}
};

Class Derived: public Base
{
Public:
Void h (int ){
Cout <"Derivd: h (int a)" <endl;
}
Virtual void g (int ){
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?


[Html]
# 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 );
}

# 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:


[Html]
# Include <iostream>
Using namespace std;
 
Class Base
{
Public:
Virtual void f (int ){
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 (1, 2.5 );
 
Base B;
B. f (5 );
B. f (3.5 );
 
Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; f (3.5 );
}

# Include <iostream>
Using namespace std;

Class Base
{
Public:
Virtual void f (int ){
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 (1, 2.5 );

Base B;
B. f (5 );
B. f (3.5 );

Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; 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


[Html]
Derived d;
D. f (3 );
D. f (2.5 );
 
Derived * pd = new Derived ();
Pd-> f (3 );
Pd-> f (1, 2.5 );

Derived d;
D. f (3 );
D. f (2.5 );

Derived * pd = new Derived ();
Pd-> f (3 );
Pd-> f (2.5); as long as the f () function is executed through the Derived object or the Derived pointer, only void Derived: f (double d) is executed.


[Html]
Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; f (3.5 );

Base * pBase = new Derived ();
PBase-> f (5 );
PBase-> f (3.5); when calling pBase-> f (5), find the function to be executed in the pBase class first, because the Base class has two functions: virtual void f (int a) and void f (double), because the real parameter is 5, which is of the int type, therefore, you must call the virtual void f (int a) function. Because f (int a) is a virtual function, you can judge the specific object pointed to by pBase. The specific object is a subclass of Derived, go to the virtual function table of the Derived subclass and find the void f (int a) function. Because the Derived subclass inherits the virtual function vitural void f (int a) of the parent class Base, the output of virtual Base: f (int );

When calling pBase-> f (3.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 3.5, is a double class, so you need to call the void f (double d) function, because this function is a common member function, therefore, it is output directly. Void Base: f (double d );

 

 
 

 

Another example:


[Html]
# Include <iostream>
Using namespace std;
 
Class Base
{
Public:
Virtual void f (int ){
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 ){
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 (1, 2.5 );
 
Base B;
B. f (5 );
B. f (3.5 );
 
Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; f (3.5 );
}

# Include <iostream>
Using namespace std;

Class Base
{
Public:
Virtual void f (int ){
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 ){
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 (1, 2.5 );

Base B;
B. f (5 );
B. f (3.5 );

Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; 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.


[Html]
Derived d;
D. f (3 );
D. f (2.5 );
 
Derived * pd = new Derived ();
Pd-> f (3 );
Pd-> f (1, 2.5 );

Derived d;
D. f (3 );
D. f (2.5 );

Derived * pd = new Derived ();
Pd-> f (3 );
Pd-> f (2.5); similarly, when all sub-class objects or sub-class pointers are used to call the f () function, only virtual void f (int a) is executed, and the output virtual Derived :: f (int)


[Html] view plaincopyprint? Base * pBase = new Derived ();
PBase-> f (5 );
PBase-& gt; f (3.5 );

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) and void f (double d) are overload functions, because the real parameter is 5, int type, so we need to call virtual void f (int a) virtual function, because this function is a virtual function, you must determine the specific object to which pBase points, because pBase points to a subclass object, therefore, you need to find the virtual void f (int a) function in the virtual function table of the subclass, and then execute the function. Therefore, the virtual Derived: f (int a) is output ).

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.