


C++11 new features - polymorphism and virtual functions, override specifier
Polymorphism and virtual functions
Polymorphism is one of the important features of C, and it is also a feature that is difficult for beginners to understand. Let’s look at a simple example first:
The code is very simple. According to the author’s own words, describe the key points from the definition to the function of polymorphism in the above code. It should be It won’t be simpler anymore, starting from the main function:
-
Pointer to the base class
- Object pointing to the derived class
- The derived class overrides the virtual function defined in the base class
These three points are necessary conditions, and one of them is indispensable.
There are other interesting aspects of the above code. Although it does not affect the results, it is still good to understand them. When redefining (overriding) the virtual function in the base class in the derived class:
-
virtual keyword is not required
- It is not necessary to declare the function as public
Override failure
Override of virtual function It is required that the signatures of the two functions must be consistent. For example, the following code:
Since the signature of sub2::f1 is inconsistent with the signature of base::f1, polymorphism cannot be achieved.
Maybe you will think that this error is too obvious and will never happen. That's because the member function in this example is too simple. In actual development, with the expansion of development scale, the inheritance relationship of classes will become deeper and deeper, and the parameters of member functions will also become more and more. We often encounter the signatures and overrides of member functions defined in derived classes. The signature of the object is inconsistent, resulting in overwriting failure.
And the terrible thing is that this kind of error will not produce compilation errors and is not easy to find.
override specifier
In order to solve this problem, a method was introduced in C 11: use the override specifier when declaring and defining override functions in derived classes:
Due to the clear purpose of the function, when the compiler cannot find a virtual function with the same signature in the base class, a compilation error will occur.
The author feels
It should become a habit of C programmers to make better use of the functions of the language itself and not give up any opportunity to automatically detect errors. It can also be regarded as the embodiment of craftsman spirit.
Related articles:
Talk in detail about the c 11 final and override specifiers
The differences and connections between auto and decltype in the new features of C 11
Related videos:
The above is the detailed content of C++11 new features - polymorphism and virtual functions, override specifier. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).
