


What is the difference between virtual function and pure virtual function?
The difference between virtual functions and pure virtual functions: 1. Pure virtual functions only have definitions and no implementation; virtual functions have both definitions and implementation codes. 2. A class containing pure virtual functions cannot define its objects, but a class containing virtual functions can.
Related recommendations: "C Video Tutorial"
Virtual function (impure virtual)
The main function of C's virtual function is "runtime polymorphism". The parent class provides the implementation of the virtual function and provides the default function implementation for the subclass.
Subclasses can override the virtual functions of the parent class to achieve specialization of the subclass.
The following is a virtual function in a parent class:
class A { public: virtual void ss() { cout<<"我是基类的虚函数"<<endl; } };
Pure virtual function
A class in C that contains a pure virtual function is called It is an "abstract class". Abstract classes cannot use new to create objects. Only subclasses that implement this pure virtual function can create new objects.
The pure virtual function in C is more like "only providing declarations, no implementation". It is a constraint on subclasses and "interface inheritance".
Pure virtual functions in C are also a kind of "runtime polymorphism".
If the following class contains pure virtual functions, it is an "abstract class":
class A { public: virtual void out1(string s)=0; //我是基类的虚函数 };
For example
#include<iostream> #include<string> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; class a { private: public: a(){ //构造函数用内联函数的形式 } //虚函数 virtual void xhs(){ //这个虚函数必须得在基类中实现 cout<<"我是基类的虚函数"<<endl;//即使是空的虚函数也要在基类中实现 } //派生类中可以不写这个函数,但是派生类对象调用时会调用积累的虚函数 //纯虚函数 virtual void cxhs() =0; //这个纯虚函数不在基类中实现,必须在子类中实现 }; class b:public a { private: public: void xhs(){ //这个是可有可无的 cout<<"我是派生类覆盖基类虚函数的函数"<<endl; } //* //* void cxhs(){ //这个是必须有实现的 cout<<"我是派生类覆盖基类虚函数的函数"<<endl; } //* //* }; //* //* int main() //* //* { //* //* b c; //* //* c.xhs(); //* //调用派生类的 c.cxhs();//调用派生类的 }
Virtual functions and pure virtual functions The difference
1) Pure virtual functions only have definitions and no implementation; while virtual functions have both definitions and implementation codes.
Pure virtual functions generally do not have a code implementation part, such as virtual void print() = 0; 2)
General virtual functions must have a code implementation part, otherwise the function will be undefined mistake.
2) A class containing pure virtual functions cannot define its objects, but a class containing virtual functions can.
The above is the detailed content of What is the difference between virtual function and pure virtual function?. 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



Virtual function debugging methods: set breakpoints to step through; use assert() to verify conditions; use debugger tools to check dynamic types, function stacks and redefine virtual functions.

Function overloading in C++ allows defining different implementations for functions of the same name with different parameters, while virtual functions allow overriding base class functions in derived classes to achieve polymorphism. Function overloading and virtual functions can work together. By designing a virtual overloaded function in the base class, the derived class can only overload versions of specific parameter combinations, thereby providing more flexible polymorphism, such as calculating different types in practical cases The distance of the shape from its origin.

In C++, friend functions interact with virtual functions so that the friend function can access the virtual function and call the friend function in the derived class to access the private members of the base class. This interaction can be used to access data hidden in the inheritance hierarchy or to implement polymorphic behavior.

A virtual function is a polymorphism mechanism that allows a derived class to override the member functions of its base class: Declaration: Add the keyword virtual before the function name. Call: Using a base class pointer or reference, the compiler will dynamically bind to the appropriate implementation of the derived class. Practical case: By defining the base class Shape and its derived classes Rectangle and Circle, it demonstrates the application of virtual functions in polymorphism, calculating area and drawing shapes.

Virtual functions in C++ allow derived classes to redefine methods inherited from base classes to achieve polymorphism. The syntax is: declare a virtual function with the virtual keyword in the base class, and redefine it with override in the derived class. By calling a virtual function through a pointer or reference, a derived class object can call a base class virtual function. The main functions of virtual functions include: realizing polymorphism, supporting dynamic binding and providing abstraction.

In C++, pure virtual functions are declared but not implemented in the base class, forcing derived classes to implement specific behavior to promote polymorphism. Derived classes must implement all pure virtual functions, otherwise they must also become abstract classes. Pure virtual functions ensure polymorphism by ensuring that only classes that implement all pure virtual functions can be used as pointers or references to abstract classes. In practical cases such as graphics drawing, it can ensure that all shape objects can respond to draw() calls, achieve polymorphism, and improve the scalability and maintainability of the code.

Virtual functions use dynamic binding to determine the function to be called at runtime, achieving polymorphism. Its advantages include scalability and reusability, but it also introduces overhead and complexity. Virtual functions are often used to implement methods of different types of objects in a uniform way.

Virtual functions and pure virtual functions in C++ are commonly used tools by many programmers who use object-oriented programming. Under the premise of using them correctly, the flexibility and maintainability of the program can be greatly improved. This article will discuss the application skills of virtual functions and pure virtual functions, and share some practical development experiences. 1. Virtual function 1. What is a virtual function? Virtual functions are a technique used to implement polymorphism, which allows the member functions of a class to be dynamically determined at runtime. When a virtual function is called using a base class pointer or reference, the program will
