C++ の純粋仮想関数と抽象クラス
実装がわからないため、すべての関数の実装を基本クラスで提供できない場合があります。このようなクラスは と呼ばれます 抽象クラス たとえば、Shape を基本クラスとします。 Shape で関数draw()の実装を提供することはできませんが、すべての派生クラスにはdraw()の実装が必要であることがわかっています。同様に、Animal クラスには move() の実装がありません (すべての動物が移動すると仮定) が、すべての動物が移動方法を知っている必要があります。抽象クラスのオブジェクトを作成することはできません。
あ 純粋仮想関数 C++ の (または抽象関数) は、実装できる仮想関数です。ただし、その関数を派生クラスでオーバーライドする必要があります。そうしないと、派生クラスも抽象クラスになってしまいます。純粋仮想関数は、宣言内で 0 を割り当てることによって宣言されます。
純粋な仮想関数の例
C++
// An abstract class> class> Test {> > // Data members of class> public> :> > // Pure Virtual Function> > virtual> void> show() = 0;> > /* Other members */> };> |
完全な例
純粋な仮想関数は、抽象クラスから派生したクラスによって実装されます。
C++
// C++ Program to illustrate the abstract class and virtual> // functions> #include> using> namespace> std;> class> Base {> > // private member variable> > int> x;> public> :> > // pure virtual function> > virtual> void> fun() = 0;> > // getter function to access x> > int> getX() {> return> x; }> };> // This class inherits from Base and implements fun()> class> Derived :> public> Base {> > // private member variable> > int> y;> public> :> > // implementation of the pure virtual function> > void> fun() { cout < <> 'fun() called'> ; }> };> int> main(> void> )> {> > // creating an object of Derived class> > Derived d;> > // calling the fun() function of Derived class> > d.fun();> > return> 0;> }> |
出力
fun() called
いくつかの興味深い事実
1. 少なくとも 1 つの純粋仮想関数を持つクラスは抽象クラスです。
例
以下の C++ コードでは、Test には純粋な仮想関数 show() があるため、Test は抽象クラスです。
C++
// C++ program to illustrate the abstract class with pure> // virtual functions> #include> using> namespace> std;> class> Test {> > // private member variable> > int> x;> public> :> > // pure virtual function> > virtual> void> show() = 0;> > // getter function to access x> > int> getX() {> return> x; }> };> int> main(> void> )> {> > // Error: Cannot instantiate an abstract class> > 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. 抽象クラス型のポインタと参照を持つことができます。
たとえば、次のプログラムは正常に動作します。
C++
// C++ program that demonstrate that> // we can have pointers and references> // of abstract class type.> #include> using> namespace> std;> class> Base {> public> :> > // pure virtual function> > virtual> void> show() = 0;> };> class> Derived :> public> Base {> public> :> > // implementation of the pure virtual function> > void> show() { cout < <> 'In Derived
'> ; }> };> int> main(> void> )> {> > // creating a pointer of type> > // Base pointing to an object> > // of type Derived> > Base* bp => new> Derived();> > // calling the show() function using the> > // pointer> > bp->show();>> |