logo

C++ における継承

の能力 クラス 別のクラスからプロパティと特性を派生することを呼び出す 継承 。継承はオブジェクト指向プログラミングの最も重要な機能の 1 つです。

継承は、既存のクラスから新しいクラスが作成される機能またはプロセスです。作成された新しいクラスは派生クラスまたは子クラスと呼ばれ、既存のクラスは基本クラスまたは親クラスと呼ばれます。現在の派生クラスは、基本クラスから継承されていると言われます。



派生クラスが基本クラスを継承すると言う場合、派生クラスは基本クラスのプロパティを変更せずに、基本クラスのすべてのプロパティを継承し、独自のクラスに新しい機能を追加する可能性があることを意味します。派生クラスのこれらの新機能は、基本クラスには影響しません。派生クラスは、基本クラスの特殊化されたクラスです。

  • サブクラス: 他のクラスのプロパティを継承したクラスをサブクラスまたは派生クラスと呼びます。
  • スーパークラス: サブクラスにプロパティが継承されるクラスは、基本クラスまたはスーパークラスと呼ばれます。

この記事は次のサブトピックに分かれています。

  • 継承を使用する理由とタイミングは何ですか?
  • 継承のモード
  • 相続の種類

継承を使用する理由とタイミングは何ですか?

車両のグループを考えてみましょう。 Bus、Car、Truck のクラスを作成する必要があります。メソッドfuelAmount()、capacity()、applyBrakes()は、3つのクラスすべてで同じになります。継承を回避してこれらのクラスを作成する場合、以下の図に示すように、これらの関数をすべて 3 つのクラスのそれぞれに記述する必要があります。



C++ での継承

CSV Javaから読み取る

上記のプロセスにより、同じコードが 3 回重複することが明確にわかります。これにより、エラーやデータの冗長性が発生する可能性が高まります。このような状況を回避するには、継承を使用します。 Vehicle クラスを作成し、その中にこれら 3 つの関数を記述し、残りのクラスを Vehicle クラスから継承すると、データの重複を簡単に回避して再利用性を高めることができます。 3 つのクラスが vehicle クラスから継承されている以下の図を見てください。

C++ の継承アプリケーション



継承を使用すると、残りの 3 つのクラスを基本クラス (Vehicle) から継承しているため、関数を 3 回作成する必要はなく 1 回だけ作成する必要があります。
C++ での継承の実装 : 基本クラスから継承されるサブクラスを作成するには、以下の構文に従う必要があります。

派生クラス: 派生クラスは、基本クラスから派生したクラスとして定義されます。
構文 :

class :  {  //body }>

どこ
class — 新しいクラスを作成するためのキーワード
派生クラス名 — 基本クラスを継承する新しいクラスの名前
access-specifier — private、public、または protected のいずれか。どちらも指定されていない場合は、PRIVATE がデフォルトとして使用されます。
Base-class-name — 基本クラスの名前
注記 : 派生クラスは継承しません アクセス プライベートデータメンバーに。ただし、そのクラスが宣言するプライベート メンバーを含む完全な親オブジェクトを継承します。

例:
1. class ABC : private XYZ //プライベート導出
{ }
2. クラス ABC : パブリック XYZ //パブリック導出
{ }
3. class ABC : protected XYZ //protected 派生
{ }
4. class ABC: XYZ //デフォルトではプライベート導出
{ }

注記:

o 基本クラスが派生クラスによってプライベートに継承される場合、基本クラスのパブリック メンバーは派生クラスのプライベート メンバーになるため、基本クラスのパブリック メンバーには派生クラスのメンバー関数によってのみアクセスできます。派生クラスのオブジェクトにはアクセスできません。
o 一方、基底クラスが派生クラスによってパブリックに継承される場合、基底クラスのパブリック メンバーも派生クラスのパブリック メンバーになります。したがって、基本クラスのパブリック メンバーには、派生クラスのオブジェクトだけでなく、派生クラスのメンバー関数からもアクセスできます。

C++
// Example: define member function without argument within // the class #include  using namespace std; class Person {  int id;  char name[100]; public:  void set_p()  {  cout << 'Enter the Id:';  cin>>ID;  コート<< 'Enter the Name:';  cin>> 名前;  void display_p() { cout<< endl <<'Id: '<< id << '
Name: ' << name <> もちろん;  コート<< 'Enter the Course Fee:';  cin>> 料金;  void 表示_s() { 表示_p();  コート<<'Course: '<< course << '
Fee: ' << fee << endl;  } }; int main() {  Student s;  s.set_s();  s.display_s();  return 0; }>

出力:

Enter the Id: 101 Enter the Name: Dev Enter the Course Name: GCS Enter the Course Fee:70000  Id: 101 Name: Dev Course: GCS Fee: 70000>
C++
// Example: define member function without argument outside the class #include using namespace std; class Person {  int id;  char name[100];    public:  void set_p();  void display_p(); }; void Person::set_p() {  cout<<'Enter the Id:';  cin>>ID;  コート<<'Enter the Name:';  cin>>名前; } void Person::display_p() { cout<>コース;  コート<<'Enter the Course Fee:';  cin>>料金; void Student::display_s() {display_p();  コート<<'
Course: '<

出力:

Enter the Id: 101 Enter the Name: Dev Enter the Course Name: GCS Enter the Course Fee: 70000 Id: 101 Name: Dev Course: GCS Fee: 70000>
C++
// Example: define member function with argument outside the class #include #include using namespace std; class Person {  int id;  char name[100];    public:  void set_p(int,char[]);  void display_p(); }; void Person::set_p(int id,char n[]) {  this->id=id;  strcpy(this->name,n);  } void Person::display_p() { cout<

CPP
// C++ program to demonstrate implementation // of Inheritance #include  using namespace std; // Base class class Parent { public:  int id_p; }; // Sub class inheriting from Base Class(Parent) class Child : public Parent { public:  int id_c; }; // main function int main() {  Child obj1;  // An object of class child has all data members  // and member functions of class parent  obj1.id_c = 7;  obj1.id_p = 91;  cout << 'Child id is: ' << obj1.id_c << '
';  cout << 'Parent id is: ' << obj1.id_p << '
';  return 0; }>

出力
Child id is: 7 Parent id is: 91>

上記のプログラムでは、「Child」クラスは「Parent」クラスからパブリックに継承されるため、クラス「Parent」のパブリック データ メンバーもクラス「Child」によって継承されます。
継承のモード: 継承には 3 つのモードがあります。

  1. パブリックモード : パブリック基本クラスからサブクラスを派生した場合。その後、基本クラスのパブリック メンバーは派生クラスでパブリックになり、基本クラスの保護されたメンバーは派生クラスで保護されます。
  2. 保護モード : Protected 基本クラスからサブクラスを派生した場合。その後、基本クラスのパブリック メンバーと保護されたメンバーの両方が派生クラスで保護されます。
  3. プライベートモード : Private 基本クラスからサブクラスを派生した場合。その後、基本クラスのパブリック メンバーと保護されたメンバーの両方が、派生クラスではプライベートになります。

注記: 基本クラスのプライベート メンバーには派生クラスで直接アクセスできませんが、保護されたメンバーには直接アクセスできます。たとえば、以下の例では、クラス B、C、および D にはすべて変数 x、y、および z が含まれています。あくまでアクセスの問題です。

CPP
// C++ Implementation to show that a derived class // doesn’t inherit access to private data members. // However, it does inherit a full parent object. class A { public:  int x; protected:  int y; private:  int z; }; class B : public A {  // x is public  // y is protected  // z is not accessible from B }; class C : protected A {  // x is protected  // y is protected  // z is not accessible from C }; class D : private A // 'private' is default for classes {  // x is private  // y is private  // z is not accessible from D };>


以下の表は、上記の 3 つのモードをまとめたもので、パブリック モード、プロテクト モード、およびプライベート モードで派生された場合の、サブクラス内の基本クラスのメンバーのアクセス指定子を示しています。

相続の種類:-

  1. 単一継承
  2. マルチレベルの継承
  3. 多重継承
  4. 階層的な継承
  5. ハイブリッド継承

C++ における継承の種類

1. 単一継承 : 単一継承では、クラスは 1 つのクラスのみから継承できます。つまり、1 つのサブクラスは 1 つの基本クラスのみによって継承されます。

C++ の単一継承

構文 :

Javaのソート文字列
class subclass_name : access_mode base_class {  // body of subclass }; OR class A {  ... .. ...  }; class B: public A { ... .. ... };>
CPP
// C++ program to explain  // Single inheritance #include using namespace std; // base class class Vehicle {  public:  Vehicle()  {  cout << 'This is a Vehicle
';  } }; // sub class derived from a single base classes class Car : public Vehicle { }; // main function int main() {   // Creating object of sub class will  // invoke the constructor of base classes  Car obj;  return 0; }>

出力
This is a Vehicle>

C++
// Example: #include using namespace std; class A {  protected:  int a;    public:  void set_A()  {  cout<<'Enter the Value of A=';  cin>>あ;    void disp_A() { cout<出力:- A の値を入力 = 3 3 B の値を入力 = 5 5 3 * 5 の積 = 15

C++
// Example: #include using namespace std; class A {  protected:  int a;    public:  void set_A(int x)  {  a=x;   }    void disp_A()  {  cout<Product of 4 * 5 = 20>

2. 多重継承: 多重継承は、クラスが複数のクラスから継承できる C++ の機能です。つまり1つ サブクラス 複数から継承される 基本クラス

C++ での多重継承

構文 :

class subclass_name : access_mode base_class1, access_mode base_class2, .... {  // body of subclass }; class B {  ... .. ...  }; class C { ... .. ... }; class A: public B, public C { ... ... ... };>

ここで、基本クラスの数はカンマ (‘, ‘) で区切られ、すべての基本クラスのアクセス モードを指定する必要があります。

CPP
// C++ program to explain // multiple inheritance #include  using namespace std; // first base class class Vehicle { public:  Vehicle() { cout << 'This is a Vehicle
'; } }; // second base class class FourWheeler { public:  FourWheeler()  {  cout << 'This is a 4 wheeler Vehicle
';  } }; // sub class derived from two base classes class Car : public Vehicle, public FourWheeler { }; // main function int main() {  // Creating object of sub class will  // invoke the constructor of base classes.  Car obj;  return 0; }>

出力
This is a Vehicle This is a 4 wheeler Vehicle>

C++
// Example: #include using namespace std; class A {  protected:  int a;    public:  void set_A()  {  cout<<'Enter the Value of A=';  cin>>あ;    void disp_A() { cout<詳細については、記事を参照してください 多重継承 


3. 多レベルの継承 : このタイプの継承では、派生クラスは別の派生クラスから作成されます。

C++ におけるマルチレベルの継承

アンキタデイブ

構文:-

class C {  ... .. ...  }; class B:public C { ... .. ... }; class A: public B { ... ... ... };>
CPP
// C++ program to implement // Multilevel Inheritance #include  using namespace std; // base class class Vehicle { public:  Vehicle() { cout << 'This is a Vehicle
'; } }; // first sub_class derived from class vehicle class fourWheeler : public Vehicle { public:  fourWheeler()  {  cout << 'Objects with 4 wheels are vehicles
';  } }; // sub class derived from the derived base class fourWheeler class Car : public fourWheeler { public:  Car() { cout << 'Car has 4 Wheels
'; } }; // main function int main() {  // Creating object of sub class will  // invoke the constructor of base classes.  Car obj;  return 0; }>

出力
This is a Vehicle Objects with 4 wheels are vehicles Car has 4 Wheels>

4. 階層的な継承 : このタイプの継承では、複数のサブクラスが 1 つの基本クラスから継承されます。つまり、単一の基本クラスから複数の派生クラスが作成されます。

それ以外の場合は Java

C++ の階層継承

構文:-

class A  {   // body of the class A.  }  class B : public A  {   // body of class B.  }  class C : public A  {   // body of class C.  }  class D : public A  {   // body of class D.  }>
CPP
// C++ program to implement // Hierarchical Inheritance #include  using namespace std; // base class class Vehicle { public:  Vehicle() { cout << 'This is a Vehicle
'; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle { }; // main function int main() {  // Creating object of sub class will  // invoke the constructor of base class.  Car obj1;  Bus obj2;  return 0; }>

出力
This is a Vehicle This is a Vehicle>

5. ハイブリッド (仮想) 継承 : ハイブリッド継承は、複数の種類の継承を組み合わせて実装されます。例: 階層継承と多重継承の組み合わせ。
以下の図は、階層継承と複数の継承の組み合わせを示しています。

C++ のハイブリッド継承。

CPP
// C++ program for Hybrid Inheritance #include  using namespace std; // base class class Vehicle { public:  Vehicle() { cout << 'This is a Vehicle
'; } }; // base class class Fare { public:  Fare() { cout << 'Fare of Vehicle
'; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle, public Fare { }; // main function int main() {  // Creating object of sub class will  // invoke the constructor of base class.  Bus obj2;  return 0; }>

出力
This is a Vehicle Fare of Vehicle>
C++
// Example: #include   using namespace std;  class A  {   protected:   int a;   public:   void get_a()   {   cout << 'Enter the value of 'a' : ';   cin>>あ;   } };    クラス B : パブリック A { 保護: int b;   パブリック: void get_b() { cout<< 'Enter the value of 'b' : ';  cin>>b;   } };  クラス C { 保護: int c;   パブリック: void get_c() { cout<< 'Enter the value of c is : ';   cin>>c;   } };    クラス D : パブリック B、パブリック C {保護: int d;   public: void mul() { get_a();   get_b();   get_c();   コート<< 'Multiplication of a,b,c is : ' < 

6. ハイブリッド継承の特殊なケース: マルチパス継承 :
2 つの基本クラスを持ち、これら 2 つの基本クラスが 1 つの共通の基本クラスを持つ派生クラスは、マルチパス継承と呼ばれます。このタイプの継承ではあいまいさが生じる可能性があります。
例:

CPP
// C++ program demonstrating ambiguity in Multipath // Inheritance #include  using namespace std; class ClassA { public:  int a; }; class ClassB : public ClassA { public:  int b; }; class ClassC : public ClassA { public:  int c; }; class ClassD : public ClassB, public ClassC { public:  int d; }; int main() {  ClassD obj;  // obj.a = 10; // Statement 1, Error  // obj.a = 100; // Statement 2, Error  obj.ClassB::a = 10; // Statement 3  obj.ClassC::a = 100; // Statement 4  obj.b = 20;  obj.c = 30;  obj.d = 40;  cout << ' a from ClassB : ' << obj.ClassB::a;  cout << '
 a from ClassC : ' << obj.ClassC::a;  cout << '
 b : ' << obj.b;  cout << '
 c : ' << obj.c;  cout << '
 d : ' << obj.d << '
'; }>

出力
 a from ClassB : 10 a from ClassC : 100 b : 20 c : 30 d : 40>

上記の例では、ClassB と ClassC は両方とも ClassA を継承しており、どちらも ClassA のコピーを 1 つ持っています。ただし、クラス D はクラス B とクラス C の両方を継承するため、クラス D にはクラス A の 2 つのコピー (1 つはクラス B から、もう 1 つはクラス C から) があります。
Class-D のオブジェクトを介して ClassA のデータ メンバーにアクセスする必要がある場合、クラス B または ClassC からアクセスされるパスを指定する必要があります。bcoz コンパイラーは、クラス A の 2 つのコピーを区別できません。クラスD。

この曖昧さを回避するには 2 つの方法があります。

1) スコープ解決演算子を使用して曖昧さを回避します。 上記の例のステートメント 3 および 4 に示すように、スコープ解決演算子を使用すると、データ メンバー a へのアクセス元のパスを手動で指定できます。

CPP
obj.ClassB::a = 10; // Statement 3 obj.ClassC::a = 100; // Statement 4>


注記: それでも、クラス D にはクラス A のコピーが 2 つあります。
2) 仮想基本クラスを使用して曖昧さを回避します。

CPP
#include class ClassA {  public:  int a; }; class ClassB : virtual public ClassA {  public:  int b; }; class ClassC : virtual public ClassA {  public:  int c; }; class ClassD : public ClassB, public ClassC {  public:  int d; }; int main() {  ClassD obj;  obj.a = 10; // Statement 3  obj.a = 100; // Statement 4  obj.b = 20;  obj.c = 30;  obj.d = 40;  cout << '
 a : ' << obj.a;  cout << '
 b : ' << obj.b;  cout << '
 c : ' << obj.c;  cout << '
 d : ' << obj.d << '
'; }>

出力:

a : 100 b : 20 c : 30 d : 40>

上記の例によれば、クラス D にはクラス A のコピーが 1 つだけあるため、ステートメント 4 はステートメント 3 で指定された a の値を上書きします。