C++ ポリモーフィズム

C++ ポリモーフィズム

ポリモーフィズムという言葉は、多くの形式を持つことを意味します。簡単に言うと、ポリモーフィズムは、メッセージを複数の形式で表示できる機能と定義できます。ポリモーフィズムの実例としては、同時に異なる特性を持つことができる人が挙げられます。男性は同時に父親であり、夫であり、従業員でもあります。したがって、同じ人が状況によって異なる行動を示します。これをポリモーフィズムと呼びます。ポリモーフィズムは、オブジェクト指向プログラミングの重要な機能の 1 つと考えられています。

ポリモーフィズムの種類

  • コンパイル時のポリモーフィズム
  • ランタイムポリモーフィズム
ポリモーフィズムの種類

ポリモーフィズムの種類

1. コンパイル時のポリモーフィズム

このタイプの多態性は、関数のオーバーロードまたは演算子のオーバーロードによって実現されます。

A. 関数のオーバーロード

同じ名前でパラメータが異なる複数の関数がある場合、それらの関数は次のように呼ばれます。 過負荷、 したがって、これは関数のオーバーロードとして知られています。関数は次のようにオーバーロードできます。 引数の数を変更する または 引数の型を変更する 。簡単に言うと、これはオブジェクト指向プログラミングの機能であり、多数のタスクが 1 つの関数名の下にリストされている場合に、同じ名前で異なるパラメーターを持つ多数の関数が提供されます。関数をオーバーロードする際には、関数のオーバーロードに関する特定のルールに従う必要があります。

以下は、関数のオーバーロードまたはコンパイル時のポリモーフィズムを示す C++ プログラムです。

C++




// C++ program to demonstrate> // function overloading or> // Compile-time Polymorphism> #include> using> namespace> std;> class> Geeks {> public> :> > // Function with 1 int parameter> > void> func(> int> x)> > {> > cout < <> 'value of x is '> < < x < < endl;> > }> > // Function with same name but> > // 1 double parameter> > void> func(> double> x)> > {> > cout < <> 'value of x is '> < < x < < endl;> > }> > // Function with same name and> > // 2 int parameters> > void> func(> int> x,> int> y)> > {> > cout < <> 'value of x and y is '> < < x < <> ', '> < < y> > < < endl;> > }> };> // Driver code> int> main()> {> > Geeks obj1;> > // Function being called depends> > // on the parameters passed> > // func() is called with int value> > obj1.func(7);> > // func() is called with double value> > obj1.func(9.132);> > // func() is called with 2 int values> > obj1.func(85, 64);> > return> 0;> }>

出力

value of x is 7 value of x is 9.132 value of x and y is 85, 64 

説明: 上の例では、function という名前の単一の関数 関数() は 3 つの異なる状況で異なる動作をします。これはポリモーフィズムの特性です。これについて詳しく知りたい場合は、記事を参照してください。 C++ での関数のオーバーロード

B. 演算子のオーバーロード

C++ には、演算子にデータ型の特別な意味を与える機能があり、この機能は演算子のオーバーロードとして知られています。たとえば、文字列クラスの加算演算子 (+) を使用して、2 つの文字列を連結できます。この演算子のタスクは 2 つのオペランドを追加することであることがわかります。したがって、単一の演算子「+」を整数オペランドの間に置くとそれらを追加し、文字列オペランドの間に置くとそれらを連結します。

以下は、演算子のオーバーロードを示す C++ プログラムです。

CPP




// C++ program to demonstrate> // Operator Overloading or> // Compile-Time Polymorphism> #include> using> namespace> std;> class> Complex {> private> :> > int> real, imag;> public> :> > Complex(> int> r = 0,> int> i = 0)> > {> > real = r;> > imag = i;> > }> > // This is automatically called> > // when '+' is used with between> > // two Complex objects> > Complex operator+(Complex> const> & obj)> > {> > Complex res;> > res.real = real + obj.real;> > res.imag = imag + obj.imag;> > return> res;> > }> > void> print() { cout < < real < <> ' + i'> < < imag < < endl; }> };> // Driver code> int> main()> {> > Complex c1(10, 5), c2(2, 4);> > // An example call to 'operator+'> > Complex c3 = c1 + c2;> > c3.print();> }>

出力

12 + i9 

説明: 上の例では、演算子「+」がオーバーロードされています。通常、この演算子は 2 つの数値 (整数または浮動小数点数) を加算するために使用されますが、ここでは 2 つの虚数または複素数の加算を実行する演算子を作成します。これについて詳しく知りたい場合は、記事を参照してください。 演算子のオーバーロード

2. 実行時のポリモーフィズム

このタイプの多態性は次のようにして実現されます。 関数のオーバーライド 。遅延バインディングと動的ポリモーフィズムは、実行時ポリモーフィズムの別名です。 関数呼び出しは実行時に解決されます。 ランタイムポリモーフィズム 。対照的に、コンパイル時ポリモーフィズムでは、コンパイラは実行時にオブジェクトを推定した後、どの関数呼び出しをオブジェクトにバインドするかを決定します。

A. 関数のオーバーライド

関数のオーバーライド この問題は、派生クラスに基本クラスのメンバー関数の 1 つが定義されている場合に発生します。その基本関数はオーバーライドされると言われます。

C++ での関数のオーバーライド

関数オーバーライド 説明

データメンバーによる実行時のポリモーフィズム

実行時ポリモーフィズムは、C++ のデータ メンバーでは実現できません。派生クラスのインスタンスを参照する親クラスの参照変数によってフィールドにアクセスする例を見てみましょう。

C++




// C++ program for function overriding with data members> #include> using> namespace> std;> // base class declaration.> class> Animal {> public> :> > string color => 'Black'> ;> };> // inheriting Animal class.> class> Dog :> public> Animal {> public> :> > string color => 'Grey'> ;> };> // Driver code> int> main(> void> )> {> > Animal d = Dog();> // accessing the field by reference> > // variable which refers to derived> > cout < < d.color;> }>

出力

Black 

親クラスの参照は常に親クラスのデータ メンバーを参照することがわかります。

B. 仮想機能

仮想関数 は、キーワード virtual を使用して基本クラスで宣言され、派生クラスで再定義 (オーバーライド) されるメンバー関数です。

仮想機能に関する重要なポイント:

  • 仮想関数は本質的に動的です。
  • キーワードを挿入することで定義されます。 バーチャル 基本クラス内にあり、常に基本クラスで宣言され、子クラスでオーバーライドされます。
  • 実行時に仮想関数が呼び出される

以下は、仮想関数を示す C++ プログラムです。

C++




// C++ Program to demonstrate> // the Virtual Function> #include> using> namespace> std;> // Declaring a Base class> class> GFG_Base {> public> :> > // virtual function> > virtual> void> display()> > {> > cout < <> 'Called virtual Base Class function'> > < <> ' '> ;> > }> > void> print()> > {> > cout < <> 'Called GFG_Base print function'> > < <> ' '> ;> > }> };> // Declaring a Child Class> class> GFG_Child :> public> GFG_Base {> public> :> > void> display()> > {> > cout < <> 'Called GFG_Child Display Function'> > < <> ' '> ;> > }> > void> print()> > {> > cout < <> 'Called GFG_Child print Function'> > < <> ' '> ;> > }> };> // Driver code> int> main()> {> > // Create a reference of class GFG_Base> > GFG_Base* base;> > GFG_Child child;> > base = &child;> > // This will call the virtual function> > base->GFG_Base::display();>> > // this will call the non-virtual function> > base->print();>>

出力

Called virtual Base Class function Called GFG_Base print function 

例 2:

C++




// C++ program for virtual function overriding> #include> using> namespace> std;> class> base {> public> :> > virtual> void> print()> > {> > cout < <> 'print base class'> < < endl;> > }> > void> show() { cout < <> 'show base class'> < < endl; }> };> class> derived :> public> base {> public> :> > // print () is already virtual function in> > // derived class, we could also declared as> > // virtual void print () explicitly> > void> print() { cout < <> 'print derived class'> < < endl; }> > void> show() { cout < <> 'show derived class'> < < endl; }> };> // Driver code> int> main()> {> > base* bptr;> > derived d;> > bptr = &d;> > // Virtual function, binded at> > // runtime (Runtime polymorphism)> > bptr->print();>> return> 0;> }>

出力

print derived class show base class