C++中的纯虚函数和抽象类详细指南

2021年4月12日09:18:10 发表评论 774 次浏览

有时, 由于我们不知道具体实现, 因此无法在基类中提供所有功能的具体实现。这样的类称为抽象类。例如, 让Shape为基类。我们无法在Shape中提供功能draw()的实现, 但是我们知道每个派生类都必须具有draw()的实现。同样, 动物类也没有move()的实现(假设所有动物都在移动), 但是所有动物都必须知道如何移动。我们不能创建抽象类的对象。

C++++中的纯虚函数(或抽象函数)是虚拟功能对于我们没有实现的内容, 我们只声明它。通过在声明中分配0来声明纯虚函数。请参见以下示例。

//An abstract class
class Test
{   
     //Data members of class
public :
     //Pure Virtual Function
     virtual void show() = 0;
    
    /* Other members */
};

一个完整的例子:

纯粹的虚函数由从Abstract类派生的类实现。以下是一个简单的示例来演示相同的内容。

#include<iostream>
using namespace std;
  
class Base
{
    int x;
public :
     virtual void fun() = 0;
     int getX() { return x; }
};
  
//This class inherits from Base and implements fun()
class Derived: public Base
{
     int y;
public :
     void fun() { cout <<"fun() called" ; }
};
  
int main( void )
{
     Derived d;
     d.fun();
     return 0;
}

输出如下:

fun() called

一些有趣的事实:

1)如果一个类至少具有一个纯虚函数, 则它是抽象的。

在以下示例中, Test是一个抽象类, 因为它具有纯虚函数show()。

//pure virtual functions make a class abstract
#include<iostream>
using namespace std;
  
class Test
{
    int x;
public :
     virtual void show() = 0;
     int getX() { return x; }
};
  
int main( void )
{
     Test t;
     return 0;
}

输出如下:

Compiler Error: cannot declare variable 't' to be of abstract
 type 'Test' because the following virtual functions are pure 
within 'Test': note:     virtual void Test::show()

2)我们可以有抽象类类型的指针和引用。

例如, 以下程序可以正常运行。

#include<iostream>
using namespace std;
  
class Base
{
public :
     virtual void show() = 0;
};
  
class Derived: public Base
{
public :
     void show() { cout <<"In Derived \n" ; }
};
  
int main( void )
{
     Base *bp = new Derived();
     bp->show();
     return 0;
}

输出如下:

In Derived

3)如果我们不重写派生类中的纯虚函数, 则派生类也将成为抽象类。

下面的示例演示了相同的内容。

#include<iostream>
using namespace std;
class Base
{
public :
     virtual void show() = 0;
};
  
class Derived : public Base { };
  
int main( void )
{
   Derived d;
   return 0;
}
Compiler Error: cannot declare variable 'd' to be of abstract type 
'Derived'  because the following virtual functions are pure within
'Derived': virtual void Base::show()

4)抽象类可以具有构造函数。

例如, 以下程序可以编译并正常运行。

#include<iostream>
using namespace std;
  
//An abstract class with constructor
class Base
{
protected :
    int x;
public :
   virtual void fun() = 0;
   Base( int i) { x = i; }
};
  
class Derived: public Base
{
     int y;
public :
     Derived( int i, int j):Base(i) { y = j; }
     void fun() { cout <<"x = " <<x <<", y = " <<y; }
};
  
int main( void )
{
     Derived d(4, 5);
     d.fun();
     return 0;
}

输出如下:

x = 4, y = 5

与Java比较

在Java中, 可以使用abstract关键字将类抽象化。类似地, 可以通过使用abstract关键字使函数成为纯虚函数或抽象函数。

接口与抽象类:

接口没有任何方法的实现, 可以将其视为方法声明的集合。在C++++中, 可以通过将所有方法设为纯虚拟方法来模拟接口。在Java中, 接口有一个单独的关键字。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: