분류 전체보기175 [C++] Standard Template Library Container: Map (ENG) Creating Map A map stores pairs of key and value. Keys cannot be duplicated. C++ maps are automatically sorted. It is based on the binary search tree (ascending order). #include int main() { std::map simpleScoreMap; simpleScoreMap.insert(std::pair("Mocha", 100)); simpleScoreMap.insert(std::pair("Coco", 50)); simpleScoreMap["Mocha"] = 0; std::cout 2022. 1. 2. [C++] Standard Template Library Container: Vector (ENG) STL Container vector, map, set, stack, queue, list, deque ... STL container is a standard interface for all template-based containers. It allows for automatic memory management. Creating Vectors Vector is a dynamic array that can hold any data type. All elements in a vector are located in contiguous memory. Memory is automatically managed as the number of elements increase. You can randomly acce.. 2022. 1. 2. [C++] Exception (ENG) Out Of Range #include int main() { std::string myCatName = "Coco"; try { char ch = myCatName.at(5); } catch (const std::out_of_range& e) { std::cerr GetName(); std::cout 2022. 1. 2. [C++] Static Keyword (ENG) static and extern static keyword makes global variables with limited scope. Scope of the file, the namespace, the class, and the function. extern keyword allows access to global variables in other files. // ExternTest.h extern int globalValue; void IncreaseValue(); // ExternTest.c #include "ExternTest.h" int globalValue = 2; // if this line contains static keyword, the main functions cause linke.. 2022. 1. 2. [C++] Inline Functions (ENG) Inline Functions Creating new functions is not always good. It is slow, not optimized for the CPU cache. Sometimes, functions are good for readability. But if the calculations are simple, the overheads of calling the functions are burdensome. So that's why you should use inline functions. // Member Inline Function // Animal.h class Animal { public: Animal(int age); inline int GetAge() const; } i.. 2022. 1. 2. [C++] Casting (ENG) C-style Casting C-style Casting is an implicit casting. int score = (int)someVariable; static_cast For Values: static_cast converts two numeric types. It tries to keep the same value between before and after the conversion. So the binary representations can be different. int number1 = 3; // 0000 0000 0000 0011 short number2 = static_cast(number1); // 0000 0011 float number1 = 3.f; // 0100 0000 0.. 2022. 1. 2. [C++] Object-Oriented Programming: Interface (ENG) Multiple Inheritance // Faculty.h class Faculty { }; // Student.h class Student { }; // TA.h class TA : public Student, public Faculty { }; // Main.cpp TA* myTA = new TA(); The constructors are called in the order of the parent class in the child class declaration. (Regardless of the order of the initialiation list.) problem 1: Compiler doesn't know which function is supposed to be called. // Ma.. 2022. 1. 2. [C++] Object-Oriented Programming: Polymorphism (ENG) Memory of the Member Function Member functions are allocated to the memory only once while compliling. Function Overriding // Animal.h class Animal { public: void Speak(); } // Animal.cpp void Animal::Speak() { std::cout 2022. 1. 2. [C++] Object-Oriented Programming: Inheritance (ENG) Difference between Java and C++ // Java public class Cat extends Animal { private String Name; public Cat(int age, String name) { super(age); // calls Animal(int age) Name = name; } } // C++ class Cat : public Animal { public: Cat(int age, const char* name); private: char* mName; }; Cat::Cat(int age, const char* name) : Animal(age) { size_t size = string(name) +1; mName = new char[size]; strcpy(.. 2022. 1. 2. 이전 1 ··· 7 8 9 10 11 12 13 ··· 20 다음