C + + Initial knowledge virtual function

Source: Internet
Author: User

Do java,android development. Because of the project needs, learning a little C + +, I will talk about the two days of the virtual function of understanding it. If you are wrong, please correct me. Also hope to initiate.

The definition of virtual function is to unify the interface

1, virtual function is similar to Java interface. When inheriting the parent class, implement the virtual function inside the declaration and begin implementing the function method. Inheriting a parent class is called a derived class.

2, some of the parent class is not virtual function, but you want to use the inside function, you need to precede the function name:: Parent class name. In the case of a derived class name, it is a derived class function method, not a function inside the parent class. In C + +, are all functions preceded by the name of the class? This is a very flexible application.

3, there is a pure virtual function, virtual float area () = 0; Pure virtual function

pure virtual function is a function that is" initialized "to 0 when declaring a virtual function. The general form of declaring a pure virtual function is
    virtual function type function name (parameter table column) = 0;

A few points to note about pure virtual functions:

    1. The pure virtual function has no function body;
    2. The Last "= 0" does not mean that the return value of the function is 0, it only plays a formal role, tells the compilation system "This is pure virtual function";
    3. This is a declaration statement, and there should be a semicolon at the end.

Second, the use of virtual function is:

    1. declares member functions in the base class as virtual functions with virtual.
      This function can be redefined in a derived class to give it new functionality and be easily invoked. When you define a virtual function outside of a class, you do not have to add virtual.
    2. redefine this function in a derived class, requiring that the function name, function type, number of function arguments, and type are all the same as the virtual functions of the base class, and redefine the function body according to the needs of the derived class.
      C + + stipulates that when a member function is declared as a virtual function, the function with the same name in its derived class automatically becomes a virtual function. Therefore, when a derived class re-declares the virtual function, you can add virtual, or do not add, but it is customary to declare the function at each layer to add virtual, so that the program clearer. If the virtual function of the base class is not redefined in the derived class, the derived class simply inherits the virtual function of its immediate base class.
    3. defines a pointer variable to a base class object and points it to the object in the same class of family that needs to invoke the function. The
    4. calls this virtual function through the pointer variable, which is called the function with the same name as the object that the pointer variable points to. The
      uses a virtual function with a pointer variable to a base class object to make it easy to invoke a function of the same name that is not the same class in the same family, as long as the base class pointer points to it. If the pointer constantly points to objects of the same class that are not homogeneous, you can constantly call functions with the same name in those objects. This is like saying, keep telling the taxi driver where to go, and the driver will send you where you want to go.
A non-virtual function defined in a base class is sometimes redefined in a derived class (such as the area function in Example 12.1), and if the member function is called with a base-class pointer, the system invokes the member function of the base class part in the object, or if the member function is called with a derived class pointer. The system calls the member functions in the derived class object, which is not a polymorphic behavior (using different types of pointers) and does not use the function of a virtual function.

This passage is from http://c.biancheng.net/cpp/biancheng/view/244.html.

Three

The definition of the global variable and the declaration of the function are all in the. h file.

With the function definition implementation, global variables are used in the. cpp: H and. cpp names are all one by one corresponding.


C + + Initial knowledge virtual function

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.