kang9366
Repository
kang9366
글쓰기
설정
전체 방문자
오늘
어제
  • 분류 전체보기 (143)
    • Programming (70)
      • Java (1)
      • C++ (11)
      • Kotlin (12)
      • Keras (2)
      • Android (34)
      • Jetpack Compose (1)
      • Error Solution (7)
    • CS (36)
      • 자료구조 (13)
      • 운영체제 (1)
      • 알고리즘 (4)
      • 컴퓨터 보안 (8)
      • 기타 (10)
    • Data Science (28)
      • 데이터분석 (7)
      • 머신러닝 (14)
      • 딥러닝 (7)

인기 글

최근 글

최근 댓글

Github · Instagram · Facebook
kang9366

Repository

Programming/C++

[C++] 접근제한자

2022. 9. 6. 07:54

public

public으로 선언된 데이터 멤버 및 멤버함수는 다른 클래스에서도 접근이 가능하다.

public 멤버는 . 연산자를 사용하여 프로그램의 아무곳에서나 접근 할 수 있다.

#include <iostream>
 
using namespace std;
 
class Circle {
public :
    double radius;
    double print_area() {
        return 3.14 * radius * radius;
    }
};
 
int main(void)
{
    Circle circle;
    circle.radius = 5;
 
    cout << "반지름 : " << circle.radius << "\n";
    cout << "원의 넓이 : " << circle.print_area() << "\n";
 
    return 0;
}

위 프로그램에서 데이터 멤버 radius는 공개(public)되어 있으므로 클래스 외부(main함수)에서 접근할 수 있다.

 

 

private

클래스 멤버를 private으로 선언하면 해당 멤버는 오직 클래스 내부에서 접근할 수 있다.

클래스 외부의 모든 객체나 함수에서 직접 접근할 수 없다.(getter, setter함수를 이용해 접근 가능)

클래스 내부의 멤버 함수 또는 friend 함수만 클래스의 private 멤버에 접근할 수 있다.

 

#include <iostream>
 
using namespace std;
 
class Circle {
private :
    double radius;
public :
    double print_area() {
        return 3.14 * radius * radius;
    }
};
 
int main(void)
{
    Circle circle;
    circle.radius = 5; // 접근 오류!!
 
    cout << "반지름 : " << circle.radius << "\n"; // 접근 오류!!
    cout << "원의 넓이 : " << circle.print_area() << "\n";
 
    return 0;
}

위 프로그램에서 radius에 직접 접근하면 에러가 발생한다.

 

#include <iostream>
 
using namespace std;
 
class Circle {
private :
    double radius;
public :
    void set_radius(double r) {
        radius = r; // 클래스 내부에서 접근 가능
    }
    double get_radius() {
        return radius; // 클래스 내부에서 접근 가능
    }
    double print_area() {
        return 3.14 * radius * radius;
    }
};
 
int main(void)
{
    Circle circle;
    circle.set_radius(5);
 
    cout << "반지름 : " << circle.get_radius() << "\n";
    cout << "원의 넓이 : " << circle.print_area() << "\n";
 
    return 0;
}

setter는 클래스 외부에서 직접 접근할 수 없는 private 멤버의 값을 초기화 해 줄 때 사용하며,

getter는 이 private 멤버의 값을 반환할 때 사용한다.

 

getter와 setter함수를 이용해 private 클래스의 멤버 변수에 접근이 가능하다.

 

protected

클래스 외부에서는 protected 멤버에 접근할 수 없지만 해당 클래스의 하위 클래스에서는 접근할 수 있다.

#include <iostream>
 
using namespace std;
 
class Parent {
protected :
    int id;
};
 
class Child : public Parent {
public :
    void set_id(int i) {
        // 클래스 외부에서 protected 멤버에 접근
        id = i;
    }
    void print_id() {
        cout << "ID : " << id << "\n";
    }
};
 
int main(void)
{
    Child child;
 
    child.set_id(10);
    child.print_id();
 
    return 0;
}

 

 

C++ 클래스 내에 멤버에 대한 접근 제한자를 두지 않으면 기본적으로 private이다. (자바는 public이 default)

C/C++ 구조체 멤버에 대한 기본적인 접근 제한자는 public이다.

저작자표시 비영리 변경금지

'Programming > C++' 카테고리의 다른 글

[C++] 클래스  (0) 2022.09.06
[C++] enum  (0) 2022.09.06
[C++] 구조체  (0) 2022.09.06
[C++] 포인터  (0) 2022.09.06
[C++] Template  (0) 2022.09.06
    'Programming/C++' 카테고리의 다른 글
    • [C++] 클래스
    • [C++] enum
    • [C++] 구조체
    • [C++] 포인터
    kang9366
    kang9366

    티스토리툴바