Inheritance methods for C + + derived classes Summary: __c++

Source: Internet
Author: User
 the inheritance of derived classes is summarized as follows:
Inheritance Way Description
Public The public and protected members of the base class are inherited by the derived class and remain in their original state
Private The public and protected members of the base class are inherited by the derived class and become private members of the derived class
Protected

The public and protected members of the base class are inherited by the derived class and become protected members of the derived class

Note: Private members of the base class cannot be accessed by derived classes, regardless of the inheritance method. As you can see from the table above, methods and properties declared as public can be accessed arbitrarily, methods and properties declared as protected are accessible only to the class itself and to its subclasses, while methods and properties declared private can only be accessed by objects of the current class.

1. The friend function must be declared in the class and defined outside the class, and the declaration should precede the function return type with the keyword friend. A friend function is not a member function of a class, but it can access private and protected type data members in a class.

2. The number and type of arguments must be exactly the same as the virtual function in the base class when redefining a virtual function, which is completely different from the function overload.

3. #include < filename > and #include "filename" file contains the two formats in which the first format is used to contain header files that are provided by the system and placed in a specified subdirectory While the second format is used to include header files or other source files that are defined by the user in the current directory or other directory.

4. Arrays can also be the arguments and parameters of a function, and if an array element serves as an argument to a function, its usage is the same as that of a variable. The address of an array is passed when the array name is used as the arguments and parameters of the function. When passing by value, the value transfer is one-way, that is, only the parameters are passed from the argument to the formal parameter, and the argument cannot be returned from the parameter. The initial value of the formal parameter is the same as that of the argument, and when the value of the formal parameter changes, the argument does not change and the final values are different. When the array name is passed as a function parameter, the real parameter group changes when the parameter group changes, because the actual argument and the formal parameter are the same array.

Note: The real parameter group and the form parameter group type should be consistent, if inconsistent, the result will be wrong, the parameter group can also not specify the size, the array name in the definition of an array followed by an empty square brackets, in order to handle the needs of the array elements in the called function, you can set a separate parameter, passing the number of array elements. such as: int sum (int array[],int n);

5. Overload, overlay and hide the difference.

The overload of a function means that C + + allows multiple functions with the same name to exist, but the parameters of each function with the same name must differ: the number of formal parameters is different, or the number of formal arguments is the same, but the parameter types vary.

Overwrite (Override) refers to a redefined function in a derived class whose function name, parameter column, and return value type must be strictly consistent with the corresponding function in the half class, and the overridden function and the overridden function are only different from the function body (part of the curly braces). An overlay version in a subclass is automatically invoked when the derived class object invokes the same name function in the subclass, rather than the overridden version of the function in the parent class, which is called overwrite.

Let's look at the difference between overload and overlay from the point of view of member functions.

member functions are overloaded with the following characteristics: 1 the same range (in the same class); 2) The function name is the same; 3) the parameters are different; 4 The virtual keyword is optional.

The features of the overlay are: 1 different ranges (in the derived class and the base class); 2) The function name is the same; 3) The parameter is the same; the base class function must have the virtual keyword.

For example, in the following program: #include   < iostream.h >
class  base
{
Public:
void  f (int  x) {cout  <<   "base::f (int)  "   <<  x  <<  endl;}
void  f (float  x) {cout  <<   base::f (float)   "  <<  x  <& Lt  endl;
Virtual   void  g (void) {cout  <<   base::g (void)   <<  endl;}
};

Class  derived:  public  base
{
Public:
Virtual   void  g (void) {COUT&NB Sp <<   "derived::g (void)"   <<  endl;}
};

void  main (void)
{
Derived D;
base  * pb  =   & D;
PB-> F; /  Run Result: base::f (int)
PB-> F (3.14f); //  Run Result: base::f (float) 3.14
PB-> g (); //  Run Result: derived::g (void)
}

function base::f (int) and base::f (float) are overloaded with each other, while base::g (void) is overwritten by derived::g (void).

Shadowing means that a function of a derived class masks a base class function with the same name as the following: 1 if the function of the derived class has the same name as the function of the base class, the arguments are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that you are not confused with overloading). 2 If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function has no virtual keyword. At this point, the functions of the base class are hidden (note that you are confused with overrides).

For example, in the following program:

#include < iostream.h >

Class

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.