산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C++ 공개, 보호 및 비공개 상속

C++ 공개, 보호 및 비공개 상속

이 튜토리얼에서는 예제를 통해 C++에서 public, protected 및 private 상속을 사용하는 방법을 배웁니다.

C++ 상속에서 다른 액세스 모드의 기본 클래스에서 자식 클래스를 파생할 수 있습니다. 예를 들어,

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

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

키워드 public에 주목하세요. 코드에서

class Derived : public Base

즉, 공개 모드의 기본 클래스에서 파생된 클래스를 만들었습니다. . 또는 protected에서 클래스를 파생시킬 수도 있습니다. 또는 비공개 모드.

이 3개의 키워드(public , protected , 및 private )는 액세스 지정자로 알려져 있습니다. C++ 상속에서.

<시간>

C++의 공개, 보호 및 비공개 상속

공개 , 보호됨,비공개 상속에는 다음과 같은 기능이 있습니다.

참고: 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에서 :

아시다시피 private 멤버는 클래스 외부에서 직접 액세스할 수 없습니다. 결과적으로 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++ friend 함수 및 friend 클래스
  8. C++의 구조 및 클래스
  9. 사설 영역 네트워크는 Sigfox 공용 네트워크에 구축됩니다.
  10. 구조체와 클래스의 차이점:C++ 예제로 설명