当前位置:网站首页>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 .
边栏推荐
- 2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )
- 【单片机仿真】(九)指令系统 — 算术运算指令 之 ADD、ADDC、SUBB、INC、DEC、DA
- C语言基础Day4-数组
- About XML file (VI) - the difference between JSON and XML file
- 人脸检测几种方法
- 【单片机仿真】(十三)指令系统逻辑运算指令 — 移位指令
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- 无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?
- Pytorch best practices and code templates
- GraphQL初识
猜你喜欢

05_ Service call ribbon

【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表

Rtx3090 installing pytorch3d

zsh: command not found: mysql

【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码

Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...

快照:数据快照(数据兜底方式)

GraphQL初识

The place where the dream begins ---- first knowing C language
![2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
随机推荐
Automatic assembly & set injection
We should increase revenue and reduce expenditure
【PHP】tp6多表连接查询
MySQL optimized index
Polynomial interpolation fitting (II)
内置键盘连续444
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
数据源对象管理(第三方对象资源) & 加载properties文件
【单片机仿真】(二)keil 安装教程
【单片机仿真】(八)指令系统 — 数据传送指令
Fiddler抓包
Face key point detection
[single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
C语言基础Day4-数组
Comparison between redis and other databases
First blog ----- self introduction
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]
C language foundation day4 array
ncnn paramdict&modelbin