当前位置:网站首页>Pure virtual function
Pure virtual function
2022-07-19 03:15:00 【Rock–】
Pure virtual function
- Just give the declaration in the base class , No virtual function implementation is defined , Instead, implement... In a derived class . This virtual function is called pure virtual function . If an ordinary function only gives its declaration and does not implement its function body , This can't be compiled . A pure virtual function has no body , A pure virtual function needs to be declared with =0;
- Define format :
class < Base class name >
{
virtual < type >< Function name >(< Parameter table >)=0;
......
};
Classes with pure virtual functions are called abstract classes
Classes with pure virtual functions are called abstract classes . Abstract classes can only be used as the base class of derived classes , Cannot define objects , But you can define pointers . After the derived class implements the pure virtual function , Define a pointer to an abstract class object , And point to or reference subclass objects .
In defining pure virtual functions , You can't define the implementation part of a virtual function ;
Before redefining this pure virtual function , You can't call this function .
The only purpose of an abstract class is to provide a base class for derived classes , Pure virtual functions are used as the basis of member functions in derived classes , And realize dynamic polymorphism . If the derived class inherited from the abstract class cannot implement all the pure virtual functions in the base class , Then the derived class becomes an abstract class . Because it inherits the abstract functions of the base class , A class that contains pure virtual functions is an abstract class . The pure virtual function has defined the declaration of this method in the abstract class , Other classes can only implement according to this interface .
The difference between interface and abstract class
C++ The interface we generally call in , Means the method provided externally , Provided to external calls . It is a bridge between the outside and the inside . It is also provided in the form of classes , But generally, this class only has member functions , No data members ;
Abstract classes can contain both data members and methods .
An instance of an abstract class
- abstract class IShape As the base class : Only header files , No implementation file
#ifndef SHAPE_H
#define SHAPE_H
#include
using std::string;
//interface
class IShape
{
public:
virtual float getArea()=0; // Pure virtual function , Get the area
virtual string getName()=0; // Pure virtual function , Returns the name of the drawing
};
#endif
2. Derived class Circle Class inherits from abstract class IShape:
Circle.h The header file :
#ifndef CIRCLE_H
#define CIRCLE_H
#include"Shape.h"
class CCircle : public IShape // The public is inherited from IShape class
{
public:
CCircle(float radius); // Constructors
public:
virtual float getArea(); // Implementation declaration implements functions of two base classes , When declaring, you need to add virtual keyword , There is no need to add virtual Keyword. .
virtual string getName();
private:
float m_fRadius; // Derived classes can have their own members
};
#endif
Circle.cpp Implementation file :
#include"Circle.h"
CCircle::CCircle(float radius)
:m_fRadius(radius) // Use the initialization list of the constructor to initialize
{
}
float CCircle::getArea() / / Implement functions that implement two base classes
virtual string getName();
{
return 3.14 * m_fRadius * m_fRadius;
}
string CCircle::getName()
{
return "CCircle";
}
3. Derived class Rect Class inherits from abstract class IShape:
Rect.h The header file :
#ifndef RECT_H
#define RECT_H
#include"shape.h"
class CRect : public IShape
{
public:
CRect(float nWidth, float nHeight);
public:
virtual float getArea();
virtual string getName();
private:
float m_fWidth; // The rectangle class has its own two properties , Width and height
float m_fHeight;
};
Rect.cpp Implementation file :
#include"Rect.h"
CRect::CRect(float fWidth, float fHeight)
:m_fWidth(fWidth), m_fHeight(fHeight)
{
}
float CRect::getArea()
{
return m_fWidth * m_fHeight;
}
string CRect::getName()
{
return "CRect";
}
4. The test file main.cpp:
#include
#include"Rect.h"
#include"Circle.h"
using namespace std;
int main()
{
IShape* pShape = NULL; // Defines a pointer to an abstract class , Note that abstract classes cannot define objects but can define pointers
pShape = new CCircle(20.2); // The base class pointer points to the object of the derived class
cout<getName()<<" "<getArea()<<endl;
delete pShape; // The release of the CCirle Memory occupied by objects , But the pointer doesn't disappear , It is now a wild pointer , We must assign a value to it before using
pShape = new CRect(20, 10); // The base class pointer points to the object of the derived class
cout<getName()<<" "<getArea()<<endl;
return 0;
}
The operation results are as follows : You can see , We use the pointer of the parent class to call the same function , The corresponding functions of these two derived classes are called respectively ,
It determines the method to call according to the type pointed to by the pointer . Even if we need to add several new classes in the future , We still call this method , This is the great charm of polymorphism .
边栏推荐
- First knowledge of JPA (ORM idea, basic operation of JPA)
- [MCU simulation] (VI) addressing mode - index addressing and relative addressing
- Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
- RTX3090安装pytorch3D
- [MCU simulation] (VII) addressing mode - bit addressing
- D. Permutation restoration (greedy / double pointer /set)
- 【单片机仿真】(二十一)DB(Define Byte)— 定义字节
- Specifications, multi table query basis
- Transaction and storage engine in MySQL database
- [MCU simulation] (XIII) instruction system logic operation instruction shift instruction
猜你喜欢

ubuntu清除cuda缓存

Graphql first acquaintance

Is there really no way out for functional testing? 10K capping is never a joke

2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
![[template record] string hash to judge palindrome string](/img/90/43109c6bd008990fa5b8147e629685.png)
[template record] string hash to judge palindrome string

无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?

樂視還有400多比特員工?過著沒有老板的神仙日子 官方出來回應了...
![[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th](/img/fd/f0c0e17f2942525a78fbd443514992.png)
[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th

Code demonstration of fcos face detection model in openvino

Comparison between redis and other databases
随机推荐
Redis和其他数据库的比较
【单片机仿真】(十三)指令系统逻辑运算指令 — 移位指令
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
Is there really no way out for functional testing? 10K capping is never a joke
Data source object management (third-party object resources) & load properties file
Examine your investment path
05_服务调用Ribbon
Go语言 实现发送短信验证码 并登录
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]
数据源对象管理(第三方对象资源) & 加载properties文件
Fiddler grabbing
While loop
乐视还有400多位员工?过着没有老板的神仙日子 官方出来回应了...
05 central processing unit
[single chip microcomputer simulation] (10) instruction system - multiplication instruction and division of arithmetic operation instruction
Specifications、多表查询基础
Common MySQL commands you can use
【单片机仿真】(一)Proteus8.9 安装教程
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code