๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

*<์”น์–ด๋จน๋Š” C++>์„ ๋ณด๋ฉฐ ๊ณต๋ถ€ํ•˜์—ฌ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

 

 

์˜ˆ์‹œ๋ฅผ ๋“ค๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชฌ์Šคํ„ฐ(Monster) ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

class Monster {
private:
   string name;
   int hp;
   int attackDamage;

public:
   Monster();
   Monster(string name, int hp, int attackDamage) { ... }
   ...
};

 

 


๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ• (๋™์  ํ• ๋‹น X)

 

์•”์‹œ์  ๋ฐฉ๋ฒ•(Implicit)

Monster monster("๋‹ฌํŒฝ์ด", 100, 5);
Monster defMonster;                  // ๋””ํดํŠธ ์ƒ์„ฑ์ž ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ

 

๋ช…์‹œ์  ๋ฐฉ๋ฒ•(Explicit)

Monster monster = Monster("๋‹ฌํŒฝ์ด", 100, 5);
Monster defMonster = Monster();     // ๋””ํดํŠธ ์ƒ์„ฑ์ž ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ

 

๋””ํดํŠธ ์ƒ์„ฑ์ž ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

 

ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

// Monster Monster();    ๋ฆฌํ„ด๊ฐ’์ด Monster, ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” Monster() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ๊ฒƒ์œผ๋กœ ์ธ์‹

 

๋ช…์‹œ์ ์œผ๋กœ ๋””ํดํŠธ ์ƒ์„ฑ์ž ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•

 

C++ 11 ์ดํ›„๋ถ€ํ„ฐ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ช…์‹œ ๊ฐ€๋Šฅ

class Monster{
public:
   Monster() = default;
   ...
};

 

 

728x90
๋ฐ˜์‘ํ˜•