Differences between overwrite, overwrite, overload, hide, and Polymorphism

Source: Internet
Author: User

A Brief Analysis of Several confusing terms in OOP programming. Overwrite, overload, hide, polymorphism first, overwrite is also weighed, English override features: 1. it refers to the definition of functions with the same name in different scopes (derived classes and base classes). 2. function names and parameters are identical. A base class corresponds to a virtual method, that is, it is declared as a virtual function. The base class pointer and reference implement dynamic binding according to the specified object type when calling the corresponding method. Second, overload features: 1. scope, in the same class 2. the function name is the same, but the parameter type and number are not exactly the same: the same method produces different actions (same method name, implementation is different ). Hiding (masking) features: different scopes, the base class and the derived class are divided into two situations: 1. The base class and the derived class function name are the same, but the parameter list is different, and there are different virtual, the base class function is hidden in the derived class. The derived class can only call new methods, but cannot call hidden base class methods (different from overload and with different scopes) 2. The base class and the derived class have the same name and same parameter, but the base class function does not have virtual. Similarly, the derived class also hides the same name and same parameter functions of the base class (different from overwrite and non-virtual ).

include <iostream>  using namespace std;    class base  {      public:      virtual void f(float x) {cout<<"base:f"<<endl; }      void g(float x) {cout<<"base:g"<<endl; }      void h(float x) {cout<<"base:h"<<endl;}  };    class derived : public base  {  public:      virtual void f(float x) {cout<<"derived:f"<<endl; }      void g(int x) {cout<<"derived:g"<<endl; }      void h(float x) {cout<<"derived:h"<<endl;}  };    int main()  {   derived d;  base *pb = &d;  derived *pd = &d;    cout<<"test override:\n";  pb->f(3.14);  pd->f(3.14);    cout<<"test hide 1:\n";  pb->g(3.14);  pd->g(3.14);    cout<<"test hide 2:\n";  pb->h(3.14);  pd->h(3.14);    return 0;  }  

 

Analysis: dynamic binding Based on overwriting. Static binding based on the base class pointer or the type of the object to which the reference points, calling the corresponding method to hide depends on the called pointer or application type, when using a non-base class pointer or reference to an object type, it is hidden to produce confusion and should be avoided as much as possible. Polymorphism is a manifestation of OOP programming. The same message acts on the same method (except that the function name is the same) and produces different results. The representation method is overload. The implementation interface and the inheritance overload indicate that an object calls the same function name (such as add), according to the passing parameter (Message) (For example, int or float type), different methods are called to produce different results. Inheritance is manifested in different levels (derived classes and base classes). The same result is generated for calls to the same function (same name and parameter.

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.