Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction and function of Virtual function in C++

Shulou Source: shulou.com Published: 2022-06-03 19:37:01 10月02日 Update

This article mainly explains "introduction and function of virtual functions in C++". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the introduction and function of virtual functions in C++.

Catalogue

Overview

Usage

Association

Static association

Dynamic association

Case 1

Virtual functions are not used

Use virtual classes

Case 2

Summary

Overview

Virtual functions (virtual function) refer to functions that can be inherited and overridden by subclasses.

Usage

The method that the base class declares a member function as a virtual function:

Virtual [type] function name ([parameter list])

Note: when defining virtual functions outside the class, there is no need to add virtual.

Characteristics of virtual functions:

Improve program extensibility: derived classes can override functions as needed

After a member function is declared as an imaginary number, the overlay function in its derived class is automatically called a virtual function.

If the virtual function is not redefined in the derived class, the derived class simply inherits the virtual function of its direct base class.

A pointer to the base class, and when pointing to a derived class object, you can use the methods of the derived class

Association

Through binding, we can associate an identifier with a storage address, or bind a function name to a class object.

Static association

Static correlation (static binding) refers to the invocation of virtual functions by object names. At compile time, you can determine which class the virtual function it calls belongs to.

Dynamic association

Dynamic correlation (dynamic binding) refers to the determination of the relationship between the base class pointer and the virtual function in the run-time. Dynamic association provides dynamic polymorphism, that is, run-time polymorphism.

Case 1 does not use virtual functions

Square class:

# ifndef PROJECT6_SQUARE_H#define PROJECT6_SQUARE_Hclass Square {protected: int length;public: Square (int l): length (l) {}; int area () const {return length * length;}}; # endif / / PROJECT6_SQUARE_H

Rectangle class:

# ifndef PROJECT6_RECTANGLE_H#define PROJECT6_RECTANGLE_H#include "Square.h" class Rectangle: public Square {private: int height;public: Rectangle (int l, int h): Square (l), height (h) {}; int area () const {return Square::area () 2 + length * height * 4; / / two bases plus four edges}}; # endif / / PROJECT6_RECTANGLE_H

Main:

# include # include "Square.h" # include "Rectangle.h" using namespace std;int main () {/ / create object Square S1 (2), * pt; Rectangle R1 (3,3); pt = & S1; cout area ()

Tags: Functions associations objects members functions dynamic pointers methods cases outputs Cymbols functions results static polymorphic two usage content address polymorphism Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Docker Linux Huawei