4.2 C + + virtual member function

Source: Internet
Author: User

Reference: http://www.weixueyuan.net/view/6371.html

Summarize:

The virtual keyword is used only for function declarations, and if the function is defined outside the class, you do not need to add the virtual keyword.

In C + +, only member functions in a class can be declared as virtual functions, while for top-level functions they cannot be declared as virtual functions.

A virtual function is declared to form a polymorphic state.

In fact, we simply declare the display function in the base class to be a virtual function through the virtual keyword, and then, even if the display function is declared in a derived class without the virtual keyword, it will automatically become a virtual function in all derived classes.

As with normal member functions, virtual member functions can also be inherited.

The declaration method of a virtual function has been mentioned in the previous section by adding the virtual keyword before the function return type.The virtual keyword is used only for function declarations, and if the function is defined outside the class, it is not necessary to add the virtual keyword, in C + + only the member functions in the class can be declared as virtual functions, and for the top-level functions cannot be declared as virtual functions,The reason is simple, the declaration of virtual function is to form a polymorphic, and the first condition that constitutes polymorphism is the need for inheritance, the top-level function is obviously not an inheritance relationship, and therefore can not be declared as virtual function.

Example 1:
#include <iostream>using namespacestd;class Base{ Public:    Virtual voiddisplay ();};classDerived Public Base{ Public:    Virtual voiddisplay ();};void Base::d isplay () {cout<<"I ' m base class!"<<Endl;}voidderived::d isplay () {cout<<"I ' m derived class!"<<Endl;}intMain () {Base* p =New Base; P-display (); Deletep; P=Newderived; P-display (); Deletep; return 0;} In this example, we declare the display function in two classes inside the class, defined outside the class, and of course this modification does not alter the result of the program's operation. In this case, it is important to note that when declared in a class, the display function is declared for virtual functions by adding the virtual keyword before the return type, and when it is defined outside the class, the display function does not add the virtual keyword before the return type. Also in Example 1, we add the virtual keyword to the display function declaration in both the base class and the derived class to represent it as a virtual function, which is not required, and we only need to declare the display function in the base class through the virtual keyword as a virtual function. The virtual keyword is not added to the display function declaration in a derived class after that, but it will automatically become a virtual function in all derived classes. Example 2: #include<iostream>using namespacestd;class Base{ Public:    Virtual voiddisplay ();};classDerived Public Base{ Public:    voiddisplay ();};void Base::d isplay () {cout<<"I ' m base class!"<<Endl;}voidderived::d isplay () {cout<<"I ' m derived class!"<<Endl;}intMain () {Base* p =New Base; P-display (); Deletep; P=Newderived; P-display (); Deletep; return 0;}

In this example, we remove the virtual keyword that was used to declare the display member function of the derived class in Example 1, compile and run the program, and the result shows that the program's running result remains the same.

As with normal member functions, virtual member functions can also be inherited.

Example 3:
#include <iostream>using namespacestd;class Base{ Public:    Virtual voidHello () {cout<<"hello!"<<Endl;}};classDerived Public Base{    //......};intMain () {Base* p =New Base; P-hello (); Deletep; P=Newderived; P-hello (); Deletep;    Derived D;    D.hello (); return 0;}

In this case, there is no new member variable or member function in the derived class derived, which is a derived class of the base class. Let's look directly at the main function. In the main function, you still define a pointer to a base class type, and then point to the base class and the object of the derived class successively. In this case, thea virtual function with the same function name as the base class Hello function does not exist for a derived class(The inherited Hello function does not constitute a masking relationship with the Hello function in the base class), and thereforedoes not constitute polymorphism, all the while is called the base class's Hello function。 It then defines a derived class object, D, which calls the Hello function and, of course, a hello function inherited from the base class base. It is obvious from this program that the virtual function is inherited, which is indistinguishable with the normal function.

4.2 C + + virtual member 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.