--- id: cpp-encapsulation title: "C++ Encapsulation" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["getter setter", "private attribute access", "C++ μΊ‘μŠν™”"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "oop", "encapsulation"] raw_sources: ["https://www.w3schools.com/cpp/cpp_encapsulation.asp"] applied_in: [] github_commit: "" --- # [[CPP Encapsulation]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Encapsulation is the concrete APPLICATION of the previous chapter's abstract advice β€” "declare attributes private as often as you can" becomes actionable once you pair each `private` attribute with a `public` getter/setter method, meaning encapsulation isn't a separate new technique so much as the standard PATTERN for actually using `private` without making the data completely unreachable. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Encapsulation** β€” hiding "sensitive" data by declaring class attributes `private`, then providing `public` GET and SET methods for controlled access. [S1] - **Setter method** β€” a public method that ASSIGNS a value to a private attribute (`void setSalary(int s) { salary = s; }`). [S1] - **Getter method** β€” a public method that RETURNS a private attribute's value (`int getSalary() { return salary; }`). [S1] - **Real-life analogy** β€” an employee's salary is private (they can't change it directly); only the manager (a trusted method) can update or share it. [S1] - **Why encapsulate** β€” better control of data (change one part without affecting others), increased data security. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - A private attribute accessed exclusively through a getter/setter pair: `class Employee { private: int salary; public: void setSalary(int s) { salary = s; } int getSalary() { return salary; } }; Employee myObj; myObj.setSalary(50000); cout << myObj.getSalary();`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - ν˜„μž¬κΉŒμ§€ 발견된 λͺ¨μˆœ 사항 μ—†μŒ (No contradictions found in available sources). ## πŸ› οΈ 적용 사둀 (Applied in summary) μ§μ›μ˜ κΈ‰μ—¬(salary)λ₯Ό private으둜 숨기고 setSalary()/getSalary()둜만 μ ‘κ·Όν•˜λ„λ‘ ν•˜λŠ” Employee 클래슀 μ˜ˆμ œκ°€ μΊ‘μŠν™”μ˜ λŒ€ν‘œ μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ 원문에 직접 μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A private attribute accessed only through public getter/setter methods (C++): ```cpp class Employee { private: int salary; // hidden public: void setSalary(int s) { salary = s; } int getSalary() { return salary; } }; int main() { Employee myObj; myObj.setSalary(50000); cout << myObj.getSalary(); return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Access Specifiers]], [[CPP Inheritance]] - **μ°Έμ‘° λ§₯락:** μ ‘κ·Ό μ§€μ •μž 및 μΊ‘μŠν™” μ„Ήμ…˜ λ§ˆμ§€λ§‰ β€” 상속(Inheritance) μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Encapsulation β€” https://www.w3schools.com/cpp/cpp_encapsulation.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Encapsulation" page (Astra wiki-curation, P-Reinforce v3.1 format).