1. Hello World
by JiwonDev1. Main ์ฝ๋
#include <iostream> // std using namespace std; // std::cout , std::endl int main() { cout << "Hello World" << endl; return 0; }
Cpp์์๋ std::cout์ ์ด์ฉํด์ ์ถ๋ ฅ์ ํ๋ค.
2. Cout
์ฝ์์ฐฝ์ ์ถ๋ ฅ ํ ํ ostream๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
๊ฐ OS๊ฐ ์ ๊ณตํ๋ ์์คํ ์ฝํจ์ [ Window API - WriteFile() , Linux - system call - Write() ]๋ฅผ ์ด์ฉํ๋ค.
#include <cstdio> using namespace std; // std::cout ์๋ฆฌ ํ์
์ ์ํ ๊ตฌํ ์์ namespace std { class ostream { public: ostream &operator<< (int n) { printf("%d", n); return *this; } ostream &operator<< (double d) { printf("%f", d); return *this; } }; ostream cout; } int main () { cout << 3; // ostream& operator<<(int n) ํธ์ถ๋จ cout << 3.4; // ostream& operator<<(double d) ํธ์ถ๋จ cout << 3 << 4; // ์๊ธฐ ์์ ์ ์ฐธ์กฐ๋ก ๋ฆฌํดํ๋ฏ๋ก ์ฐ์ ์ถ๋ ฅ ๊ฐ๋ฅ }
ํด๋น ํจ์๋ ์์ ๊ฐ์ด ์ ์๋์ด์๋ค.
3. Namespace
cpp์์ ์ด๋ฆ ์ค๋ณต์ ํผํ๊ธฐ ์ํด ์ ๊ณตํ๋ Scope(์ง์ญ,์ ์ญ ๋ฒ์) ๊ธฐ๋ฅ์ด๋ค.
namespace { } ๋ฅผ ์ด์ฉํ์ฌ ๋ฒ์๋ฅผ ์ง์ ํ๊ณ
using ์ด๋ name:: (์ค์ฝํ ๋ถ์ ์ฐ์ฐ์)์ ์ด์ฉํ์ฌ ํด๋น ๋ฒ์์ ์๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
namespace Foo { // ๋ค์์คํ์ด์ค 1 int doSomething(int x, int y) { return x + y; } } namespace Goo { // ๋ค์์คํ์ด์ค 2 int doSomething(int x, int y) { return x - y; } } int main(void) { // ์ค๋ณต๋ ์ด๋ฆ(doSomthing)์์ ์ํ๋ ๋ฉ์๋๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ. std::cout << Foo::doSomething(4, 3) << '\n'; std::cout << Goo::doSomething(4, 3) << '\n'; return 0; }
์ฐธ๊ณ ๋ก namespace std { } ์์๋ Cpp์ ๋ชจ๋ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์ ์๋์ด ์๋ค.
#Using์ ์ด์ฉํ์ฌ ๊ฐํธํ๊ฒ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ. namespace Foo { int doSomething(int x, int y) { return x + y; } int doHello(){ return 1; } } //#์์ 1. int main(void) { using Foo::doSomething; // ๋๊ดํธ {} ์์ ์ง์ ๋์ง ์์ ํจ์๋ namespace๋ฅผ ์ฐธ์กฐํ๋ค. std::cout << doSomething(4, 3) << '\n'; // Foo::doSomething std::cout << Goo::doSomething(4, 3) << '\n'; return 0; } //#์์ 2. int main(void){ using namespace Foo; // ๋๊ดํธ {} ์์ ์ง์ ๋์ง ์์ ํจ์๋ namespace๋ฅผ ์ฐธ์กฐํ๋ค. std::cout << doSomething(4, 3) << '\n'; // Foo::doSomething doHello(); // using์ผ๋ก ๋ค์์คํ์ด์ค๋ฅผ ์ง์ ํด์คฌ์ผ๋ฏ๋ก, ์๋์ผ๋ก Foo๋ก ์ธ์. return 0; }
using namespace์ ๋ฒ์๋ ์ง์ญ๋ณ์, ์ ์ญ๋ณ์์ ๊ฐ๋ ๊ณผ ๋น์ทํ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
์ฐธ๊ณ ๋ก ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ์ถ์ธก์ด ๋ถ๊ฐ๋ฅํ ํจ์๋ ๋ฉ์๋ ์ค๋ณต์ผ๋ก ์ปดํ์ผ ์๋ฌ๊ฐ ๋จ๊ฒ ๋๋ค.
4. #include <iostream>
์์ # ์ด ๋ถ๋ ๋ฌธ์ฅ์ C์ธ์ด ์์ค์ฝ๋๊ฐ ์๋๋ผ ์ปดํ์ผ ์ ์คํ๋๋ ์ ์ฒ๋ฆฌ ๋ฌธ์ด๋ค.
#include ํ์ผ๋ช ์ ์ปดํ์ผ ํ ๋, ํด๋น ์์ค์ฝ๋๋ฅผ ์ฌ๊ธฐ๋ก ๋ถ๋ฌ์ค๋ผ๋ ์๋ฏธ
๋์ค์ ๋ฐฐ์ฐ๊ฒ ์ง๋ง cpp์์๋ ์ ์ธ๋ถ(ํค๋)์ ๊ตฌํ๋ถ(์ฝ๋)๊ฐ ๋๋์ด์ ธ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์์
๋ด๊ฐ ํ์ํ ์ฝ๋์ ํค๋ํ์ผ๋ค๊ณผ ๊ตฌํ๋ถ๋ฅผ include ํด์ค์ผ ํ๋ค. ๊ฐ๋ ๋๋ผ๋๊ฑฐ์ง๋ง, ์ฌ์ฉ๋ฒ์ด ๊ฑฐ์ง๊ฐ๋ค.
๋ณดํต ์์คํ ์ ํค๋ํ์ผ(ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ)๋ฅผ ๋ถ๋ฌ์ฌ๋๋ #include <System.h> ์ผ๋ก ์ฌ์ฉํ๊ณ
์ฌ์ฉ์ ๊ฐ์ธ์ ํค๋ํ์ผ์ ๋ถ๋ฌ์ฌ๋๋ #include "File.h" ๋๋ "...PATH/folder/File.h" ์ด๋ ๊ฒ ์ฌ์ฉํ๋ค.
๋ฌผ๋ก ์ด ๋์ ์ฐจ์ด๋ ํค๋ํ์ผ ํ์ ๋ฒ์/์์์ ์ฐจ์ด๋ผ์ "System.h"๋ก ์์ฑํด๋ ํด๋น ํ์ผ์ ์ฐพ์์ค๋ค.
์ฐธ๊ณ ๋ก cpp์์๋ c์ธ์ด์์ ์ฌ์ฉํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ตฌ๋ถํ๊ธฐ์ํด์ stdio -> cstdio ์ฒ๋ผ ์ด๋ฆ์ ๋ณ๊ฒฝํ์ฌ ์ฌ์ฉํ๋ค.
๋ํ cpp์์๋ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋๋ <iostream> ๊ณผ ๊ฐ์ด .h ๋ฅผ ๋ถ์ด์ง ์๋๊ฒ ๊ธฐ๋ณธ์ด๋ค.
๋ธ๋ก๊ทธ์ ์ ๋ณด
JiwonDev
JiwonDev