C ++中的抽象类和纯虚函数

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

抽象类是一个其中至少包含一个纯虚函数的类。 抽象类用于为其子类提供接口。 继承抽象类的类必须为纯虚函数提供定义,否则它们也将成为抽象类。

抽象类的特征 (Characteristics of Abstract Class)

  1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created.

    无法实例化Abstract类,但是可以创建Abstract类类型的指针和引用。

  2. Abstract class can have normal functions and variables along with a pure virtual function.

    抽象类可以具有普通函数和变量以及纯虚函数。

  3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.

    抽象类主要用于Upcasting,以便其派生类可以使用其接口。

  4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

    继承Abstract类的类必须实现所有纯虚函数,否则它们也将成为Abstract。

C ++中的纯虚函数 (Pure Virtual Functions in C++)

Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with = 0. Here is the syntax for a pure virtual function,

纯虚函数是没有定义的虚函数。 它们以虚拟关键字开头,以= 0结尾。 这是纯虚函数的语法,

virtual void f() = 0;

C ++中抽象类的示例 (Example of Abstract Class in C++)

Below we have a simple example where we have defined an abstract class,

在下面的示例中,我们定义了一个抽象类,

//Abstract base class
class Base          
{
    public:
    virtual void show() = 0;    // Pure Virtual Function
};

class Derived:public Base
{
    public:
    void show()
    { 
        cout << "Implementation of Virtual Function in Derived class\n"; 
    }
};

int main()
{
    Base obj;   //Compile Time Error
    Base *b;
    Derived d;
    b = &d;
    b->show();
}

Implementation of Virtual Function in Derived class

派生类中虚拟功能的实现

In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.

在上面的示例中,基类是抽象的,具有纯虚拟的show()函数,因此我们无法创建基类的对象。

为什么我们不能创建抽象类的对象? (Why can't we create Object of an Abstract Class?)

When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE(studied in last topic), but doesn't put any address in that slot. Hence the VTABLE will be incomplete.

当我们在Abstract类中创建纯虚函数时,我们为VTABLE中的函数保留了一个插槽(在上一主题中进行了研究),但没有在该插槽中放置任何地址。 因此,VTABLE将不完整。

As the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an errror message whenever you try to do so.

由于VTABLE for Abstract类不完整,因此,编译器将不允许创建此类的对象,并且在您尝试执行此操作时将显示错误消息。

纯虚拟定义 (Pure Virtual definitions)

  • Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class.

    可以在Abstract类中给Pure Virtual函数一个小的定义,您希望所有派生类都具有该定义。 仍然不能创建Abstract类的对象。

  • Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.

    另外,必须在类定义之外定义Pure Virtual函数。 如果要在类定义中定义它,则编译器将给出错误。 内联纯虚拟定义是非法的。

// Abstract base class
class Base         
{
    public:
    virtual void show() = 0;     //Pure Virtual Function
};

void Base :: show()     //Pure Virtual definition
{
    cout << "Pure Virtual definition\n";
}

class Derived:public Base
{
    public:
    void show()
    { 
        cout << "Implementation of Virtual Function in Derived class\n"; 
    }
};

int main()
{
    Base *b;
    Derived d;
    b = &d;
    b->show();
}

Implementation of Virtual Function in Derived class

派生类中虚拟功能的实现

翻译自: https://www.studytonight.com/cpp/abstract-class-and-pure-virtual.php

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值