본문 바로가기

개발 공부/C++

(20)
따라하며 배우는 C++ 19. 모던 C++ 필수 요소들 따라하며 배우는 C++ 19. 모던 C++ 필수 요소들 19.1 람다 함수와 std::function, std::bind, for_each #include #include #include #include using namespace std; void goodbye(const string& s) { cout
따라하며 배우는 C++ 18. 입력과 출력 따라하며 배우는 C++ 18. 입력과 출력 18.1 istream으로 입력받기 #include #include #include //input / output manipulators using namespace std; int main() { //기본적으로 stream은 버퍼에 저장되어 있는 걸 //임시적으로 일부씩 꺼내오는 것 char buf[10]; cin >> setw(10) >> buf; //최대 10글자까지만 버퍼에서 받을 수 있도록 방지함 //여기서 null 캐릭터 자리는 비워둬서 실질적으론 9글자까지만 가능함. cout setw(10) >> buf; cout setw(10) >> buf; cout ch) cout > i >> f; cout
따라하며 배우는 C++ 17. string 문자열 클래스 따라하며 배우는 C++ 17. string 문자열 클래스 17.1 std::string과 std::wstring #include #include #include #include using namespace std; int main() { // c-style string example //{ //char* strHello = new char[7]; //strcpy_s(strHello, sizeof(char) * 7, "hello!"); //std::cout
따라하며 배우는 C++ 16. 표준 템플릿 라이브러리 따라하며 배우는 C++ 16. 표준 템플릿 라이브러리 16.1 표준 템플릿 라이브러리와 컨테이너 소개 reference가 가장 잘 나와 있는 사이트는 cppreference.com #include #include #include #include #include #include #include #include using namespace std; void sequence_containers() { //vector { vector vec; //#include for (int i = 0; i < 10; ++i) vec.push_back(i); for (auto& e : vec) cout
따라하며 배우는 C++ 15. 의미론적 이동과 스마트 포인터 따라하며 배우는 C++ 15. 의미론적 이동과 스마트 포인터 15.1 이동의 의미와 스마트 포인터 #include #include "Resource.h" using namespace std; //RAII : resource acquisition is initialization //RAII = new한 곳에서 delete를 해 주어야 한다는 원칙. void doSomething() { Resource* res = new Resource; //work with res delete res; //이렇게 할 경우 early return, exception을 하면 //delete가 중복될 수도 있고, 까먹을 수도 있다. return; } int main() { doSomething(); return 0; } 여기서..
따라하며 배우는 C++ 14. 예외 처리 따라하며 배우는 C++ 14. 예외 처리 14.1 예외처리Exception Handling의 기본 #include #include #include using namespace std; int findFirstChar(const char* string, char ch) { for (size_t index = 0; index < strlen(string); ++index) { if (string[index] == ch) return index; } return -1; } double divide(int x, int y, bool& success) { if (y == 0) { success = false; return 0.0; } success = false; return static_cast(x) / y; ..
따라하며 배우는 C++ 13. 템플릿 따라하며 배우는 C++ 13. 템플릿 13.1 함수 템플릿 int getMax(int x, int y) { return (x > y) ? x : y; } double getMax(double x, double y) { return (x > y) ? x : y; } //이렇게 형이 여러 개일 경우 템플릿이 유용하다. #include using namespace std; template //typename 대신 class를 쓸 수도 있다. //늬앙스가 약간 다를 뿐 거의 비슷한 기능을 한다. T getMax(T x, T y) { return (x > y) ? x : y; } int main() { cout
따라하며 배우는 C++ 12. 가상 함수들 따라하며 배우는 C++ 12. 가상 함수들 12.1 다형성의 기본 개념 #include using namespace std; class Animal { protected: string m_name; public: Animal(std::string name) : m_name(name) {} public: string getName() { return m_name; } void speak() const { cout
따라하며 배우는 C++ 11. 상속 11. 상속 11.1 상속의 기본(1) Inheritance (is-a relationship) #include using namespace std; class Mother { private: int m_i; public: Mother(const int& i_in) : m_i(i_in) { cout
따라하며 배우는 C++ 10. 객체 사이의 관계들에 대해 따라하며 배우는 C++ 10. 객체 사이의 관계들에 대해 10.1 객체들의 관계 구성(요소) : Composition, Part-of -- 관계와 객체가 분리될 수 없음. 집합 : Aggregation, Has-a -- 연결이 느슨함. 관계와 객체가 분리될 수 있음. 연계, 제휴: Association, Uses-a -- 단순히 사용하는 관계 의존 : Dependency, Depends-on 10.2 구성 관계 Composition Monter.h #pragma once #include "Position2D.h" #include class Monster { private: std::string m_name; //string = char * data, unsigned int length //location..