C++의 액세스 한정자

C++의 액세스 한정자

액세스 수정자는 다음과 같습니다. 특별한 키워드 C++에서는 클래스 멤버(데이터 및 함수)의 가시성을 제어합니다.
클래스의 특정 부분에 대한 액세스를 제한하거나 허용하여 데이터 숨기기를 구현하는 데 도움이 됩니다.

유형_of_액세스_수정자

하나씩 토론해보자

1. 공개 지정자

공개 지정자는 클래스 내부, 클래스 외부 또는 다른 파일 등 어디에서나 클래스 멤버(변수 및 함수)에 액세스할 수 있도록 합니다.

2. 개인 지정자

Private 지정자는 클래스 자체(주로 멤버 메서드) 내부에서만 클래스 멤버에 액세스할 수 있도록 하며 주로 외부에서 데이터를 숨겨 사용자의 코드를 변경하지 않고도 나중에 내부적으로 변경할 수 있도록 하는 데 사용됩니다. 기본적으로 C++의 모든 클래스 멤버는 지정자가 언급되지 않은 경우 비공개입니다.

CPP
   #include          using     namespace     std  ;   class     Employee     {   private  :         // salary and empId cannot be accessed       // from outside the Class      double     salary  ;      int     empID  ;   public  :         string     name  ;     // Name can be accessed anywhere          Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     'Name: '      < <     emp  .  name      < <     endl  ;      return     0  ;   }   

산출
Name: Fedrick  

비공개 멤버에 접근하려고 하면 어떻게 되나요?

코드는 아래에서 볼 수 있듯이 컴파일러 오류를 발생시킵니다.

C++
   #include          using     namespace     std  ;   class     Employee     {   private  :         double     salary  ;      int     empID  ;   public  :         string     name  ;      // Constructor      Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     emp  .  salary      < <     endl  ;         return     0  ;   }   

산출

 compilation error    
prog.cpp: In function ‘int main()’:
prog.cpp:22:17: error: ‘double Employee::salary’ is private within this context
cout < < emp.salary < < endl;
^~~~~~
prog.cpp:6:12: note: declared private here
double salary;
^~~~~~

3. 보호된 지정자

  • 보호된 지정자는 클래스 자체 내부와 클래스 내부에서 멤버에 액세스할 수 있도록 합니다. 파생된 (자식) 클래스이지만 외부 코드를 형성하지는 않습니다.
  • 이는 주로 상속에 사용되어 자식 클래스가 데이터와 기능을 외부 세계로부터 숨기면서 재사용하거나 수정할 수 있도록 합니다.
CPP
   #include          using     namespace     std  ;   class     Employee     {   private  :         double     salary  ;   protected  :         int     empID  ;   public  :         string     name  ;      Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   // Derived class (inherits from Employee)   class     Manager     :     public     Employee     {   public  :      Manager  (  string     n       double     s       int     id  )     :     Employee  (  n       s       id  )     {}      void     showDetails  ()     {      cout      < <     'Manager Name: '      < <     name      < <     endl  ;         cout      < <     'Manager ID: '      < <     empID      < <     endl  ;         }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     'Employee Name: '      < <     emp  .  name      < <     endl  ;      Manager     m  (  'Rohit'       70000       102  );      m  .  showDetails  ();         return     0  ;   }   

산출
Employee Name: Fedrick Manager Name: Rohit Manager ID: 102  


메모 : 상속을 통한 이 액세스는 파생 클래스에 있는 기본 클래스 요소의 액세스 한정자를 변경할 수 있습니다. 상속 모드 .

퀴즈 만들기