Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the polymorphisms and virtual functions of C++?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the polymorphism and virtual function of C++". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the polymorphism and virtual function of C++" can help you solve the problem.

Polymorphisms

Polymorphism is one of the key technologies of object-oriented programming. If the programming language does not support polymorphism, it cannot be called an object-oriented language. Using polymorphism technology, functions with the same function name can be called to achieve completely different functions.

There are two polymorphisms in C++:

Polymorphisms at compile time

Through the overloading of functions and operators

Run-time polymorphism

Run-time polymorphism means that before the program is executed, it is impossible to determine which function to call according to the function name and parameters, but must be dynamically determined according to the specific circumstances of the execution of the program; it is achieved through the class inheritance relationship public and virtual functions, and the purpose is to establish a general program; generality is one of the main goals of the program.

Run-time polymorphism can only be achieved through references or pointer calls

Virtual function

A virtual function is a member function of a class and is defined in the following format:

Virtual returns the type function name (parameter table)

The keyword virtual indicates that the member function is a virtual function, and virtual is only used in the class definition. If the virtual function is defined outside the class, virtual cannot be added.

Let's look at the following code

Class Animal {private: string name;public: Animal (const string& na): name (na) {} public: virtual void eat () {} virtual void walk () {} virtual void tail () {} virtual void PrintInfo () {} string& get_name () {return name } const string& get_name () const {return name;}}; class Dog: public Animal {private: string owner;public: Dog (const string& ow, const string na): Animal (na), owner (ow) {} virtual void eat () {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report