42seoul

42seoul/projects

[CPP Module] 08

Exercise 00: Easy find easyfind ๋ผ๋Š” ํ…œํ”Œ๋ฆฟํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค. ํ•ด๋‹นํ•จ์ˆ˜๋Š” container์™€ value๋ฅผ ๋ฐ›๊ณ  container์•ˆ์— ํ•ด๋‹น value๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹นํ•˜๋Š” value์œ„์น˜์˜ iterator๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ์กด์žฌํ•˜์ง€์•Š๋Š”๋‹ค๋ฉด error ์ฒ˜๋ฆฌ ํ•„์š”. iterator() typename T::iterator iter; iter = std::find(container.begin(), container.end(), value); std::cout

42seoul/projects

[CPP Module] 07

C++ templates https://jun-13.tistory.com/148 [C++] templates C++ templates ์ฝ”๋“œ์˜ ์ผ๋ฐ˜ํ™”์™€ ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ // templates function template T add(T a, T b) { return a + b; } int main() { // intํ˜• ์ธ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ std::cout moaoh.dev Exercise 00: Start with a few functions what is templates?? // templates function template T add(T a, T b) { return a + b; } int main() { // intํ˜• ์ธ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ std::cout

42seoul/projects

[C++] templates

C++ templates ์ฝ”๋“œ์˜ ์ผ๋ฐ˜ํ™”์™€ ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ // templates function template T add(T a, T b) { return a + b; } int main() { // intํ˜• ์ธ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ std::cout

42seoul/projects

[CPP Module] 06

C++ casts https://jun-13.tistory.com/146 [C++] casts C++ casts c++์—๋Š” ๋‹ค์–‘ํ•œ ํ˜• ๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž(๋˜๋Š” ์บ์ŠคํŠธ ์—ฐ์‚ฐ์ž)๊ฐ€ ์žˆ๋‹ค. static_cast reinterpret_cast dynamic_cast const_cast ์œ„์™€ ๊ฐ™์ด ํฌ๊ฒŒ 4๊ฐ€์ง€์˜ cast๋“ค์ด ์กด์žฌํ•œ๋‹ค. static_cast ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ํ˜• ๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž๋กœ, moaoh.dev Exercise 00: Conversion of scalar static_cast ํ˜•์‹ ๊ฐ„์˜ ํ˜• ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ˜• ๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž์ด๋‹ค. int i = 42; double d = static_cast(i); // int๋ฅผ double๋กœ ๋ณ€ํ™˜ double d = 3.14; int i = static_cast(d..

42seoul/projects

[C++] casts

C++ casts c++์—๋Š” ๋‹ค์–‘ํ•œ ํ˜• ๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž(๋˜๋Š” ์บ์ŠคํŠธ ์—ฐ์‚ฐ์ž)๊ฐ€ ์žˆ๋‹ค. static_cast reinterpret_cast dynamic_cast const_cast ์œ„์™€ ๊ฐ™์ด ํฌ๊ฒŒ 4๊ฐ€์ง€์˜ cast๋“ค์ด ์กด์žฌํ•œ๋‹ค. static_cast ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ํ˜• ๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž๋กœ, ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ์•ˆ์ „ํ•œ ํ˜• ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ํ˜• ๋ณ€ํ™˜์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์†์‹ค์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #include intmain() { double d = 3.14; int i = static_cast(d); // double์„ int๋กœ ๋ณ€ํ™˜ std::cout

42seoul/projects

[CPP Module] 05

๋ฐ˜๋ณต๊ณผ ์˜ˆ์™ธ์ฒ˜๋ฆฌ (Repetition and Exceptions) https://jun-13.tistory.com/144 [C++] Repetition and Exceptions Repetition and Exceptions๋ž€? ์˜ˆ์™ธ ํด๋ž˜์Šค ํด๋ž˜์Šค์•ˆ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ํ™•์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํด๋ž˜์Šค class Bureaucrat { public: class GradeTooHighException : public std::exception { public: const char *wha moaoh.dev Ex00: Mommy, when I grow up, I want to be a bureaucrat! ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๊ฐœ๋…์— ๋Œ€ํ•ด ๊ธฐ์ดˆ์ ์œผ๋กœ ๊ณต๋ถ€ํ•˜๋Š” ํŒŒํŠธ bureaucrat ํด๋ž˜์Šค co..

42seoul/projects

[C++] Repetition and Exceptions

Repetition and Exceptions๋ž€? ์˜ˆ์™ธ ํด๋ž˜์Šค ํด๋ž˜์Šค์•ˆ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ํ™•์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํด๋ž˜์Šค class Bureaucrat { public: class GradeTooHighException : public std::exception { public: const char *what() const throw() { return ("error"); } }; **std::exception**์€ C++ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์˜ˆ์™ธ ํด๋ž˜์Šค ์œ„์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. #include "Bureaucrat.hpp" intmain() { try { b.setGrade(0); } catch (std::exception &e) { std::cout

42seoul/projects

[C++] interfaces ( ์ธํ„ฐํŽ˜์ด์Šค )

interfaces ( ์ธํ„ฐํŽ˜์ด์Šค ) "ํŠน์ • ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์„ ์•ฝ์†ํ•œ ์ถ”์ƒ ํ˜•์‹”์ด๋ผ๊ณ  ๋ช…์‹œ๊ฐ€ ๋˜์–ด์žˆ๋‹ค. #include class Ihello { public: virtual void print() = 0; }; class korean : public Ihello { public: virtual void print() { std::cout

42seoul/projects

[C++] abstract class (์ถ”์ƒ ํด๋ž˜์Šค)

abstract class (์ถ”์ƒ ํด๋ž˜์Šค) ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆœ์ˆ˜ ๊ฐ€์ƒ ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. class Test { private: public: virtual void sound() = 0; // ์ˆœ์ˆ˜๊ฐ€์ƒํ•จ์ˆ˜ }; class Test { private: public: virtual void sound() = 0; }; class Copy : public Test { public: virtual void sound() { std::cout

moaoh
'42seoul' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก