본문 바로가기

C++22

[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++] 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: 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.
[C++] Object-Oriented Programming : Class (ENG) Access Modifier public : accessible to anyone protected : accessible from child classes private : accessible only from the class (not the object) The default for access permissions is private. class SomeClass { public: int PublicMember; protected: int mProtectedMember; // m for member variables private: int mPrivateMember1; int mPrivateMember2; }; Create and Delete an Object // create in stack m.. 2022. 1. 2.
[C++] File I/O (ENG) File Stream ifstream : input file ofstream : output file fstream : input and output file We can use operator and a manipulator in a file stream. // read only open ifstream fin; // an object fin.open("helloWorld.txt"); // write only open ofstream fout; fout.open("helloWorld.txt"); // read and write fstream fs; fs.open("helloWorld.txt"); open( ) Every stream has open( ) method. fin.open("HelloWorl.. 2022. 1. 2.
[C++] File I/O (ENG) File Stream ifstream : input file ofstream : output file fstream : input and output file We can use operator and a manipulator in a file stream. // read only open ifstream fin; // an object fin.open("helloWorld.txt"); // write only open ofstream fout; fout.open("helloWorld.txt"); // read and write fstream fs; fs.open("helloWorld.txt"); open( ) Every stream has open( ) method. fin.open("HelloWorl.. 2022. 1. 2.
[C++] String (ENG) std::string Class A string is not an array, but a class in C++. A string using a std::string class can be increased in length. #include std::string name; std::cin >> name; also possible to assign and append. It is intuitive and secure. string firstName = "Pury"; string fullName = "Onion"; // assignment fullName = firstName; // appending fullName += "Bab"; concatenation: string firstName = "Pury".. 2021. 12. 5.