์ด์ ์ฅ์์ ๋ณด์ฌ์ค "Hello World" ํ๋ก๊ทธ๋จ์ ์ ์ฉ์ฑ์ ๋ค์ ์์ฌ์ค๋ฝ์ต๋๋ค. ๋ช ์ค์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์ปดํ์ผํ ๋ค์ ๊ฒฐ๊ณผ ํ๋ก๊ทธ๋จ์ ์คํํด์ผ ํ๋ฉด์ ์์ฑ๋ ๊ฐ๋จํ ๋ฌธ์ฅ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์์ต๋๋ค. ์ถ๋ ฅ ๋ฌธ์ฅ์ ์ง์ ์
๋ ฅํ๋ ๊ฒ์ด ํจ์ฌ ๋ ๋นจ๋์ ๊ฒ์
๋๋ค.
๊ทธ๋ฌ๋ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ฉด์ ๊ฐ๋จํ ํ
์คํธ๋ฅผ ์ธ์ํ๋ ๋ฐ๋ง ๊ตญํ๋์ง ์์ต๋๋ค. ์กฐ๊ธ ๋ ๋์๊ฐ์ ์ค์ ๋ก ์์
์ ์ ์ฝํ๋ ์ ์ฉํ ์์
์ ์ํํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ ์ ์๊ฒ ํ๋ ค๋ฉด ๋ณ์ ๊ฐ๋
์ ๋์
ํด์ผ ํฉ๋๋ค .
์ซ์ 5๋ฅผ ๊ธฐ์ตํ๋ผ๊ณ ํ ๋ค์ ์ซ์ 2๋ ๋์์ ์ธ์ฐ๋ผ๊ณ ํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ๋ฉ๋ชจ๋ฆฌ์ ๋ ๊ฐ์ ๋ค๋ฅธ ๊ฐ(5 ๋ฐ 2)์ ์ ์ฅํ์ต๋๋ค. ์ด์ ๋ด๊ฐ ๋งํ ์ฒซ ๋ฒ์งธ ์ซ์์ 1์ ๋ํ๋ผ๊ณ ์์ฒญํ๋ฉด ์ซ์ 6(์ฆ, 5+1)๊ณผ 2๋ฅผ ๊ธฐ์ตํด์ผ ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์๋ฅผ ๋ค์ด ์ด๋ฌํ ๊ฐ์ ๋นผ์ ๊ฒฐ๊ณผ๋ก 4๋ฅผ ์ป์ ์ ์์ต๋๋ค.
์์์ ์ค๋ช
ํ ์ ์ฒด ํ๋ก์ธ์ค๋ ์ปดํจํฐ๊ฐ ๋ ๊ฐ์ ๋ณ์๋ก ์ํํ ์ ์๋ ๊ฒ๊ณผ ์ ์ฌํฉ๋๋ค. ๋์ผํ ํ๋ก์ธ์ค๋ฅผ ๋ค์ ๋ช
๋ น๋ฌธ ์ธํธ๋ก C++๋ก ํํํ ์ ์์ต๋๋ค.
a = 5;
b = 2;
a = a + 1;
result = a - b;
ํค์๋์ ์๋ณ์
ํค์๋๋ ์ฌ๋ฌ๊ฐ์ง ๋ฌธ๋ฒ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํด์ c++์์ ์ง์ํ๋ ๋จ์ด๋ค์ ๋๋ค.
์๋ณ์๋ ๋ณ์, ํจ์, ํด๋์ค๋ช ๋ฑ ์ฐ๋ฆฌ๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์๋ก๋ค๋ฅธ ๊ณต๊ฐ์ ๊ตฌ๋ถํ๊ธฐ์ํด ์ง์ด์ง
์ด๋ฆ์ ์ด์ผ๊ธฐํฉ๋๋ค.
- ํค์๋๋ ์์ฝ๋์ด ์์ผ๋ฏ๋ก ํค์๋๋ ์๋ณ์๊ฐ ๋ ์ ์์ต๋๋ค.
- ์๋ณ์๋ ๋์๋ฌธ์, ์ซ์ ๋ฐ ๋ฌธ์๋ก๋ง ๊ตฌ์ฑ๋ ์ ์์ต๋๋ค.
- ์๋ณ์๋ ๋์๋ฌธ์ ๋๋ _ ๋ฌธ์๋ก๋ง ์์๋์ผ ํฉ๋๋ค. ( ์ซ์๋ก ์์๋ ์ ์์ต๋๋ค. )
- C++์ ๋๋ฌธ์์ ์๋ฌธ์๋ฅผ ๊ตฌ๋ถํฉ๋๋ค.
alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
ํน์ ์ปดํ์ผ๋ฌ์๋ ์ถ๊ฐ๋ก ํน์ ์์ฝ๋ ํค์๋๊ฐ ์์ ์๋ ์์ต๋๋ค.
๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ
๋ณ์์ ๊ฐ์ ์ปดํจํฐ ๋ฉ๋ชจ๋ฆฌ์ 0๊ณผ 1๋ก ์ ์ฅ๋ฉ๋๋ค. ์ฐ๋ฆฌ ํ๋ก๊ทธ๋จ์ ๋ณ์๊ฐ ์ ์ฅ๋ ์ ํํ ์์น๋ฅผ ์ ํ์๊ฐ ์์ต๋๋ค. ๋จ์ํ ์ด๋ฆ์ผ๋ก ๊ฐ์ ธ๋ค๊ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํ๋ก๊ทธ๋จ์ด ์์์ผ ํ ๊ฒ์ ๋ณ์์ ์ ์ฅ๋ ๋ฐ์ดํฐ์ ์ข
๋ฅ์
๋๋ค. ๋จ์ํ ์ ์๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ๋ฌธ์๋ ํฐ ๋ถ๋ ์์์ ์ซ์๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์์ด ๋ค๋ฆ
๋๋ค. ๋ชจ๋ 0๊ณผ 1์ ์ฌ์ฉํ์ฌ ํ์๋์ง๋ง ๋์ผํ ๋ฐฉ์์ผ๋ก ํด์๋์ง ์์ผ๋ฉฐ ๋ง์ ๊ฒฝ์ฐ ๋์ผํ ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ์ง ์์ต๋๋ค.
๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ์ ๋๋ถ๋ถ์ ์์คํ
์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํ๋ ๊ธฐ๋ณธ ์ ์ฅ ๋จ์๋ฅผ ๋ํ๋ด๋ ์ธ์ด๋ก ์ง์ ๊ตฌํ๋๋ ๊ธฐ๋ณธ ์ ํ์
๋๋ค. ์ฃผ๋ก ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ฅํ ์ ์์ต๋๋ค.
- ๋ฌธ์ ์ ํ : 'A' ๋๋ ์ ๊ฐ์ ๋จ์ผ ๋ฌธ์๋ฅผ ๋ํ๋ผ ์ ์์ต๋๋ค '$'. ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ํ์ char1๋ฐ์ดํธ ๋ฌธ์์ ๋๋ค. ๋ ๋์ ๋ฌธ์๋ฅผ ์ํด ๋ค๋ฅธ ์ ํ๋ ์ ๊ณต๋ฉ๋๋ค.
- ์ ์ ์ ํ : 7 ๋๋ ์ ๊ฐ์ ์ ์ ๊ฐ์ ์ ์ฅํ ์ ์์ต๋๋ค 1024. ๋ค์ํ ํฌ๊ธฐ๋ก ์กด์ฌํ๋ฉฐ ์์ ๊ฐ์ ์ง์ํ๋์ง ์ฌ๋ถ์ ๋ฐ๋ผ ๋ถํธ ๊ฐ ์๊ฑฐ๋ ๋ถํธ ๊ฐ ์์ ์ ์์ต๋๋ค.
- ๋ถ๋ ์์์ ์ ํ : ์ธ ๊ฐ์ง ๋ถ๋ ์์์ ์ ํ ์ค ์ฌ์ฉ๋๋ ์ ํ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ ๋ฐ๋ ์์ค ์ผ๋ก 3.14๋๋ ์ ๊ฐ์ ์ค์ ๊ฐ์ ๋ํ๋ผ ์ ์์ต๋๋ค .0.01
- BOOL ์ ํ : C++์์ ๋ก ์๋ ค์ง ๋ถ์ธ ์ ํ bool์ ๋ ๊ฐ์ง ์ํ true๋๋ false.
๊ตฌ๋ถ์๋ฃํํฌ๊ธฐ๋ฒ์
๊ธฐ๋ณธํ | void | – | – |
๋ฌธ์ํ | (signed) char | 1 byte | -128 ~ 127 |
unsigned char | 1 byte | 0 ~ 255 | |
wchar_t | 2 byte | 0 ~ 65,535 | |
์ ์ํ | bool | 1 byte | 0 ~ 1 |
(signed) short (int) | 2 byte | -32,768 ~ 32,767 | |
unsigned short (int) | 4 byte | 0 ~ 65,535 | |
(signed) int | 4 byte | -2,147,483,648 ~ 2,147,483,647 | |
unsigned int | 4 byte | 0 ~ 4,294,967,295 | |
(signed) long (int) | 4 byte | -2,147,483,648 ~ 2,147,483,647 | |
unsigned long (int) | 4 byte | 0 ~ 4,294,967,295 | |
__int8 | 1 byte | -128 ~ 127 | |
__int16 | 2 byte | -32,768 ~ 32,767 | |
__int32 | 4 byte | -2,147,483,648 ~ 2,147,483,647 | |
__int64 | 8 byte | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | |
์ค์ํ | float | 4 byte | 3.4E-38(-3.4*10^38) ~ 3.4E+38(3.4*10^38) (7digits) |
(long) double | 8 byte | 1.79E-308(-1.79*10^308) ~ 1.79E+308(1.79*10^308) (15digits) |
signed๋ C/C++ ํ๋ก๊ทธ๋จ ์ธ์ด์์ ์ ์ํ ๋ณ์ ์ค ๋ถํธ๋ฅผ ๊ฐ๋ ๋ณ์๋ฅผ ์ ์ธ ํ๋ค. ์ ์ํ ์ค ์์๋ 2์ ๋ณด์ ์ฒด๊ณ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ์ด ํค์๋์ ์ํด ๋ถํธ๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๋ณ์ ์ ์ธํ ์ ์๋ค. ๊ทธ๋ฌ๋ ์ ์ํ์ ๋ณ์์์ unsigned๊ฐ ์์ผ๋ฉด ์์๋ฅผ ์ฌ์ฉํ ์ ์๋ ๋ถํธ๋ฅผ ๊ฐ๋ ์ ์ํ์ด ๋๋ค.
char(์ ํํ 1๋ฐ์ดํธ์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง) ๊ธฐ๋ณธ ์ ํ ์ค ์ด๋ ๊ฒ๋ ํ์ค ํฌ๊ธฐ๊ฐ ์ง์ ๋์ง ์์์ต๋๋ค. ๋ชจ๋ ์ปดํ์ผ๋ฌ์ ์ปดํจํฐ์ ํ์ค ํฌ๊ธฐ๊ฐ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ๊ฐ ์ปดํ์ผ๋ฌ ๋ง๋ค ํ๋ก๊ทธ๋จ์ด ์คํ๋ ๋ ์ ํฉํ ์ด๋ฌํ ์ ํ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
ํฌ๊ธฐ๊ณ ์ ํ ํํ ๊ฐ๋ฅํ ๊ฐ์น๋ฉ๋ชจ
8๋นํธ | 256 | = 2^8 |
16๋นํธ | 65 536 | = 2^16 |
32๋นํธ | 4 294 967 296 | = 2^32 (~40์ต) |
64๋นํธ | 18 446 744 073 709 551 616 | = 2^64 (~180์ต) |
์ ์ ์ ํ์ ๊ฒฝ์ฐ ํํ ๊ฐ๋ฅํ ๊ฐ์ด ๋ ๋ง๋ค๋ ๊ฒ์ ํํํ ์ ์๋ ๊ฐ์ ๋ฒ์๊ฐ ๋ ํฌ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์๋ฅผ ๋ค์ด, 16๋นํธ ๋ถํธ ์๋ ์ ์๋ 0์์ 65535๊น์ง์ ๋ฒ์์์ 65536๊ฐ์ ๊ณ ์ ํ ๊ฐ์ ๋ํ๋ผ ์ ์๋ ๋ฐ๋ฉด ๋ถํธ ์๋ ์ ์๋ ๋๋ถ๋ถ์ ๊ฒฝ์ฐ -32768์์ 32767 ์ฌ์ด์ ๊ฐ์ ๋ํ๋ผ ์ ์์ต๋๋ค. 16๋นํธ ์ค ํ๋๊ฐ ๋ถํธ์ ์ฌ์ฉ๋๋ค๋ ์ฌ์ค ๋๋ฌธ์ ์์ ๊ฐ์ ๋ถํธ ์๋ ์ ํ์ ๋นํด ๋ถํธ ์๋ ์ ํ์์ ๋๋ต ์ ๋ฐ์ผ๋ก ์ค์ด๋ญ๋๋ค.
๋ถ๋ ์์์ ์ ํ์ ๊ฒฝ์ฐ ํฌ๊ธฐ๋ ์ ํจ ๋ฐ ์ง์์ ๋ํด ๋ ๋ง๊ฑฐ๋ ์ ์ ๋นํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฐ๋์ ์ํฅ์ ์ค๋๋ค.
C++๋ ์์์ ๋
ผ์ํ ๊ธฐ๋ณธ ์ ํ์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ํ ์ ํ์ ์ง์ํฉ๋๋ค. ์ด๋ฌํ ๋ค๋ฅธ ์ ํ์ ๋ณตํฉ ๋ฐ์ดํฐ ์ ํ ์ด๋ผ๊ณ ํ๋ฉฐ C++ ์ธ์ด์ ์ฃผ์ ๊ฐ์ ์ค ํ๋์
๋๋ค. ์ฐ๋ฆฌ๋ ๋ํ ๋ค์ ์ฅ์์ ๋ ์์ธํ ๋ณผ ๊ฒ์
๋๋ค.
๋ณ์ ์ ์ธ
C++๋ ๊ฐ๋ ฅํ ํ์์ ์ธ์ด์ด๋ฉฐ ๋ชจ๋ ๋ณ์๋ฅผ ์ฒ์ ์ฌ์ฉํ๊ธฐ ์ ์ ํด๋น ํ์์ผ๋ก ์ ์ธํด์ผ ํฉ๋๋ค. ์ด๊ฒ์ ๋ณ์์ ๋ํด ๋ฉ๋ชจ๋ฆฌ์ ์์ฝํ ํฌ๊ธฐ์ ํด๋น ๊ฐ์ ํด์ํ๋ ๋ฐฉ๋ฒ์ ์ปดํ์ผ๋ฌ์ ์๋ ค์ค๋๋ค. C++์์ ์ ๋ณ์๋ฅผ ์ ์ธํ๋ ๊ตฌ๋ฌธ์ ๊ฐ๋จํฉ๋๋ค. ์ ํ ๋ค์์ ๋ณ์ ์ด๋ฆ(์ฆ, ํด๋น ์๋ณ์)์ ์์ฑํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
int a;
float mynumber;
์ด๊ฒ์ ๋ ๊ฐ์ง ๋ณ์ ์ ์ธ์ ๋๋ค. ์ฒซ ๋ฒ์งธ a๋ int ์๋ณ์๊ฐ ์๋ ์ ํ์ ๋ณ์๋ฅผ ์ ์ธํฉ๋๋ค. ๋ ๋ฒ์งธ mynumber๋ float ์๋ณ์๊ฐ ์๋ ์ ํ์ ๋ณ์๋ฅผ ์ ์ธํฉ๋๋ค. ์ผ๋จ ์ ์ธ๋๋ฉด ๋ณ์ a๋ฐ mynumberํ๋ก๊ทธ๋จ์ ๋๋จธ์ง ๋ฒ์ ๋ด์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋์ผํ ์ ํ์ ๋ณ์๋ฅผ ๋ ์ด์ ์ ์ธํ๋ ๊ฒฝ์ฐ ์๋ณ์๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ๋จ์ผ ๋ช
๋ น๋ฌธ์์ ๋ชจ๋ ์ ์ธํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
int a, b, c;
์ด๊ฒ์ ์ธ ๊ฐ์ ๋ณ์( a, b๋ฐ c)๋ฅผ ์ ์ธํ๊ณ ๋ชจ๋ ์ ํ int์ด๋ฉฐ ๋ค์๊ณผ ์ ํํ ๊ฐ์ ์๋ฏธ๋ฅผ ๊ฐ์ต๋๋ค.
int a;
int b;
int c;
ํ๋ก๊ทธ๋จ ๋ด์์ ๋ณ์ ์ ์ธ์ด ์ด๋ป๊ฒ ์๋ํ๋์ง ๋ณด๊ธฐ ์ํด ์ด ์ฅ์ ์์ ๋ถ๋ถ์์ ์ ์ํ ์ ์ ๊ธฐ์ต์ ๋ํ ์์ ์ ์ ์ฒด C++ ์ฝ๋๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
// operating with variables
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
}
๋ณ์ ์ด๊ธฐํ
์์ ์์์ ๋ณ์๊ฐ ์ ์ธ๋๋ฉด ์ฒ์ ๊ฐ์ด ํ ๋น๋ ๋๊น์ง ๋ฏธ์ ์ ๊ฐ์ ๊ฐ์ง๋๋ค. ๊ทธ๋ฌ๋ ๋ณ์๋ ์ ์ธ๋ ์๊ฐ๋ถํฐ ํน์ ๊ฐ์ ๊ฐ์ง ์ ์์ต๋๋ค. ์ด๊ฒ์ ๋ณ์ ์ด๊ธฐํ ๋ผ๊ณ ํฉ๋๋ค.
C++์์๋ ๋ณ์๋ฅผ ์ด๊ธฐํํ๋ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
์ฒซ ๋ฒ์งธ ์ด๊ธฐํ ๋ ๋ฑํธ ๋ค์ ๊ฐ์ ์ถ๊ฐํ๋ ๊ฒ์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค
type identifier = initial_value;
int x์ ์ธ๋ ์๊ฐ๋ถํฐ 0 ๊ฐ์ผ๋ก ์ด๊ธฐํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.
int x = 0;โ
์์ฑ์ ์ด๊ธฐํ (C++ ์ธ์ด์์ ๋์
) ๋ก ์๋ ค์ง ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ ์ ์ด๊ธฐ ๊ฐ์ ๊ดํธ( )๋ก ๋ฌถ์ต๋๋ค.
type identifier (initial_value);
์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Int x (0);
๋ง์ง๋ง์ผ๋ก, ๊ท ์ผ ์ด๊ธฐํ ๋ก ์๋ ค์ง ์ธ ๋ฒ์งธ ๋ฐฉ๋ฒ ์ ์์ ์ ์ฌํ์ง๋ง ๊ดํธ ๋์ ์ค๊ดํธ( {})๋ฅผ ์ฌ์ฉํฉ๋๋ค(์ด๋ 2011๋
C++ ํ์ค ๊ฐ์ ํ์ ๋์
๋จ).
type identifier {initial_value};
์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
int x {0};
๋ณ์๋ฅผ ์ด๊ธฐํํ๋ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋ชจ๋ C++์์ ์ ํจํ๊ณ ๋๋ฑํฉ๋๋ค.
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value: 5
int b(3); // initial value: 3
int c{2}; // initial value: 2
int result; // initial value undetermined
a = a + b;
result = a - c;
cout << result;
return 0;
}
์ ๋ณ์๊ฐ ์์ฑ ๋ ๋ ์ปดํ์ผ๋ฌ๋ ํ๋ก๊ทธ๋จ์์ ์๋์ผ๋ก ๋ณ์์ ์ ํ์ ํ์ ํ ์ ์์ต๋๋ค.
int foo = 0;
auto bar = foo; // the same as: int bar = foo;
Bar ์ ํ์ foo์ intํ์ ๋ฐ๋ผ์ ์๋์ผ๋ก intํ์ผ๋ก ์ ์ธ์ด ๋ฉ๋๋ค.
์ด๊ธฐํ๋์ง ์์ ๋ณ์๋ decltype์ง์ ์์ ํจ๊ป ์ ํ ์ถ๋ก ์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
int foo = 0;
decltype(foo) bar; // the same as: int bar;
bar๋ foo์ ๊ฐ์ ํ์
์ผ๋ก ์ ์ธ๋ฉ๋๋ค. decltype์ ์ต๊ทผ์ ์ธ์ด์ ์ถ๊ฐ๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์
๋๋ค
auto. decltype์ ์์ฃผ ์ฌ์ฉ๋์ง๋ ์์ง๋ง ๋๋๋ก ์ฝ๋์ ๊ฐ๋
์ฑ์ ๋์ด๊ธฐ ์ํด์
์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๋ฌธ์์ด ํด๋์ค ์๊ฐ
C++ ์ธ์ด์ ์ฃผ์ ์ฅ์ ์ค ํ๋๋ ์ฌ๋ฌ๊ฐ์ง ์ด๋ฏธ ๋ง๋ค์ด์ง class ์ ํ๋ค์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฌํ ์ ํ์ ์๋ stringํด๋์ค์
๋๋ค. ์ด ์ ํ์ ๋ณ์๋ ๋จ์ด๋ ๋ฌธ์ฅ๊ณผ ๊ฐ์ ์ผ๋ จ์ ๋ฌธ์๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค. ๋งค์ฐ ์ ์ฉํ ๊ธฐ๋ฅ์
๋๋ค!
๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ๊ณผ์ ์ฒซ ๋ฒ์งธ ์ฐจ์ด์ ์ ์ด ์ ํ์ ๊ฐ์ฒด(๋ณ์)๋ฅผ ์ ์ธํ๊ณ ์ฌ์ฉํ๋ ค๋ฉด ํ๋ก๊ทธ๋จ์ด ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ(header <string>) ๋ด์์ ์ ํ์ด ์ ์๋ ํค๋๋ฅผ ํฌํจํด์ผ ํ๋ค๋ ๊ฒ์
๋๋ค.
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is a string";
cout << mystring;
return 0;
}
์ด์ ์์ ์์ ๋ณผ ์ ์๋ฏ์ด ์ซ์ ์ ํ ๋ณ์๊ฐ ์ ํจํ ์ซ์ ๋ฆฌํฐ๋ด๋ก ์ด๊ธฐํ๋ ์ ์๋ ๊ฒ์ฒ๋ผ ๋ฌธ์์ด์ ์ ํจํ ๋ฌธ์์ด ๋ฆฌํฐ๋ด๋ก ์ด๊ธฐํ๋ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ ์ ํ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ชจ๋ ์ด๊ธฐํ ํ์์ string์์๋ ์ ํจํฉ๋๋ค.
string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};
String์ ๋ํ ์ด๊ธฐ ๊ฐ ์์ด ์ ์ธ๋๊ณ ์คํ ์ค์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๊ฒ๊ณผ ๊ฐ์ด ๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ์ด ํ ์ ์๋ ๋ค๋ฅธ ๋ชจ๋ ๊ธฐ๋ณธ ์์ ์ ์ํํ ์ ์์ต๋๋ค.
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
string ํด๋์ค๋ ๋ณตํฉ ์ ํ ์ ๋๋ค. ์์ ์์์ ๋ณผ ์ ์๋ฏ์ด ๋ณตํฉ ์ ํ ์ ๊ธฐ๋ณธ ์ ํ ๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค . ๋์ผํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ด๊ธฐํํฉ๋๋ค.
'๐ Development Study > ๐ป Win32API' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฐ์ฐ์ (0) | 2022.08.17 |
---|---|
์์ (0) | 2022.08.17 |
ํ๋ก๊ทธ๋จ์ ๊ตฌ์กฐ (0) | 2022.08.17 |
์ปดํ์ผ๋ฌ (0) | 2022.08.17 |
๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ์ดํด (0) | 2022.07.27 |
๋๊ธ