"C + +" Three concepts to distinguish--overloading, hiding (redefine, overwrite (rewrite)

Source: Internet
Author: User

Overload

• Concept: In the same scope, the function name is the same, the parameter list is different (the number of arguments is different, or the parameter type is different, or the number of parameters and parameter types are different), the return value type can be the same or different; This is called C + + overload!

Note: The C language does not have a mechanism for function overloading;

• Example code:

1#include <iostream>2 using namespacestd;3 4 intADD (intAintb)5 {6     returnA +b;7 }8 9 floatADD (floatAfloatb)Ten { One     returnA +b; A } -  - intMain () the { -Cout<<add (4,5) <<endl;//call int Add (int a,int b) -Cout<<add (2.5f,3.7f) <<endl;//Call float Add (float a,float b) -     return 0; +}

At this point, two functions Add (); In the same scope, the function name is the same as add, the parameter types are different; it forms the function overloading in C + +;

C++ function Overloading: functions with the same function name are called, and the corresponding functions are selected according to the type and argument order of the arguments and the number of arguments.

C++ function Overloading is a static polymorphism (also known as Static-linked, static-bound, static-resolution)

Overwrite (also called rewrite)

• overriding (overriding) Prerequisites: The parent class function is a virtual function;

• overriding (overriding) concept: When you define a virtual function that is exactly the same as the parent class in a subclass, this function of the subclass is called the override (also called overwrite) of the virtual function of the parent class.

• What is the definition of a virtual function that is exactly the same as the parent class in a subclass:

There are two types of situations:

1. That is, the virtual function in the subclass and the virtual function in the parent class, the function name, the number of arguments, the parameter type, and the return value type are the same; In this case, the virtual function of the subclass overrides the virtual function in the parent class, which forms the rewrite;

2. Covariant-is the virtual function in the subclass and the virtual function in the parent class, the function name, the number of arguments, the parameter types are the same, but the return value type is different; the virtual function of the parent class returns a pointer or reference to the parent class, and the subclass virtual function returns a pointer or reference to the child class, which also overrides the virtual function in the parent class. also constitutes a rewrite;--we call this special situation covariant.

"Note": in the base class defines a function that is exactly the same as the virtual function of the derived class, then the function of the derived class is to override the virtual function of the base class, at which point the function of the derived class is a virtual function, if not displayed with the virtual decoration, the compiler will default to the virtual function;

1#include <iostream>2 using namespacestd;3 4 class Person5 {6  Public:7     Virtual voidbuytickets ()8     {9cout <<"Buy Tickets-unanimous"<<Endl;Ten     } One protected: A     string_name;//name - }; -  the classStudent: Public Person - { -  Public: -     voidbuytickets () +     { -cout <<"Buy tickets-half price"<<Endl; +     } A protected: at     int_num;//School Number - }; -  - voidFun (person*p) - { -P->buytickets (); in } -  to voidFun (Person &p) + { - p.buytickets (); the } *  $ voidTest ()Panax Notoginseng { - Person p; the Student S; + Fun (p); A Fun (s); the  +Fun (&p); -Fun (&s); $ } $  - intMain () - { the Test (); -System"Pause");Wuyi     return 0; the}

Output Result:

Example 2:

1#include <iostream>2 using namespacestd;3 class Person4 {5  Public:6     Virtualperson& Buytickets ()//base class virtual function7     {8cout <<"Adult Ticket-unanimous"<<Endl;9         return* This;Ten     } One  Public: A     string_name;//name - }; -  the classStudent: Public Person - { -  Public: -     Virtualstudent& Buytickets ()//derived class virtual functions +     { -cout <<"students buy tickets-Priceticket"<<Endl; +         return* This; A     } at  Public: -     int_num;//School Number - }; -  - voidFun (person*p) - { inP->buytickets (); - } to  + voidFun (Person &p) - { the p.buytickets (); * } $ voidTest ()Panax Notoginseng { - Person p; the Student S; + Fun (p); A Fun (s); the  +Fun (&p); -Fun (&s); $ } $ intMain () - { - Test (); theSystem"Pause"); -     return 0;Wuyi}

Output Result:

Resources

1. C + + three concepts to distinguish--overload, Hide (redefine), overwrite (rewrite)

C + + three concepts to distinguish-overload, hide (redefine, overwrite (override)

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.