C++

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..
ยทC++/๊ธฐํƒ€
cin.ignore()์€ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, cin์œผ๋กœ 1000 5000์„ ๋ฐ›์•˜์„ ๋•Œ ๋‹ค์Œ cin์€ ์ž…๋ ฅ์„ ํ•˜์ง€๋„ ์•Š์•˜๋Š”๋ฐ 5000 ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋ฒ„ํผ์— 5000 ๊ฐ’์ด ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ cin.ignore()์œผ๋กœ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๊ณ  ๋‚˜์„œ ๋‹ค์‹œ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐฉ์ง€๋ฉ๋‹ˆ๋‹ค.
Chan Lee
'C++' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (4 Page)