Home Backend Development C#.Net Tutorial What is the difference between virtual function and pure virtual function?

What is the difference between virtual function and pure virtual function?

Nov 17, 2020 am 09:30 AM
pure virtual function 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.

What is the difference between virtual function and pure virtual function?

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;
    }
};
Copy after login

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;  //我是基类的虚函数 
};
Copy after login

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();//调用派生类的 
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of C++ function debugging: How to debug problems in virtual functions? Detailed explanation of C++ function debugging: How to debug problems in virtual functions? May 02, 2024 pm 03:42 PM

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.

How do C++ function overloading and virtual functions work together? How do C++ function overloading and virtual functions work together? Apr 26, 2024 am 10:09 AM

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.

Interaction between C++ friend functions and virtual functions Interaction between C++ friend functions and virtual functions Apr 16, 2024 pm 03:45 PM

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.

How to declare and call virtual functions in C++? How to declare and call virtual functions in C++? Apr 12, 2024 pm 04:03 PM

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.

Can a C++ function be declared virtual? What is the role of virtual functions? Can a C++ function be declared virtual? What is the role of virtual functions? Apr 19, 2024 pm 06:06 PM

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.

How do pure virtual functions promote polymorphism in C++? How do pure virtual functions promote polymorphism in C++? Jun 03, 2024 pm 08:01 PM

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.

The secret of C++ virtual functions revealed The secret of C++ virtual functions revealed Apr 19, 2024 am 09:18 AM

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.

Application skills of virtual functions and pure virtual functions in C++ Application skills of virtual functions and pure virtual functions in C++ Aug 22, 2023 am 11:33 AM

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

See all articles