프로그래밍/C++ 9

string 사용법 정리

operator[], at(), front(), back() size(), length(), capacity(), resize(), shrink_to_fit(), reserve(), clear(), erase(), empty() c_str(), substr(), replace(), compare(), copy(), find(), insert(), push_back(), pop_back() begin(), end() swap(), operator+ string substr(size_t index = 0, size_t len = npos) const; 문자열을 잘라서 반환한다. s2 = s1.substr(); // 처음부터 끝까지 s2 = s1.substr(3); // 3부터 끝까지.. s2 = s1.sub..

프로그래밍/C++ 2023.11.13

const, static 멤버 변수,함수

const 멤버 변수 상수를 저장하는 변수이며, 생성과 동시에 초기화 리스트롤 이용해서 초기화되어야 한다. class MyClass { private: const int a; public: MyClass(int a) : a(a) {} } MyClass myclass(5); static 멤버 변수 클래스 변수라고도 하며, 클래스의 객체들이 공유하는 변수이다. 메모리에 한번만 할당되어 공유되며, 별도 정의를 통해 초기화 해야한다. class MyClass { private: static int a; } int MyClass::a = 0; const static 멤버 변수 클래스의 객체들이 공유하는 상수이다. 이니셜라이저가 아닌 다음과 같이 선언과 동시 초기화가 가능하다. class MyClass { publ..

프로그래밍/C++ 2023.10.22

참조자

참조자란 변수에 대한 또하나의 이름이다. 선언과 동시에 참조해야한다. NULL로 초기화 할 수 없다. 참조자를 반환하는 함수인 경우 지역변수를 반환하지 않도록 한다. int a = 10; int& b = a; int Function(int& a); int& Function(int& a); const 참조자는 상수를 참조할 수 있다. ( 함수 매개변수로 const 참조자를 쓸 수 있다. ) const int &a = 30; int Function(const int& a); //참조자a가 가리키는 값을 변경하지 않는다.

프로그래밍/C++ 2023.10.22

Const 정리

Const 란 Constant의 약자로, 변수를 상수화 하고자 할때 사용하는 키워드이다. const 위치에 ('*'의 왼쪽, 오른쪽) 따라서 상수화 대상을 변경할 수 있다. [포인터가 참조하는 값을 상수화] const int* ptr = &value; ptr = &value2; // ok *ptr = 10; // error [포인터 변수를 상수화] int* const ptr = &value; ptr = &value2 // error *ptr = 10; // ok const 객체 객체를 const로 생성하면 const 멤버함수만 호출할 수 있다.(객체 내 데이터를 수정할 수 없기 떄문) class MyClass { public: void AddNum(int num) const; void AddNum2(in..

프로그래밍/C++ 2023.10.08

함수 오버로딩

함수 오버로딩이란 매개변수만 다른 동일한 함수명을 여러개 정의할 수 있는 기능이다. C에서는 복수 함수명을 허용하지 않지만 C++은 함수 오버로딩을 지원한다. 매개변수의 자료형 및 개수가 달라야한다. int Function(int a); int Function(int a, int b); int Function(double a, double b); const 여부로 오버로딩이 가능하다. (단, 매개변수가 포인터 또는 참조형 이어야한다.) void Function(char* a); void Function(const char* a); const 함수는 const 객체에서만 호출할 수 있다. class MyClass { void Function(char* a) { } void Function(char* a) ..

프로그래밍/C++ 2023.10.04