--- id: cpp-constructors title: "C++ Constructors" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["constructor rules", "same name as class", "no return type", "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", "constructors"] raw_sources: ["https://www.w3schools.com/cpp/cpp_constructors.asp"] applied_in: [] github_commit: "" --- # [[CPP Constructors]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A constructor breaks TWO rules every other function in C++ follows β€” it has NO return type at all, "not even void," and its name must be IDENTICAL to the class name β€” meaning a constructor isn't really "a special kind of function" so much as a fundamentally different syntactic category that happens to look function-shaped, distinguished purely by the compiler recognizing the name-matches-class pattern. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Constructor** β€” a special method AUTOMATICALLY called when an object is created; runs with zero explicit invocation from the programmer. [S1] - **Four constructor rules** β€” same name as the class; NO return type (not even `void`); usually declared `public`; automatically called on object creation. [S1] - **Constructor with parameters** β€” sets initial attribute values at creation time (`Car(string x, string y, int z) { brand = x; ... }`), so each object starts pre-configured. [S1] - **Defined outside the class** β€” same `::` scope resolution pattern as regular methods. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic parameterless constructor: `class MyClass { public: MyClass() { cout << "Hello World!"; } }; MyClass myObj; // triggers the constructor automatically`. [S1] - Constructor with parameters setting initial attributes: `class Car { public: string brand; string model; int year; Car(string x, string y, int z) { brand = x; model = y; year = z; } }; Car carObj1("BMW", "X5", 1999);`. [S1] - Defined outside the class via `::`: `Car(string x, string y, int z); // declaration Car::Car(string x, string y, int z) { brand = x; model = y; year = z; } // definition`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μƒμ„±μžλŠ” λ°˜ν™˜ νƒ€μž…μ΄ μ „ν˜€ μ—†μŒ**: voidμ‘°μ°¨ μ“°μ§€ μ•ŠλŠ”λ‹€λŠ” 점과 클래슀 이름과 μ™„μ „νžˆ 동일해야 ν•œλ‹€λŠ” 점이 λ‹€λ₯Έ λͺ¨λ“  ν•¨μˆ˜/λ©”μ„œλ“œμ™€ 근본적으둜 λ‹€λ₯Έ 두 κ°€μ§€ κ·œμΉ™μœΌλ‘œ λͺ…μ‹œλ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν”Όμž μ£Όλ¬Έ λΉ„μœ ("μƒμ„±μžλŠ” μ†ŒμŠ€Β·μΉ˜μ¦ˆΒ·ν† ν•‘μ„ 미리 μ–Ήμ–΄μ£ΌλŠ” μš”λ¦¬μ‚¬")κ°€ μƒμ„±μžκ°€ 객체 생성 μ‹œ μžλ™μœΌλ‘œ 초기 섀정을 λ§ˆμ³μ€€λ‹€λŠ” κ°œλ…μ„ μ§κ΄€μ μœΌλ‘œ μ„€λͺ…ν•˜λŠ” μ‹€μ „ λΉ„μœ λ‘œ 원문에 μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A constructor with parameters setting initial attribute values automatically (C++): ```cpp class Car { public: string brand; string model; int year; Car(string x, string y, int z) { // Constructor with parameters brand = x; model = y; year = z; } }; int main() { Car carObj1("BMW", "X5", 1999); // constructor runs automatically return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Class Methods]], [[CPP Constructors Overloading]], [[CPP Function Overloading]] - **μ°Έμ‘° λ§₯락:** μƒμ„±μž β€” μƒμ„±μž μ˜€λ²„λ‘œλ”©(Constructors Overloading) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Constructors β€” https://www.w3schools.com/cpp/cpp_constructors.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Constructors" page (Astra wiki-curation, P-Reinforce v3.1 format).