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 is polymorphism in C++?

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

Share

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

This article introduces what is polymorphism in C++. The content is very detailed. Interested friends can use it for reference. I hope it can be helpful to you.

What is polymorphism?

As the name implies, it is the multiple forms of the same thing in different scenes.

The following will be described in detail.

Static polymorphism

The function overloading we talked about before is a simple static polymorphism.

Int Add (int left, int right) {return left + right;} double Add (double left, int right) {return left + right;} int main () {Add (10,20); / / Add (10.0,20.0); / / this is a problem code Add (10.00.20); / / normal code return 0;}

As you can see, static polymorphism is completed by the compiler during compilation, and the compiler will choose to call the appropriate function according to the argument type. If there is an appropriate function to call, it will issue a warning or an error. It is relatively simple and does not make much introduction.

Dynamic polymorphism

What is dynamic polymorphism? Dynamic polymorphism: this is obviously a set of antonyms with static polymorphism, which determines which class's virtual function to call according to the object pointed to by the reference (pointer) of the base class when the program is running.

I went to school in Lintong, Xi'an, so I lifted a chestnut with the bus here:

Class TakeBus {public: void TakeBusToSubway () {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