工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> C言語

C++ の公開、保護、非公開の継承

C++ の公開、保護、非公開の継承

このチュートリアルでは、例を使用して、C++ でパブリック、プロテクト、およびプライベートの継承を使用する方法を学習します。

C++ 継承では、さまざまなアクセス モードで基本クラスから子クラスを派生させることができます。たとえば、

class Base {
.... ... ....
};

class Derived : public Base {
.... ... ....
};

キーワード public に注意してください コード内

class Derived : public Base

これは、パブリック モードで基本クラスから派生クラスを作成したことを意味します。 .または、protected でクラスを派生させることもできます または非公開 モード。

これらの 3 つのキーワード (publicprotected 、および private ) は アクセス指定子 として知られています C++ 継承で。


C++ での public、protected、および private 継承

公開保護、非公開 継承には次の機能があります:

注: private 基本クラスのメンバーは、派生クラスからアクセスできません。

class Base {
    public:
        int x;
    protected:
        int y;
    private:
        int z;
};

class PublicDerived: public Base {
    // x is public
    // y is protected
    // z is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {
    // x is protected
    // y is protected
    // z is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {
    // x is private
    // y is private
    // z is not accessible from PrivateDerived
}

例 1:C++ パブリック継承

// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class PublicDerived : public Base {
   public:
    // function to access protected member from Base
    int getProt() {
        return prot;
    }
};

int main() {
    PublicDerived object1;
    cout << "Private = " << object1.getPVT() << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.pub << endl;
    return 0;
}

出力

Private = 1
Protected = 2
Public = 3

ここで、PublicDerived を導き出しました。 Base から 公開モードで .

その結果、PublicDerived で :

プライベート以来 そして保護 メンバーは main() からアクセスできません 、パブリック関数 getPVT() を作成する必要があります と getProt() それらにアクセスするには:

// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;

getPVT() 関数は Base 内で定義されています .しかし、getProt() 関数は PublicDerived 内で定義されています .

これは pvt のためです 、これは非公開です Base で 、PublicDerived にアクセスできません .

ただし、prot PublicDerived にアクセス可能です 公共の継承によるものです。つまり、getProt() PublicDerived 内から保護された変数にアクセスできます .


パブリック継承のアクセシビリティ

アクセシビリティ 非公開メンバー 保護されたメンバー 公開メンバー
基本クラス はい はい はい
派生クラス いいえ はい はい

例 2:C++ で保護された継承

// C++ program to demonstrate the working of protected inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class ProtectedDerived : protected Base {
   public:
    // function to access protected member from Base
    int getProt() {
        return prot;
    }

    // function to access public member from Base
    int getPub() {
        return pub;
    }
};

int main() {
    ProtectedDerived object1;
    cout << "Private cannot be accessed." << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.getPub() << endl;
    return 0;
}

出力

Private cannot be accessed.
Protected = 2
Public = 3

ここで、ProtectedDerived を導き出しました。 Base から 保護モードで .

その結果、ProtectedDerived で :

保護されている クラス外からメンバーに直接アクセスすることはできません。その結果、getPVT() は使用できません。 ProtectedDerived から .

getPub() を作成する必要があるのもそのためです。 ProtectedDerived の関数 pub にアクセスするには

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

保護された継承のアクセシビリティ

アクセシビリティ 非公開メンバー 保護されたメンバー 公開メンバー
基本クラス はい はい はい
派生クラス いいえ はい はい (保護変数として継承)

例 3:C++ のプライベート継承

// C++ program to demonstrate the working of private inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class PrivateDerived : private Base {
   public:
    // function to access protected member from Base
    int getProt() {
        return prot;
    }

    // function to access private member
    int getPub() {
        return pub;
    }
};

int main() {
    PrivateDerived object1;
    cout << "Private cannot be accessed." << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.getPub() << endl;
    return 0;
}

出力

Private cannot be accessed.
Protected = 2
Public = 3

ここで、PrivateDerived を導き出しました。 Base から プライベート モードで .

その結果、PrivateDerived で :

ご存知のように、クラスの外部からプライベート メンバーに直接アクセスすることはできません。その結果、getPVT() は使用できません。 PrivateDerived から .

getPub() を作成する必要があるのもそのためです。 PrivateDerived の関数 pub にアクセスするには

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

プライベート継承でのアクセシビリティ

アクセシビリティ 非公開メンバー 保護されたメンバー 公開メンバー
基本クラス はい はい はい
派生クラス いいえ はい (プライベート変数として継承) はい (プライベート変数として継承)

C言語

  1. パブリッククラウドとプライベートクラウドとハイブリッドクラウド
  2. パブリッククラウドの長所と短所
  3. プライベートクラウドの長所と短所
  4. C++ の変数、リテラル、および定数
  5. C++ クラスとオブジェクト
  6. C++ メモリ管理:新規および削除
  7. C++ フレンド関数とフレンド クラス
  8. C ++の構造とクラス
  9. プライベートエリアネットワークはSigfoxパブリックネットワーク上に構築されています
  10. C++ の演算子と例:型とプログラムとは
  11. 構造体とクラスの違い:C++ の例で説明