Skip to content

Latest commit

ย 

History

History

02. Using a Class

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

02. Using a Class

  • Contents
    • Preprocessor Wrappers
    • Member ํ•จ์ˆ˜์˜ scope
    • Class Scope and Accessing Class Members
    • Constructors with Default Arguments
    • Destructors
    • ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ์‹œ๊ธฐ
    • Default Memberwise Assignment
    • Copy Constructor
    • const Objects and const Member Functions
    • Composition: Objects as Members of Classes
    • friend Functions and friend Classes
    • Using this Pointer
    • Dynamic Memory Management with Operators new and delete
    • static Class Members

Preprocessor Wrappers

  • ์ฝ”๋“œ๊ฐ€ ๋‘๋ฒˆ ์ด์ƒ ํฌํ•จ(include)๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€
    • #ifndef XXX โ€“ โ€œif XXX is not defined yetโ€
      • : ๋งŒ์•ฝ XXX๊ฐ€ ์ด๋ฏธ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ด ์ฝ”๋“œ๋ฅผ ๊ฑด๋„ˆ ๋›ด๋‹ค,
    • #define XXX
      • : ์ด ์ฝ”๋“œ๊ฐ€ ๋‚˜์ค‘์— ๋‹ค์‹œ ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ๋ผ๋ฒจ(label) XXX์„ ์ •์˜ (define)
    • #endif : #ifndef ์™€ pair๋กœ ๊ตฌ์„ฑ๋จ
  • ๋งŒ์•ฝ ํ—ค๋”๊ฐ€ ์ด์ „์— ํฌํ•จ๋˜์—ˆ๋‹ค๋ฉด
    • ๋ผ๋ฒจ XXX์€ ์ด๋ฏธ ์ •์˜ ๋˜์—ˆ๊ณ , ๋”ฐ๋ผ์„œ ํ—ค๋”ํŒŒ์ผ์€ ๋” ์ด์ƒ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋‹ค์ค‘ ์ •์˜ (multiple definition) ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€
// prevent multiple inclusions of header file
#ifndef TIME_H
#define TIME_H

// Time class definition
class Time
{
public:
    Time(); // constructor
    void setTime(int, int, int); // set hour, minute and second
    void printuniversal(); // print time in universal-time format
    void printStandard(); // print time in standard-time format
private:
    int hour; // 0-23 (24-hour clock format)
    int minute; // 0 - 59
    int second; // 0 - 59
};

#endif // !TIME_H

Member ํ•จ์ˆ˜์˜ scope

  • ํด๋ž˜์Šค ์ •์˜ ๋‚ด์—์„œ ์„ ์–ธ๋œ(declared) ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์ •์˜(define)ํ–ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ์—ฌ์ „ํžˆ class scope์— ์กด์žฌํ•œ๋‹ค.
  • ๋‹ค์Œ์˜ ์—ฐ์‚ฐ์ž๋“ค์„ ํ†ตํ•ด์„œ ์ ‘๊ทผํ•˜์ง€ ์•Š๋Š” ์ด์ƒ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์•Œ๋ ค์ง€๊ฒŒ ๋œ๋‹ค.
    • ํด๋ž˜์Šค ๊ฐ์ฒด
    • ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ์ฐธ์กฐ ๋˜๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ํฌ์ธํ„ฐ
    • ์ดํ•ญ ์Šค์ฝ”ํ”„ ์‹๋ณ„ ์—ฐ์‚ฐ์ž (binary scope resolution operator)
      • ์ด๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•จ
      • void Time::setTime(...)

Class Scope and Accessing Class Members

ํด๋ž˜์Šค ์Šค์ฝ”ํ”„ (class scope)์™€ ํด๋ž˜์Šค ๋ฉค๋ฒ„๋กœ์˜ ์ ‘๊ทผ

  • ํด๋ž˜์Šค ์Šค์ฝ”ํ”„
    • ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„
      • ํด๋ž˜์Šค ์ •์˜ ๋‚ด์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜ (member variables)
    • ๋ฉค๋ฒ„ ํ•จ์ˆ˜
      • ํด๋ž˜์Šค ์ •์˜ ๋‚ด์— ์„ ์–ธ๋œ ํ•จ์ˆ˜ (member functions)
  • ํด๋ž˜์Šค ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜๋“ค์€ ํŒŒ์ผ ์Šค์ฝ”ํ”„ (file scope)์— ์ •์˜ ๋˜์–ด์•ผ ํ•œ๋‹ค.

ํด๋ž˜์Šค ์Šค์ฝ”ํ”„ (class scope)์™€ ํด๋ž˜์Šค ๋ฉค๋ฒ„๋กœ์˜ ์ ‘๊ทผ

  • ํด๋ž˜์Šค ์Šค์ฝ”ํ”„ ๋‚ด๋ถ€์—์„œ

    • ํด๋ž˜์Šค ๋ฉค๋ฒ„๋Š” ๋ชจ๋“  ๋ฉค๋ฒ„ ํ•จ์ˆ˜์— ์˜ํ•ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ํด๋ž˜์Šค ์Šค์ฝ”ํ”„ ์™ธ๋ถ€์—์„œ

    • public ํด๋ž˜์Šค ๋ฉค๋ฒ„๋Š” ๋‹ค์Œ ํ•ธ๋“ค(handle)์„ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค
      • ๊ฐ์ฒด ์ด๋ฆ„ (object name)
      • ๊ฐ์ฒด ์ฐธ์กฐ (reference to an object)
      • ๊ฐ์ฒด ํฌ์ธํ„ฐ (pointer to an object)
  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜

    • Block scope๋ฅผ ๊ฐ€์ง -> ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ด์•„์žˆ๋Š” ์ง€์—ญ ๋ณ€์ˆ˜ (local variable)
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ง€์—ญ ๋ณ€์ˆ˜๊ฐ€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์— ์ •์˜๋œ๋‹ค๋ฉด?

    • ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” :: ์ ‘๊ทผ์ž๋ฅผ ํ†ตํ•ด์„œ ์ ‘๊ทผํ•ด์•ผ ํ•จ
      • ์˜ˆ) Time::hour
      • ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ๊ทธ ๋ณ€์ˆ˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์˜๋ฏธํ•จ
  • ์  (dot) ๋ฉค๋ฒ„ ์„ ํƒ ์—ฐ์‚ฐ์ž (.)

    • ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ๊ฐ์ฒด์˜ ์ด๋ฆ„(name) ์ด๋‚˜ ๊ฐ์ฒด์˜ ์ฐธ์กฐ(reference)์— ์‚ฌ์šฉ
      • ์˜ˆ) sunset.printUniversal( );
  • ํ™”์‚ด (arrow) ๋ฉค๋ฒ„ ์„ ํƒ ์—ฐ์‚ฐ์ž (->)

    • ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ๊ฐ์ฒด์˜ ํฌ์ธํ„ฐ์— ์‚ฌ์šฉํ•œ๋‹ค.
      • ์˜ˆ) timePtr->printUniversal( )

Constructors with Default Arguments

์ƒ์„ฑ์ž์˜ ๋””ํดํŠธ ์ธ์ž (default arguments)

  • ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
    • ์‹ฌ์ง€์–ด ์ƒ์„ฑ์ž์˜ ์ธ์ž๊ฐ€ ์ฃผ์–ด์ง€์ง€ ์•Š์€ ๊ฒฝ์šฐ ํฌํ•จ
    • Time(int=0, int=0, int=0); // default constructor
  • ๋ชจ๋“  ์ธ์ž์— ๋””ํดํŠธ ์ธ์ž๊ฐ€ ์ง€์ •๋œ ์ƒ์„ฑ์ž๋Š” ์—ญ์‹œ ๋””ํดํŠธ ์ƒ์„ฑ์ž(default constructor)์ด๋‹ค.
    • ์–ด๋– ํ•œ ์ธ์ž๋„ ์—†์ด ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ํด๋ž˜์Šค๋‹น ์ตœ๋Œ€ ํ•œ ๊ฐœ์˜ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.
#pragma once
class Time {
public:
    Time();

    void setTime(int, int, int);
    void printUniversal();
    void printStandard();
private:
    int hour;
    int minute;
    int second;
};

Destructors

ํด๋ž˜์Šค ์†Œ๋ฉธ์ž (class destructors)

  • ํŠน๋ณ„ํ•œ ์šฉ๋„์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜
  • ์†Œ๋ฉธ์ž์˜ ์ด๋ฆ„: ~์— ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๋ถ™์ž„
    • ์˜ˆ) ~Time
  • ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ ๋  ๋•Œ ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ๋œ๋‹ค.
    • ์˜ˆ) ์ž๋™๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋œ ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋  ๋•Œ
    • ์ข…๋ฃŒ ์ •๋ฆฌ ์ž‘์—… (termination housekeeping)์„ ์ˆ˜ํ–‰
    • ์†Œ๋ฉธ์ž ์‹คํ–‰ ํ›„, ์‹œ์Šคํ…œ์ด ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšŒ์ˆ˜
      • ๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ๋Š” ์žฌ์‚ฌ์šฉ ๋  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์†Œ๋ฉธ์ž (destructors)

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€๋„ ์•Š๊ณ  ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋„ ์•Š๋Š”๋‹ค.
    • ์ƒ์„ฑ์ž๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•˜๊ณ , ์˜ค๋ฒ„๋กœ๋”ฉ๋„ ๊ฐ€๋Šฅํ•จ.
    • ๋ฐ˜ํ™˜ํ˜•์ด ์—†๋‹ค. (void ํ˜•๋„ ์•„๋‹˜)
  • ํด๋ž˜์Šค๋Š” ๋‹จ ํ•œ ๊ฐœ์˜ ์†Œ๋ฉธ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • ์†Œ๋ฉธ์ž ์˜ค๋ฒ„๋กœ๋”ฉ(overloading)์€ ํ—ˆ๋ฝ๋˜์ง€ ์•Š๋Š”๋‹ค.
      • ์• ์ดˆ์— ์˜ค๋ฒ„๋กœ๋”ฉ ํ•  ์ผ์ด ์—†๋‹ค.
  • ๋งŒ์•ฝ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์†Œ๋ฉธ์ž๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” โ€œ๋นˆ(empty)โ€ ์†Œ๋ฉธ์ž๋ฅผ ๋งŒ๋“ ๋‹ค.
    • ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์Œ

์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ์‹œ๊ธฐ

์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ ๋ฉ”์ปค๋‹ˆ์ฆ˜

  • ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์‹œ ๋‚ด๋ถ€์ ์œผ๋กœ (implicitly) ์ž๋™ ํ˜ธ์ถœ๋จ
  • ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ ์‹œ๊ธฐ์™€ ์ˆœ์„œ๋Š” ์–ด๋Š scope์—์„œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„
    • ์˜ˆ์ œ ์ฐธ์กฐ
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒ์„ฑ์ž์˜ ํ˜ธ์ถœ ์ˆœ์„œ์˜ ์—ญ์ˆœ์œผ๋กœ ์†Œ๋ฉธ์ž๊ฐ€ ํ˜ธ์ถœ๋จ

์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ ์‹œ๊ธฐ

๊ฐ์ฒด๊ฐ€ global scope์—์„œ ์ •์˜๋  ๋•Œ (์ฆ‰, ์ „์—ญ ๋ณ€์ˆ˜์ผ ๋•Œ)

  • ์ „์—ญ ๋ณ€์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ๋‚ด๋‚ด ์‚ด์•„์žˆ์Œ
  • main ํ•จ์ˆ˜๊ฐ€ ์‹œ์ž‘๋˜๊ธฐ ์ „์— ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ƒ์„ฑ์ž ํ˜ธ์ถœ
  • main ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜์–ด ์†Œ๋ฉธ์ž ํ˜ธ์ถœ

๊ฐ์ฒด๊ฐ€ ์ž๋™ ์ง€์—ญ ๋ณ€์ˆ˜ (local automatic variable) ์ผ ๋•Œ (: ๋ณดํ†ต main ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๋Š” ๊ฒƒ)

  • ๊ฐ์ฒด๊ฐ€ ์ •์˜๋  ๋•Œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ƒ์„ฑ์ž ํ˜ธ์ถœ
  • ๊ฐ์ฒด๊ฐ€ ํฌํ•จ๋œ scope๋ฅผ ๋ฒ—์–ด๋‚  ๋•Œ ์†Œ๋ฉธ๋˜์–ด ์†Œ๋ฉธ์ž ํ˜ธ์ถœ

๊ฐ์ฒด๊ฐ€ ์ •์  ์ง€์—ญ ๋ณ€์ˆ˜ (static local variable) ์ผ ๋•Œ

  • Static ๋ณ€์ˆ˜๋Š” ๋‹จ ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜์–ด ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ๋‚ด๋‚ด ์‚ด์•„์žˆ๋Š” ๋ณ€์ˆ˜์ด๋‹ค
  • ๋”ฐ๋ผ์„œ ์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๊ฐ€ ์ฒ˜์Œ ์ƒ์„ฑ๋  ๋•Œ ๋‹จ ํ•œ๋ฒˆ ํ˜ธ์ถœ
  • main ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜์–ด ์†Œ๋ฉธ์ž ํ˜ธ์ถœ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-10-21 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 2 29 22

> global๊ณผ static์€ ์œ„์น˜๊ฐ€ ๊ฐ™์•„ ํŠน์„ฑ์ด "๋น„์Šท"

์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž์˜ ํ˜ธ์ถœ ์‹œ๊ธฐ ์˜ˆ์ œ

CreatAndDestroy.h
#pragma once
#include <string>
using std::string;

class CreateAndDestory {
public:
    CreateAndDestory(int, string);
    ~CreateAndDestory();

private:
    int objectID;
    string message;
};
CreatAndDestroy.cpp
#include <iostream>
using std::cout;
using std::endl;

#include "Create.h"

void create(void); // prototype

CreateAndDestory Temp(2, "Temp");
CreateAndDestory first(1, "(global before main)"); // global object
int main()
{
    cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
    CreateAndDestory second(2, "(local automatic in main)");
    static CreateAndDestory third(3, "(local static in main)");

    create(); // call function to create objects
    cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
    CreateAndDestory fourth(4, "(local automatic in main)");
    cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;
    return 0;
}

void create(void)
{
    cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl;
    CreateAndDestory fifth(5, "(local automatic in create)");
    static CreateAndDestory sixth(6, "(local static in create)");
    CreateAndDestory seventh(7, "(local automatic in create)");
    cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl;
}
driver
#include <iostream>
using std::cout;
using std::endl;

#include "Create.h"

CreateAndDestory::CreateAndDestory(int ID, string msg)
{
    objectID = ID;
    message = msg;

    cout << "Object " << objectID << "    constructor runs    " << message << endl;
}
CreateAndDestory::~CreateAndDestory()
{
    // output newline for certain objects; helps readability
    cout << (objectID == 1 || objectID == 6 ? "\n" : "");

    cout << "Object " << objectID << "    destructor runs    "
         << message << endl;
}
OUTPUT
1   constructor runs    (global before main)

MAIN FUNCTION: EXECUTION BEGINS
2   constructor runs    (local automatic in main)
3   constructor runs    (local static in main)

CREATE FUNCTION: EXECUTION BEGINS
5   constructor runs    (local automatic in create)
6   constructor runs    (local static in create)
7   constructor runs    (local automatic in create)

CREATE FUNCTION: EXECUTION ENDS
7   destructor runs    (local automatic in create)
5   destructor runs    (local automatic in create)

MAIN FUNCTION: EXECUTION BEGINS
4   constructor runs    (local automatic in main)

MAIN FUNCTION: EXECUTION ENDS
4   destructor runs    (local automatic in create)
2   destructor runs    (local automatic in create)
6   destructor runs    (local static in create)
3   destructor runs    (local static in main)
1   destructor runs    (global before main)

Default Memberwise Assignment

๋””ํดํŠธ ๋ฉค๋ฒ„๋ณ„ (memberwise) ๋Œ€์ž…

  • ๋Œ€์ž… ์—ฐ์‚ฐ์ž (=)
    • ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋™์ผํ•œ ๋‹ค๋ฅธ ๊ฐ์ฒด์— ๋Œ€์ž…ํ•  ๋•Œ ์‚ฌ์šฉ
      • ๋Œ€์ž… ์—ฐ์‚ฐ์ž์˜ ์˜ค๋ฅธํŽธ์— ์žˆ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋Š” ๊ฐœ๋ณ„์ ์œผ๋กœ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ์™ผํŽธ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์— 1:1 ๋Œ€์ž… ๋œ๋‹ค.
    • ๋ฉค๋ฒ„๋ณ„ ๋Œ€์ž…์€ ๋™์ ์œผ๋กœ ํ• ๋‹น๋œ ํฌ์ธํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์— ๋Œ€ํ•ด์„œ๋Š” ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋ฅผ ์œ ๋ฐœ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜
      • ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‚˜์ค‘์— ๋‘ ๋ฒˆ ํ•ด์ œํ•  ์ˆ˜ ์žˆ์Œ

Copy Constructor

๋ณต์‚ฌ ์ƒ์„ฑ์ž (copy constructor)

  • ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ๊ธฐ์กด ๊ฐ์ฒด์˜ ๊ฐ’์— ์˜ํ•œ ์ „๋‹ฌ
    • ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
    • ์›๋ž˜ ๊ฐ์ฒด์˜ ๊ฐ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด์˜ ๋Œ€์‘๋˜๋Š” ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋กœ ๋ณต์‚ฌํ•œ๋‹ค. (์ฆ‰, ๋ฉค๋ฒ„๋ณ„ ๋Œ€์ž…์„ ์ˆ˜ํ–‰)
  • ๋™์  ํ• ๋‹น๋œ ํฌ์ธํ„ฐ๋ฅผ ํฌํ•จํ•œ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์— ๋Œ€ํ•ด์„œ๋Š” ์—ญ์‹œ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋ฅผ ์œ ๋ฐœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Shallow Copy vs. Deep Copy

  • ์–•์€ ๋ณต์‚ฌ(Shallow Copy)์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ–ˆ์„ ๋•Œ ๋ณ€์ˆ˜๊ฐ€ ๋”ฐ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์žกํžˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ณต์‚ฌํ•œ ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.
  • ๋งŒ์ผ ๋™์ ํ• ๋‹น์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ•œ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

const Objects and const Member Functions

์ƒ์ˆ˜ (const)

  • ์ตœ์†Œ ํŠน๊ถŒ ์›๋ฆฌ (Principle of least privilege)
    • ๊ฐ ์†Œํ”„ํŠธ์›จ์–ด ๋ชจ๋“ˆ(๊ฐ์ฒด, ํ•จ์ˆ˜, โ€ฆ)์—๋Š” ๊ทธ๋“ค์—๊ฒŒ ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๊ถŒํ•œ๋งŒ์„ ๋ถ€์—ฌํ•œ๋‹ค.
    • ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„์˜ ๊ธฐ๋ณธ์ ์ธ ์›์น™ ์ค‘ ํ•˜๋‚˜
    • ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์—๋„ ์ ์šฉ๋จ
  • const ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉ
    • ์ˆ˜์ •์„ ์‹œ๋„ํ•˜๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
const a = 10;
a = 20;         // error!

์ƒ์ˆ˜ ๊ฐ์ฒด (constant objects)

  • ์ตœ์†Œ ํŠน๊ถŒ ์›๋ฆฌ (Principle of least privilege)
  • ๊ฐ ์†Œํ”„ํŠธ์›จ์–ด ๋ชจ๋“ˆ(๊ฐ์ฒด, ํ•จ์ˆ˜, โ€ฆ)์—๋Š” ๊ทธ๋“ค์—๊ฒŒ ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๊ถŒํ•œ๋งŒ์„ ๋ถ€์—ฌํ•œ๋‹ค.
  • ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„์˜ ๊ธฐ๋ณธ์ ์ธ ์›์น™ ์ค‘ ํ•˜๋‚˜
  • ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์—๋„ ์ ์šฉ๋จ
  • ์ƒ์ˆ˜ ๊ฐ์ฒด (constant objects)
    • const ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉ
    • ๊ฐ์ฒด๋ฅผ ์ˆ˜์ • (์ฆ‰ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ˆ˜์ •) ํ•  ์ˆ˜ ์—†์Œ์„ ์˜๋ฏธ
      • ์ˆ˜์ •์„ ์‹œ๋„ํ•˜๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

const ๋ฉค๋ฒ„ ํ•จ์ˆ˜

  • ์ƒ์ˆ˜ ๊ฐ์ฒด๋Š” const ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋งŒ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ
  • const๋กœ ์„ ์–ธ๋œ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Œ
  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ์ •์˜์— ๋ชจ๋‘ const ํ˜•์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•จ
  • ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž๋Š” const ํ˜•์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์—†์Œ

Const ์œ„์น˜์— ๋”ฐ๋ฅธ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์˜ ์˜๋ฏธ

  1. ํ•จ์ˆ˜๋ช… ๋’ค์˜ 'const'๊ฐ€ ๊ฐ€์ง€๋Š” ์˜๋ฏธ ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ์ฝ๊ธฐ์ „์šฉ(RDONLY)์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ํ‘œ์‹œ์ด๋‹ค.
    ์ฆ‰, '์ฝ๊ธฐ'๋งŒ ํ• ๋ฟ '์“ฐ๊ธฐ'๋Š” ํ•˜์ง€์•Š๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  2. ์œ„์˜ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ const๊ฐ€ ๊ฐ€์ง€๋Š” ์˜๋ฏธ 'call by reference'๋กœ ๋ณต์‚ฌ ์˜ค๋ฒ„ํ—ค๋“œ์—†์ด ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฝ๊ธฐ์ „์šฉ(RDONLY)์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ํ‘œ์‹œ์ด๋‹ค.

  3. ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ํƒ€์ž… ์•ž์˜ 'const'๊ฐ€ ๊ฐ€์ง€๋Š” ์˜๋ฏธ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ฝ๊ธฐ์ „์šฉ(RDONLY)์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ํ‘œ์‹œ์ด๋‹ค.
    ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ right-hand-side์— ํ•ด๋‹นํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ด ๊ฐ’์„ &&(r-value ์ฐธ์กฐ)๋กœ ๋ฐ›์•„๋ฒ„๋ฆฌ๋ฉด ์ˆ˜์ •ํ•  ์—ฌ์ง€๊ฐ€ ์ƒ๊ธด๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ€๋Šฅ์„ฑ์„ ์ œ๊ฑฐํ•˜๊ณ  'ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๋Œ€์ž…์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•˜๋ผ'๋Š” ์˜๋ฏธ๋ผ ์ƒ๊ฐ๋œ๋‹ค.

Const ์œ„์น˜์— ๋”ฐ๋ฅธ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์˜ ์˜๋ฏธ

  1. ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ ๋ชปํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•œ๋‹ค.

const char* c_ptr;

char s1[] = "hello";
char s2[] = "bonjour";

const char* c_ptr = s1;
c_ptr = s2;       // ์ฃผ์†Œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
c_ptr[0] = 'a';   // ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
  • ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  • ํ•˜์ง€๋งŒ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ(์ฃผ์†Œ๊ฐ’)์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ(์ฃผ์†Œ)์„ ๋ณ€๊ฒฝ ๋ชปํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•œ๋‹ค.

char* const ptr;

char s1[] = "hello";
char s2[] = "bonjour";

char* const c_ptr = s1;
c_ptr = s2;     // ์ฃผ์†Œ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
c_ptr[0] = 'a'; // ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
  • ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ(์ฃผ์†Œ)์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  • ํ•˜์ง€๋งŒ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. ๋Œ€์ƒ๊ณผ ๋Œ€์ƒ์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•œ๋‹ค.

const char* const c_ptr;

char s1[] = "hello";
char s2[] = "bonjour";

const char* const c_ptr = s1;
c_ptr = s2;     // ์ฃผ์†Œ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
c_ptr[0] = 'a'; // ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
  • ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ๊ณผ ๊ทธ ๊ฐ’์„ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

Member Initializer

  • ๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”๊ธฐ (Member initializer)

    • ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋Š” member initializer๋ฅผ ์ด์šฉํ•˜์—ฌ ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
    • const ํ˜• ๋˜๋Š” ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋Š” ๋ฐ˜๋“œ์‹œ member initializer๋ฅผ ์ด์šฉํ•˜์—ฌ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•จ
  • ๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”๊ธฐ ๋ชฉ๋ก (Member initializer list)

    • ํด๋ž˜์Šค ์ƒ์„ฑ์ž์˜ ์ธ์ž ๋ฆฌ์ŠคํŠธ์™€ ํ•จ์ˆ˜ ์‹œ์ž‘์˜ { ์‚ฌ์ด์— ์œ„์น˜
    • ๋ณต์ˆ˜์˜ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ์Œ
    • ํด๋ž˜์Šค ์ƒ์„ฑ์ž๊ฐ€ ์ˆ˜ํ–‰๋˜๊ธฐ ์ง์ „์— ์ˆ˜ํ–‰
    • ๊ตฌ์ฒด์ ์ธ ์„œ์‹์€ ๋‹ค์Œ ์˜ˆ์ œ ์ฐธ์กฐ
  • ์ด๋‹ˆ์…œ๋ผ์ด์ €์˜ ์‹คํ–‰์„ ํฌํ•จํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์˜ ๊ณผ์ •

    • 1๋‹จ๊ณ„ : ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ํ• ๋‹น
    • 2๋‹จ๊ณ„ : ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์ด์šฉํ•œ ๋ฉค๋ฒ„๋ณ€์ˆ˜(๊ฐ์ฒด)์˜ ์ดˆ๊ธฐํ™”
    • 3๋‹จ๊ณ„ : ์ƒ์„ฑ์ž์˜ ๋ชธ์ฒด ๋ถ€๋ถ„ ์‹คํ–‰
  • ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉค๋ฒ„๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”.

    • const๋กœ ์„ ์–ธ๋œ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋„ ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅ.
      • ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™” ๋˜๋Š” ํ˜•ํƒœ์ด๋ฏ€๋กœ...

Composition: Objects as Members of Classes

๋ณตํ•ฉ (Composition)

  • has-a ๊ด€๊ณ„๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•จ
  • ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ์‹œ
    • AlarmClock ๊ฐ์ฒด๋Š” Time์˜ ๊ฐ์ฒด๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ€์ง„๋‹ค.

๋ณตํ•ฉ ๊ด€๊ณ„์—์„œ ๋ฉค๋ฒ„ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”

  • ๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”๊ธฐ(member initializers)์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ์ž์˜ ์ธ์ž๋ฅผ ํ†ตํ•ด ๋ฉค๋ฒ„ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž์—๊ฒŒ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌ
  • ๋ฉค๋ฒ„ ๊ฐ์ฒด๋Š” ํด๋ž˜์Šค ์ •์˜์— ์„ ์–ธ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ƒ์„ฑ๋จ
  • ๋งŒ์•ฝ ๋ฉค๋ฒ„ ์ดˆ๊ธฐํ™”๊ธฐ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
    • ๋ฉค๋ฒ„ ๊ฐ์ฒด์˜ ๋””ํดํŠธ ์ƒ์„ฑ์ž๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.

Composition ์˜ˆ์ œ

Date.h

#pragma once

class Date {
public:
    Date(int = 1, int = 1, int = 1900);
    Date(int, int);
    void print()const;
    ~Date();

private:
    int month;
    int day;
    int year;

    int checkDay(int)const;
};

Date.cpp

#include <iostream>
using std::cout;
using std::endl;

#include "Date.h"

Date::Date(int mn, int dy, int yr) {
    if (mn > 0 && mn <= 12) {
        month = mn;
    }
    else {
        month=1;
        cout << "Invalid month (" << mn << ") set to 1.\n";
    }

    year = yr;
    day = checkDay(dy);

    cout << "Date object constructor fordate ";
    print();
    cout << endl;
}
void Date::print() const {
    cout << month << '/' << day << '/' << year;
}

Date::~Date() {
    cout << "Date object destructor for date ";
    print();
    cout << endl;
}

int Date::checkDay(int testDay) const {
    static const int daysPerMonth[13] = {
        0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (testDay > 0 && testDay <= daysPerMonth[month])
        return testDay;

    if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 &&
        year % 100 != 0)))
        return testDay;

    cout << "Invalid day(" << testDay << ") set to 1.\n";
    return 1;
}

Employee.h

#pragma once
#include "Date.h"
class Employee {
public:
    Employee(const char* const, const char* const,
        const Date&, const Date&);
    void print() const;
    ~Employee();

private:
    char firstName[25];
    char lastName[25];
    const Date birthDate;
    const Date hireDate;
};

Employee.cpp

#include <iostream>
using std::cout;
using std::endl;

#include <cstring>
using std::strlen;
using std::strncpy;

#include "Employee.h"
#include "Date.h"

Employee::Employee(const char *const first, const char *const last,
                   const Date &dateOfBirth, const Date &dateOfHire) : birthDate(dateOfBirth),
                                                                      hireDate(dateOfHire)
{
    int length = strlen(first);
    length = (length < 25 ? length : 24);
    strncpy(firstName, first, length);
    firstName[length] = '\0';
    length = strlen(last);
    length = (length < 25 ? length : 24);
    strncpy(lastName, last, length);
    lastName[length] = '\0';

    cout << "Employee object constructor: " << firstName << ' ' << lastName << endl;
}

void Employee::print() const
{
    cout << lastName << ", " << firstName << " Hired: ";
    hireDate.print();
    cout << " Birthday: ";
    birthDate.print();
    cout << endl;
}

Employee::~Employee()
{
    cout << "Employee object destructor: " << lastName << ", " << firstName << endl;
}

driver

#include <iostream>
using std::cout;
using std::endl;

#include "Employee.h"

int main() {
    Date birth(7, 24, 1949);
    Date hire(3, 12, 1988);
    Date birth2();

    Employee manager("Bob", "Blue", birth, hire);

    cout << endl;
    manager.print();

    cout << "\n Test Date constructor with invalid values:\n";
    Date lastDayoff(14, 35, 1994);
    cout << endl;
    return 0;
}

์‹คํ–‰ ๊ฒฐ๊ณผ

image

friend Functions and friend Classes

ํด๋ž˜์Šค์˜ friend function / friend class

  • Class scope์˜ ์™ธ๋ถ€์— ์ •์˜๋จ
  • Class์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์•„๋‹˜
    • ๊ทธ๋Ÿฌ๋‚˜ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ
  • public ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹Œ private ๋ฉค๋ฒ„์—๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋…๋ฆฝ์ ์ธ ํ•จ์ˆ˜๋‚˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ค ํด๋ž˜์Šค์˜ friend๋กœ ์„ ์–ธ๋  ์ˆ˜ ์žˆ์Œ
    • Friend ํด๋ž˜์Šค ๊ฐ„์˜ ์ง์ ‘์ ์ธ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ˆ˜ํ–‰ ์†๋„ ํ–ฅ์ƒ
    • ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋งŒ์œผ๋กœ๋Š” ์ˆ˜ํ–‰ํ•˜๊ธฐ ํž˜๋“  ๋™์ž‘์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ
  • ํด๋ž˜์Šค์˜ friend ํ•จ์ˆ˜ ์„ ์–ธ
    • ํด๋ž˜์Šค ์ •์˜์— friend๋กœ ์‹œ์ž‘๋˜๋Š” ํ•จ์ˆ˜ ์›ํ˜• ์„ ์–ธ
  • ํด๋ž˜์Šค์˜ friend ํด๋ž˜์Šค ์„ ์–ธ
    • ClassTwo ํด๋ž˜์Šค๋ฅผ ClassOne ํด๋ž˜์Šค์˜ friend ์„ ์–ธ
      • friend class ClassTwo; ๋ฅผ ClassOne ํด๋ž˜์Šค ์ •์˜์— ์ถ”๊ฐ€
      • ClassTwo ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ClassOne ํด๋ž˜์Šค์˜ friend๊ฐ€ ๋จ

Friendship ๊ด€๊ณ„์˜ ํŠน์„ฑ

  • ์นœ๊ตฌ ๊ด€๊ณ„ (Friendship relation) ๋Š” ํ—ˆ์šฉ๋˜๋Š” ๊ฒƒ (์ทจ๋“ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜)
    • Class B ๊ฐ€ Class A์˜ friend๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ, class A๋Š” class B๋ฅผ friend๋กœ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•จ (A๊ฐ€ B๋ฅผ ํ—ˆ์šฉ)
  • Friendship relation์€ ์ผ๋ฐฉ์ ์ด๊ณ , ๋˜ํ•œ ์ „์ด๋˜์ง€ ์•Š์Œ
    • Class A๊ฐ€ class B์˜ friend์—ฌ๋„, class B๊ฐ€ class A์˜ friend๊ฐ€ ๋˜์ง€ ์•Š์Œ
      • ์ฆ‰, ์ง์‚ฌ๋ž‘ ๊ด€๊ณ„
    • Class A๊ฐ€ class B์˜ friend์ด๊ณ , class B๊ฐ€ class C์˜ friend์—ฌ, class A๊ฐ€ class C์˜ friend๊ฐ€ ๋˜์ง€ ์•Š์Œ
      • ์ฆ‰, ์นœ๊ตฌ์˜ ์นœ๊ตฌ๋Š” ์ €์ ˆ๋กœ ์นœ๊ตฌ๊ฐ€ ์•„๋‹˜

Using this Pointer

This ํฌ์ธํ„ฐ๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

  • This ํฌ์ธํ„ฐ
    • ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ (self-reference)
  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” this pointer๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ์•ˆ๋‹ค.
    • ๋ชจ๋“  ๊ฐ์ฒด๋Š” C++์˜ ํ‚ค์›Œ๋“œ์ธ this ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ์ž์‹ ์˜ ์ฃผ์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ฐ์ฒด์˜ this ํฌ์ธํ„ฐ๋Š” ๊ฐ์ฒด ์ž์‹ ์˜ ์ผ๋ถ€๋Š” ์•„๋‹˜
  • ๊ฐ์ฒด๋Š” this pointer๋ฅผ ์•”์‹œ์ ์œผ๋กœ (implicitly) ๋˜๋Š” ๋ช…์‹œ์ ์œผ๋กœ (explicitly) ์‚ฌ์šฉ
    • ๋ฉค๋ฒ„ ํ•จ์ˆ˜์—์„œ ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•  ๋•Œ ์•”์‹œ์ ์œผ๋กœ ์‚ฌ์šฉ
    • this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉ
    • this ํฌ์ธํ„ฐ์˜ ํƒ€์ž…์€ ๊ฐ์ฒด ํƒ€์ž…์— ์˜ํ•ด ๊ฒฐ์ •๋จ

Dynamic Memory Management with Operators new and delete

๋™์  ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ(Dynamic Memory Management)

  • ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น (allocation) ๋ฐ ํ•ด์ œ (release)

    • ๋‚ด์žฅ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ ์‚ฌ์šฉ์ž ์ •์˜ ํƒ€์ž…์— ๋Œ€ํ•ด ๋™์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ  ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
    • new ์™€ delete ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด ๋Œ€์‹  ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ์ค‘ ํฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •๋˜๋Š” (= ๋™์ ์ธ) ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ƒ์„ฑ
  • Heap

    • ๊ฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‹คํ–‰ ์‹œ๊ฐ„์— ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด๋‹ค.
    • ๋ฐ˜๋Œ€๋กœ, ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋Š” stack ์˜์—ญ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ๋ฐ›์Œ.
  • ์—ฐ์‚ฐ์ž new

    • ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… (int, double ๋“ฑ) ์ด๋‚˜ ํด๋ž˜์Šค ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์‹คํ–‰์‹œ๊ฐ„ (execution time)์— ํ• ๋‹น ๋ฐ ์ƒ์„ฑ
      • ์˜ˆ์‹œ
        • Time *ptrTime = new Time;
        • float *ptrNum = new float;
      • ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ๋™์‹œ์— ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž๋„ ํ˜ธ์ถœ
      • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ณต์ˆ˜์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ”ํžˆ ์‚ฌ์šฉํ•จ
    • new์˜ ์˜ค๋ฅธ์ชฝ์— ์ง€์ •๋œ ํƒ€์ž…์˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
    • C์–ธ์–ด์—์„œ์˜ malloc()๊ณผ ์œ ์‚ฌ
  • ์—ฐ์‚ฐ์ž delete

    • ๋™์ ์œผ๋กœ ํ• ๋‹น๋œ ๊ฐ์ฒด๋ฅผ ์†Œ๋ฉธ์‹œํ‚ด
      • ์ด๋•Œ, ๊ฐ์ฒด์˜ ์†Œ๋ฉธ์ž๋ฅผ ํ˜ธ์ถœ
        • ์˜ˆ์‹œ
        • delete ptrTime;
        • delete ptrNum;
    • ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ•ด์ œ(release)ํ•œ๋‹ค.
      • ํ•ด์ œ๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์€ ๋‹ค๋ฅธ ๊ฐ์ฒด์— ํ• ๋‹น๋˜๊ธฐ ์œ„ํ•ด ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • C์–ธ์–ด์—์„œ์˜ free()๊ณผ ์œ ์‚ฌ
  • new๋ฅผ ์ด์šฉํ•œ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”

    • ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋œ ๊ธฐ๋ณธ์ ์ธ ํƒ€์ž…์˜ ๊ฐ์ฒด์— ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
      • ์˜ˆ์‹œ
        • double *ptr = new double(3.14159);
    • ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž์— ์‰ผํ‘œ๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ์ธ์ˆ˜ ๋ชฉ๋ก์„ ์ง€์ •
      • ์˜ˆ์‹œ
        • Time *timePtr = new Time(12,45,0);
  • new ์—ฐ์‚ฐ์ž๋Š” ๋™์ ์œผ๋กœ ๋ฐฐ์—ด์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค

    • 10๊ฐœ์˜ ์ •์ˆ˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ํ• ๋‹น
      • ์˜ˆ์‹œ
        • int *gradesArray = new int[10];
    • ๋™์  ํ• ๋‹น ๋ฐฐ์—ด์˜ ํฌ๊ธฐ
      • ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์ •์ˆ˜ ๋ณ€์ˆ˜๋‚˜ ์ •์ˆ˜ ๋ณ€์ˆ˜์˜ ์ˆ˜์‹ ํ‘œํ˜„์„ ํ†ตํ•ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Œ
        • int *gradesArray = new int[count1+count2];
  • ๋™์ ์œผ๋กœ ํ• ๋‹น๋œ ๋ฐฐ์—ด์„ ์ œ๊ฑฐ

    • delete [] gradesArray;
      • ์œ„ ๋ฌธ์žฅ์€ gradesArray๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฐ์—ด์— ๋Œ€ํ•œ ํ• ๋‹น์„ ํ•ด์ œํ•œ๋‹ค.
      • ์œ„ ๋ฌธ์žฅ์˜ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค๋ฉด,
        • ์œ„ ๋ฌธ์žฅ์€ ๋จผ์ € ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ๊ฐ์ฒด์— ๋Œ€ํ•œ ์†Œ๋ฉธ์ž๋ฅผ ํ˜ธ์ถœํ•œ ํ›„, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•œ๋‹ค.
      • ์œ„ ๋ฌธ์žฅ์—์„œ ๋Œ€๊ด„ํ˜ธ([])๊ฐ€ ์—†๊ณ  gradesArray๊ฐ€ ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.
        • ๋ฐฐ์—ด ๋‚ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ์ฒด๋งŒ ์†Œ๋ฉธ์ž ํ˜ธ์ถœ์„ ๋ฐ›์€ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
        • ๋™์  ํ• ๋‹น ๋ฐ›์€ ๋ฐฐ์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์‹œ []๋ฅผ ๋ˆ„๋ฝํ•˜์ง€ ์•Š๋„๋ก ์กฐ์‹ฌ.

static Class Members

  • Static ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„

    • ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๋ณต์‚ฌ๋ณธ
      • ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€ ๋ณ€์ˆ˜๋ฅผ ๊ณต์œ  : โ€œClass-wideโ€ ์ •๋ณด
      • ํด๋ž˜์Šค์˜ ํŠน์ •ํ•œ ๊ฐ์ฒด์˜ ์†์„ฑ์ด ์•„๋‹Œ ํด๋ž˜์Šค ์ž์ฒด์˜ ์†์„ฑ์ž„
    • ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์˜ ์„ ์–ธ ์‹œ static ํ‚ค์›Œ๋“œ๋กœ ์‹œ์ž‘
    • ๋น„๋ก ์ „์—ญ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋ณด์ด๋‚˜, ์‹ค์ œ๋กœ๋Š” class scope๋ฅผ ๊ฐ€์ง
    • public, private or protected ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„ ๋ชจ๋‘์— static ์„ ์„ ์–ธ๋  ์ˆ˜ ์žˆ๋‹ค.
      • private, protected์ธ ๊ฒฝ์šฐ public static member function์„ ํ†ตํ•ด ์ ‘๊ทผ
  • Public static ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„

    • ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•„๋„ ์กด์žฌ (ํด๋ž˜์Šค ์ž์ฒด์˜ ์†์„ฑ)
      • ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ public static ํด๋ž˜์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด
        • ํด๋ž˜์Šค์˜ ์ด๋ฆ„๊ณผ ์ดํ•ญ ์Šค์ฝ”ํ”„ ์‹๋ณ„ ์—ฐ์‚ฐ์ž(::)๋ฅผ ์‚ฌ์šฉ
        • ์˜ˆ์‹œ โ€“ Martian::martianCount
    • ํด๋ž˜์Šค์˜ ์–ด๋– ํ•œ ๊ฐ์ฒด์—์„œ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
      • ๊ฐ์ฒด์˜ ์ด๋ฆ„๊ณผ ์  ์—ฐ์‚ฐ์ž(dot operator)(.)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
        • ์˜ˆ์‹œ โ€“ myMartian.martianCount
  • Static ๋ฉค๋ฒ„ ํ•จ์ˆ˜

    • ํด๋ž˜์Šค์˜ ํŠน์ • ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ์ž์ฒด์˜ ์„œ๋น„์Šค
    • static์ด ์•„๋‹Œ ํด๋ž˜์Šค ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„ ๋˜๋Š” ๋ฉค๋ฒ„ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
    • static ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” this ํฌ์ธํ„ฐ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค.
    • static ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์™€ static ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐ์ฒด์™€๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•œ๋‹ค.
      • ๊ทธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋„ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„, static ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.