19. [C++] ํด๋ž˜์Šค ์ƒ์†

2019. 6. 11. 10:54ใ†PROGRAMMING/C & C++

 

 

1. ์ƒ์†

 

Aํด๋ž˜์Šค(๋ถ€๋ชจ ํด๋ž˜์Šค)๊ฐ€ ๊ฐ€์ง„ ๊ณตํ†ต๋œ ์š”์†Œ๋ฅผ

์—ฌ๋Ÿฌ ํด๋ž˜์Šค(์ž์‹ ํด๋ž˜์Šค)์—๊ฒŒ ์žฌ์‚ฌ์šฉํ•˜๋„๋ก ๋ฌผ๋ ค์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

๋ถ€๋ชจ ํด๋ž˜์Šค๋Š” ์ž์‹ ํด๋ž˜์Šค๋“ค์˜ ์ถ”์ƒ์ ์ธ ์กด์žฌ๊ฐ€ ๋œ๋‹ค.

(์ƒ์†์€ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋ฌธ๋ฒ•์ด๋‹ค.)

 

๋ถ€๋ชจ ํด๋ž˜์Šค == ๊ธฐ๋ณธ ํด๋ž˜์Šค(Base Class) == ์ƒ์œ„ ํด๋ž˜์Šค == ์Šˆํผ ํด๋ž˜์Šค

์ž์‹ ํด๋ž˜์Šค == ํ™•์žฅ ํด๋ž˜์Šค(Derived Class) == ํ•˜์œ„ ํด๋ž˜์Šค == ์„œ๋ธŒ ํด๋ž˜

 

 

์ƒ์†์˜ ํ˜•ํƒœ์—๋Š” public ์ƒ์†, protected ์ƒ์†, private ์ƒ์†์ด ์žˆ๋‹ค.

๋ณดํ†ต ๋ถ€๋ชจ์˜ ๋ฉค๋ฒ„ ์ ‘๊ทผ์„ ์œ„ํ•ด public ์ƒ์†์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 


 

 

 

CGameObject ํด๋ž˜์Šค๋ฅผ CPlayer์— ์ƒ์†์‹œํ‚จ ๋ชจ์Šต์ด๋‹ค.

 

 


 

 

[ ์ž์‹ ๊ฐ์ฒด(CPlayer)์˜ ์ƒ์„ฑ ๊ณผ์ • ]

๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น -> ๋ถ€๋ชจ ์ƒ์„ฑ์ž ํ˜ธ์ถœ(CGameObject) -> ์ž์‹ ์ƒ์„ฑ์ž ํ˜ธ์ถœ.

 

[ ์ž์‹ ๊ฐ์ฒด(CPlayer)์˜ ์†Œ๋ฉธ ๊ณผ์ • ]

์ž์‹ ์†Œ๋ฉธ์ž ํ˜ธ์ถœ(CPlayer) -> ๋ถ€๋ชจ ์†Œ๋ฉธ์ž ํ˜ธ์ถœ(CGameObject) -> ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜ํ™˜.

 

 

์ƒ์„ฑ ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

์ž์‹ ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋Š” ์ž์‹ ์ด ์ƒ์†ํ•œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์˜๋ฌด๋ฅผ ์ง€๋‹Œ๋‹ค.

๊ทธ๋ž˜์„œ ์ž์‹์˜ ์ƒ์„ฑ์ž๋Š” ๋ถ€๋ชจ์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ๋กœ

๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋Š” ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

๊ฐ์ฒด์˜ ์†Œ๋ฉธ ๊ณผ์ •๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

 

 

 

 


 

 

 

์ž์‹ ๊ฐ์ฒด๋Š” ๋ถ€๋ชจ์˜ ๋ฉค๋ฒ„๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 


 

 

 

[ ์ƒ์†์˜ ์กฐ๊ฑด ]

is - a ๊ด€๊ณ„ -> ~์€ ~์ด๋‹ค.

ex) ํ•™์ƒ์€ ์‚ฌ๋žŒ์ด๋‹ค.

CPerson (๋ถ€๋ชจ) - CStudent (์ž์‹)

์ด์€ ๋ฌด๊ธฐ์ด๋‹ค.

CWeapon (๋ถ€๋ชจ) - CGun (์ž์‹)

 

 

has - a ๊ด€๊ณ„ -> ~์ด -์„ ๊ฐ–๋Š”๋‹ค.

์ƒ์† ๊ด€๊ณ„ ๋ณด๋‹ค๋Š” ํฌํ•จ ๊ด€๊ณ„๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

has - a ๊ด€๊ณ„๋Š” ๋‹ค์ค‘ ์ƒ์†์œผ๋กœ ๋ณ€์งˆ๋  ์œ„ํ—˜์ด ์กด์žฌํ•œ๋‹ค.

(๋‹ค์ค‘ ์ƒ์† - ํ•˜๋‚˜์˜ ์ž์‹์ด ๋‘˜ ์ด์ƒ์˜ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ƒ์†.

๋‹ค์ค‘ ์ƒ์†์˜ ๋ฌธ์ œ์  - ๋ชจํ˜ธ์„ฑ.)