C++/๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ (OOP)

INTRO int total {0}; total = 100 + 200; ์œ„ ์ฝ”๋“œ์—์„œ total์€ ํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜ ๊ฐ’, L-Value์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  total์— ํ• ๋‹น๋˜๋Š” 100+200 ์ˆ˜์‹์€ ํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋Š” R-Value์ž…๋‹ˆ๋‹ค. C++์—์„œ ์ด๋Ÿฐ ๊ฐ’๋“ค์€ ์ด๋ฆ„์—†๋Š” temp value๋กœ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋‹ค๊ฐ€, total๊ณผ ๊ฐ™์€ ๊ฐ’์— ๋ณต์‚ฌ ์ €์žฅ๋˜๊ณ  ๋‚œ ๋’ค ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ์ผ์ด temp value๋ฅผ temp object๋กœ๋งŒ ๋ฐ”๋€Œ์–ด์„œ ๊ฐ์ฒด์—์„œ๋„ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด temporary object๊ฐ€ ๊ทธ๋งŒํผ ๋งŽ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ๊ฐ์ฒด๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. Deep copy๋ฅผ ํ–‰ํ•˜๋ฉด ๋”์šฑ ๋งŽ์ด ์š”..
C++ Class ์˜ Copy Constructor์—๋Š” default copy constructor์ด ์‚ฌ์šฉํ•˜๋Š” Shallow Copying๊ณผ Deep Copying์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์–•์€ ๋ณต์‚ฌ, shallow copying์„ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์ง€๋งŒ, ์–•์€ ๋ณต์‚ฌ๊ฐ€ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ๊ฐ๊ฐ ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Shallow Copying - ์–•์€ ๋ณต์‚ฌ Shallow copy๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” default copy constructor๊ฐ€ ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. Shallow copying์€ ๋ณต์‚ฌ ๋Œ€์ƒ์˜ ๊ฐ์ฒด์˜ ๋ชจ๋“  ์†์„ฑ์„ ์ง€๋‹Œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ๋  ๋•Œ ์ž…๋‹ˆ๋‹ค. ๋ณต์‚ฌ์˜ ๊ณผ์ •์—์„œ ํฌ์ธํ„ฐ๋Š” ๋ณต์‚ฌ๋˜์ง€๋งŒ, ..
ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ, ๋‚ด๋ถ€์—์„œ public: ํ‚ค์›Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํด๋ž˜์Šค์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ์ ‘๊ทผ ์ œํ•œ์ž(Access Modifier) ๋ผ๋Š” ๊ฐœ๋…์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. C++ ์ ‘๊ทผ ์ œํ•œ์ž์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 3๊ฐ€์ง€์˜ ์ ‘๊ทผ ์ œํ•œ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ public, private, protcted ์ž…๋‹ˆ๋‹ค. public ์ ‘๊ทผ ์ œํ•œ์ž๋Š” ์ฝ”๋“œ ๋‚ด์˜ ๋ชจ๋“  ๊ณณ์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. private ์ ‘๊ทผ ์ œํ•œ์ž๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค ๋ฉค๋ฒ„๋“ค์ด๋‚˜ friend๋“ค์—๊ฒŒ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. protected ์ ‘๊ทผ ์ œํ•œ์ž๋Š” ์ƒ์†(inheritance)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ์ ‘๊ทผ ์ œํ•œ์ž์ž…๋‹ˆ๋‹ค. syntax๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class Class_Name{ public: // private: // prote..
๊ฐ์ฒด๋Š” ๊ฝค๋‚˜ ํฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Pass by value ๋ฐฉ๋ฒ• ํ˜น์€ return ๊ฐ’์œผ๋กœ ํŠน์ • ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ๋Š” ํฐ ์šฉ๋Ÿ‰์„ ๋‚ญ๋น„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ, pass by reference ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. // main.cpp #include "Practice.h" #include using namespace std; int main() { Player ricky("Ricky", 100, 0); Player super_enemy = create_super_enemy(); Player ricky2 = another_hero(ricky); return 0; } // Practice.cpp #include "Practice.h" #include #include us..
Chan Lee
'C++/๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ (OOP)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)