CPlusPlusThings/basic_content/abstract/pure_virtual.cpp

33 lines
632 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file pure_virtual.cpp
* @brief 纯虚函数:没有函数体的虚函数
* 抽象类:包含纯虚函数的类
*
* @author 光城
* @version v1
* @date 2019-07-20
*/
#include <iostream>
using namespace std;
class A {
private:
int a;
public:
virtual void show() = 0; // 纯虚函数
};
int main() {
/*
* 1. 抽象类只能作为基类来派生新类使用
* 2. 抽象类的指针和引用->由抽象类派生出来的类的对象!
*/
A a; // error 抽象类,不能创建对象
A *a1; // ok 可以定义抽象类的指针
A *a2 = new A(); // error, A是抽象类不能创建对象
}