C + + Object-oriented fundamentals

Source: Internet
Author: User

Polymorphism is for interface reuse, encapsulation and inheritance for code reuse
Subclasses redefining the parent virtual function is called inheritance, not overloading! First, the basic concept

There are three main scenarios for functions that often appear in C + + with the same name but different argument lists or return values:

1. Function rewriting (override)

function overloading mainly implements the polymorphism between the parent class and the subclass, which defines a virtual function that is the same as the name and argument in the parent class and the return value.

1) The overridden function cannot be a static function, it must be a virtual function, that is, the function is declared as a virtual function in the original base class;

2) The overriding function and the base class function are divided into the declarations and definitions of two classes, which results in different scopes of the two;

3) The name of the overriding function, the argument list, and the return value (that is, the function prototype) are the same as the function of the base class;

4) The access modifier for an overriding function can be different, although the virtual function is private, and the function overridden in a derived class can be public or protect

2. Function overloading (overload)

function overloading cannot be distinguished by the return value of functions in the same scope where the function name is the same but the parameter type or parameter order is different;

3. redefinition (redefine)

Refers to subclasses that redefine non-virtual functions in the parent class (the argument list can be different) so that the corresponding functions in the parent class are hidden.

Second, the difference between overriding (override) and overloading (overload)

1, the function rewrite is the subclass and the parent class inheritance relation, is the vertical relation, the method's overloading is the relation between the method in the same class, is the horizontal relation;

2, The rewrite needs the subclass and the parent class two functions The function prototype is identical, the overload request two function parameter list is different;

3, in the rewrite relationship, call the specific call which function is based on (the object corresponds to the actual type of storage space), which involves the problem of dynamic binding and static binding, that is, the call mechanism of virtual function, and function overloading is mainly by the difference of formal parameter list to distinguish the specific call which function.

1. member function overloading characteristics: A, the same range (in the same class) B, the function name is the same C, the parameter is different d, the virtual keyword is optional 2. overriding (overwriting) refers to a derived class function overriding a base class function, characterized by: A, a different range, in the base class and the derived class B, the same , the same parameter d, the base class function must have the Virtual keyword 3, redefinition (hidden) refers to a function of a derived class that masks a base class function with the same name, the rule is: A, if the function of the derived class and the function of the base class have the same name, butdifferent parameters, the function of the base class is hidden, whether or not virtual, if the function of the derived class and the function of the base class have the same name, and the parameters are the same, but the base class does not have the virtual keyword, the function of the base class is hidden


C + + Object-oriented fundamentals

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.