Learning notes --- C ++ virtual functions, pure virtual functions

Source: Internet
Author: User

1. Virtual Functions

Assume that people is the parent class of man. Both the people class and man class define the real function walk ()

People * p = new man ();

P-> walk ();

Here P executes the walk () function of the people class. This is different from the java language. java executes man's walk () function here. So how does C ++ rewrite the subclass method and dynamically locate the subclass method?

The virtual keyword must be used to define the walk () of the parent class and subclass ()

Virtual void walk ();

Execute p-> walk (); that is, the walk () of the subclass of the execution ().

2. Pure virtual functions

Virtual void walk () = 0; a pure virtual function is defined here, which can be implemented in the subclass instead of in the parent class.

If the walk function is not implemented in the man class

People * p = new man ();

An error is reported. The man class is an abstract class.

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.