継承は基本的な概念です。 OOP (オブジェクト指向プログラミング) 。これは、あるクラスが別のクラスの機能 (フィールドとメソッド) を継承できるようにするメカニズムです。継承とは、既存のクラスに基づいて新しいクラスを作成することを意味します。別のクラスを継承したクラスは、そのクラスのメソッドとフィールドを再利用できます。
例: 次の例では、Animal が基本クラスであり、Dog Cat と Cow は Animal クラスを拡張した派生クラスです。
Java文字から文字列へ
Inheritance C++ #include using namespace std; class Animal { public: void sound() { cout << 'Animal makes a sound' << endl; } }; class Dog : public Animal { public: void sound() { cout << 'Dog barks' << endl; } }; class Cat : public Animal { public: void sound() { cout << 'Cat meows' << endl; } }; class Cow : public Animal { public: void sound() { cout << 'Cow moos' << endl; } }; int main() { Dog d; d.sound(); Cat c; c.sound(); Cow cow; cow.sound(); return 0; }
出力
Dog barks Cat meows Cow moos
説明:
- Animal は関数 sound() を持つ基本クラスです。
- Dog Cat および Cow は、それぞれ独自の sound() メソッドを定義する派生クラスです。
- main() では Dog Cat と Cow のオブジェクトが別々に作成されます。
- 各オブジェクトで sound() を呼び出すと、それぞれの子クラス メソッドが実行されます (犬の鳴き声、猫の鳴き声、牛の鳴き声)。
構文
C++class ChildClass : public ParentClass { // Additional fields and methods };
C++ では継承はどのように機能しますか?
コロン (:) と アクセス指定子 C++ の継承に使用されます。これにより、派生クラス (子クラス) は、基本クラス (親クラス) のデータ メンバー (フィールド) およびメンバー関数 (メソッド) を継承できます。
クラスが別のクラスを継承すると、親クラスのアクセス可能なすべてのメンバーが取得され、子クラスはそれらのメンバーを再定義 (オーバーライド) したり、新しい機能を追加したりすることもできます。
C++ における継承の種類
以下は、C++ でサポートされているさまざまなタイプの継承です。
1. 単一継承
単一継承では、サブクラスは 1 つのスーパークラスのみから派生します。これは、単一親クラスのプロパティと動作を継承します。単純継承とも呼ばれる場合があります。
Single Inheritance C++ #include using namespace std; class Vehicle { public: Vehicle() { cout << 'This is a Vehicle' << endl; } }; class Car : public Vehicle { public: Car() { cout << 'This Vehicle is Car' << endl; } }; int main() { Car obj; return 0; }
出力
This is a Vehicle This Vehicle is Car
2. 多重継承
で 多重継承 1 つのクラスが複数のスーパークラスを持つことができ、すべての親クラスから機能を継承できます。
Multiple Inheritance C++ #include using namespace std; class LandVehicle { public: void landInfo() { cout << 'This is a LandVehicle' << endl; } }; class WaterVehicle { public: void waterInfo() { cout << 'This is a WaterVehicle' << endl; } }; // Derived class inheriting from both base classes class AmphibiousVehicle : public LandVehicle public WaterVehicle { public: AmphibiousVehicle() { cout << 'This is an AmphibiousVehicle' << endl; } }; int main() { AmphibiousVehicle obj; obj.waterInfo(); obj.landInfo(); return 0; }
出力
This is an AmphibiousVehicle This is a WaterVehicle This is a LandVehicle
3. 多レベルの継承
C++ におけるマルチレベル継承とは、クラスが継承のチェーンを形成する別の派生クラスから派生することを意味します。
Multilevel Inheritance C++ #include using namespace std; class Vehicle { public: Vehicle() { cout << 'This is a Vehicle' << endl; } }; // Derived class from Vehicle class FourWheeler : public Vehicle { public: FourWheeler() { cout << '4 Wheeler Vehicles' << endl; } }; // Derived class from FourWheeler class Car : public FourWheeler { public: Car() { cout << 'This 4 Wheeler Vehicle is a Car' << endl; } }; int main() { Car obj; return 0; }
出力
This is a Vehicle 4 Wheeler Vehicles This 4 Wheeler Vehicle is a Car
4. 階層的な継承
階層継承では、複数のサブクラスが 1 つの基本クラスから継承されます。つまり、単一の基本クラスから複数の派生クラスが作成されます。たとえば、車とバスは両方とも車両です。
HTMLからJavaScript呼び出し関数
Hierarchical Inheritance C++ #include using namespace std; class Vehicle { public: Vehicle() { cout << 'This is a Vehicle' << endl; } }; class Car : public Vehicle { public: Car() { cout << 'This Vehicle is Car' << endl; } }; class Bus : public Vehicle { public: Bus() { cout << 'This Vehicle is Bus' << endl; } }; int main() { Car obj1; Bus obj2; return 0; }
出力
This is a Vehicle This Vehicle is Car This is a Vehicle This Vehicle is Bus
5. ハイブリッド継承
2 つ以上の種類の継承が 1 つのプログラムに組み合わされる場合。たとえば、クラスは複数の継承を使用し、複数レベルの継承チェーンの一部である場合があります。
Hybrid Inheritance C++ #include using namespace std; class Vehicle { public: Vehicle() { cout << 'This is a Vehicle' << endl; } }; class Fare { public: Fare() { cout << 'Fare of Vehicle' << endl; } }; class Car : public Vehicle { public: Car() { cout << 'This Vehical is a Car' << endl; } }; class Bus : public Vehicle public Fare { public: Bus() { cout << 'This Vehicle is a Bus with Fare'; } }; int main() { Bus obj2; return 0; }
出力
This is a Vehicle Fare of Vehicle This Vehicle is a Bus with Fare
ハイブリッド継承により次のような問題が発生する可能性があります。 ダイヤモンドの問題 C++で。これは、クラスが同じ基本クラスを共有する 2 つのクラスを継承する場合に発生します。その結果、派生クラスは基本クラスのメンバーのコピーを複数取得するため、どれを使用するかが曖昧になります。
注記 : 解決策は、仮想継承を使用して、基本クラスのコピーを 1 つだけ共有することです。
C++ における継承の利点
- コードの再利用性 : 派生クラスは、コードの重複を避けるために、基本クラスのデータ メンバーとメソッドを直接再利用できます。
- 抽象化 : 抽象化を強制する共通インターフェイスを定義する抽象クラス (純粋仮想関数を持つクラス) をサポートします。
- クラス階層: 階層 (基本→派生→さらに派生) を構築して、現実世界の関係をモデル化できます。
- 多態性 : 仮想関数による実行時のポリモーフィズムと、関数のオーバーロードとテンプレートによるコンパイル時のポリモーフィズムを完全にサポートします。
C++ における継承の欠点
- 密結合 : 子クラスは親クラスに依存します。基本クラスを変更すると、派生クラスも強制的に変更される可能性があります。
- 柔軟性の低下 : 場合によっては、合成 (has-a 関係) の方が適切な場合に継承が誤用され、コードの柔軟性が低下します。
- 複雑さの増加: 継承階層が深い (マルチレベルまたはハイブリッド) と、コードの保守とデバッグが難しくなる可能性があります。
- ダイヤモンドの問題 : ハイブリッドまたは多重継承では、同じ基本クラスが複数回継承されるとあいまいさが発生する可能性があります。
- 仮想関数のオーバーヘッド: ポリモーフィズム (仮想関数) が使用されている場合、仮想テーブルの検索により実行時のオーバーヘッドがわずかに発生します。