In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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 ()
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.