Interpretation of c ++ virtual function examples

Source: Internet
Author: User
Tags function examples

A special function in C ++ programming language is called a virtual function. How should we correctly understand C ++ virtual functions? Here we will give a detailed introduction to the basic concepts in this aspect, hoping to help you and increase your awareness of the C ++ language.

I think I have mastered the role of virtual functions. Wow, I didn't say I was an old bird, or I understand it, but it cannot be used in practice. Don't worry. Let me talk about it for the last time.
What is a virtual function? The C ++ virtual function is a function described by the keyword "Explain aul.

For example:

In this example, WindowB and WindowC do not exist at the same time, which is often used in practical applications to save memory.

 
 
  1. Class sort wa
  2. {
  3. Public:
  4. Virtual void Draw (){};
  5. };
  6. Class Using WB: public using wa
  7. {
  8. Public:
  9. Using WB (){};
  10. Void Draw ();
  11. };
  12. Class sort WC: public sort wa
  13. {
  14. Public:
  15. WindowC (){};
  16. Void Draw ();
  17. };
  18. Void required WB: Draw ()
  19. {
  20. Draw a circle;
  21. }
  22. Void merge WC: Draw ()
  23. {
  24. Draw a square;
  25. }

When WindowA is defined, you are not sure what the derived classes of WindowB and WindowC will Draw in the Draw function. In this case, the Draw is defined as a C ++ virtual function, which is implemented in the derived class.

Speaking of this, the reader will ask: I will write Draw in WindowB and WindowC, and I will write less of a kind of mongowa.

Yes, it's just a matter of writing. As you said, you will use it like this:

 
 
  1. Required WB * B = new required WB;
  2. B-> Draw (); // Draw a circle
  3. Delete B;
  4. WindowC * c = new WindowC;
  5. C-> Draw (); // Draw a square
  6. Delete c;

I use pointers here. In the code above, B and c are two independent object pointers. But what should you do if there are dozens or hundreds of derived classes instead of one or two? How many hundred variables are defined in the header file?

No, it's time to use the C ++ virtual function:

 
 
  1. WindowA * a = new WindowsB;
  2. A-> Draw (); // Draw a circle. The implementation of the Draw function in WindowB is called here.
  3. Ifa)
  4. Delete a; // The new value must be deleted.
  5. WindowA * a = new WindowC;
  6. A-> Draw (); // Draw a square. The implementation of the Draw function in WindowC is called here.
  7. Ifa)
  8. Delete;

In the above Code, a implements a transit variable. As long as it is derived from megawa, I can assign a value to a, and B and c are both temporary variables. The number of derived classes of megawa is okay. I just need to define a pointer variable of megawa in the header file.

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.