C++ 구성 관계 (Composition Relationship)
·
SW개발/C++
구성 관계 (Composition Relationship) 몬스터 클래스와 그의 멤버인 좌표 클래스로 비유했다. Monster 클래스의 멤버로 존재하는 Position2D 클래스 인스턴스 location_ (Monster.h 참고) 이 location_ 인스턴스는 Monster 클래스로 만들어진 mon1 인스턴스의 Part-of라고 할 수 있다. mon1이 소멸하면 자동으로 location_도 소멸한다. main.cpp #include "Monster.h" int main() { using namespace std; Monster mon1("Sanson", Position2D(0, 0)); cout
C++ 객체들의 관계 (Object Relationship)
·
SW개발/C++
객체들의 관계 (Object Relationship) 관계 관계를 표현하는 동사 예시 구성 (Composition) Part-of 두뇌는 육체의 일부이다 집합 (Aggregation) Has-a 어떤 사람이 자동차를 가지고 있다. 연계 (Association) Uses-a 환자는 의사의 치료를 받는다. 의사는 환자들로부터 치료비를 받는다. 의존 (Dependency) Depends-on 나는 목발을 짚었다. 관계 관계의 형태 다른 클래스에도 속할 수 있는가 멤버의 존재를 클래스가 관리하는가 방향성 구성 (Composition) 전체/부품 No Yes 단방향 집합 (Aggregation) 전체/부품 Yes No 단방향 연계 (Association) 용도 외 무관 Yes No 단방향, 양방향 의존 (Depe..
따라하며 배우는 C++ 10장
·
SW개발/C++
따라하며 배우는 C++ 10장 객체들의 관계 (Object Relationship) IntArray 컨테이너 만들어보기 참고 따라하며 배우는 C++
C++ initializer_list
·
SW개발/C++
initializer_list C++11 라이브러리 라이브러리 버전에 따라 라이브러리에 들어가 있을 수도, 아닐 수도 있다고 한다. [] 연산자를 사용할 수 없다. foreach문은 내부적으로 iterator를 사용한다. 내부적으로 iterator를 사용하는 foreach문으로 반복문을 구성하면 된다. #include //#include class IntArray { unsigned len_ = 0; int* data_ = nullptr; public: IntArray(unsigned length) : len_(length) { data_ = new int[len_]; } IntArray(const std::initializer_list& list) : IntArray(list.size()) { int ..
C++ 대입 연산자 오버로딩 (Assignment Operator Overloading)
·
SW개발/C++
대입 연산자 오버로딩 (Assignment Operator Overloading) 자기 자신을 assignment할 때(hello = hello 등) 발생할 수 있는 문제를 미리 방지할 수 있다. 깊은 복사를 직접 구현할 수 있다. #include #include class MyString { char* data_ = nullptr; int len_ = 0; public: MyString(const char* source = "") { assert(source); len_ = std::strlen(source) + 1; data_ = new char[len_]; for (int i = 0; i < len_; ++i) data_[i] = source[i]; data_[len_ - 1] = &#39;\0&#..
C++ 형변환 오버로딩 (Typecasts Overloading)
·
SW개발/C++
형변환 오버로딩 (Typecasts Overloading) (int)객체, int(객체), static_cast(객체) 등의 형태로 형변환을 할 수 있게 해준다. 묵시적 형변환도 가능해진다. 예제 #include class Cents { int cents_; public: Cents(int cents = 0) { cents_ = cents; } operator int() { std::cout
C++ 괄호 연산자 오버로딩 (Parenthesis Operator Overloading)
·
SW개발/C++
괄호 연산자 오버로딩 (Parenthesis Operator Overloading) ()(parenthesis) 오버로딩 방법은 [](subscript) 연산자와 같다. Functor 객체를 함수처럼 사용하는 것 예제 #include class Accumulator { int counter_ = 0; public: int operator() (int i) { return (counter_ += i); } }; int main() { using namespace std; Accumulator acc; cout
C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)
·
SW개발/C++
첨자 연산자 오버로딩 (Subscript Operator Overloading) [](subscript operator) 내부에 숫자 뿐만 아니라 문자 등 다양한 자료형이 들어갈 수 있다. 예제 #include class IntList { int list_[10]; public: int& operator [] (const int index) { return list_[index]; } }; int main() { using namespace std; IntList my_list; my_list[3] = 1; cout
C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
·
SW개발/C++
증감 연산자 오버로딩 (Increment and Decrement Operator Overloading) ++ 연산자 오버로딩 예제 전위(prefix), 후위(postfix)에 따라 오버로딩 방식이 다르다. 클래스 내에서 정의하는 연산자 오버로딩은 파라미터를 받지 않으면 단항 연산자처럼 앞에 붙는 방식으로 오버로딩된다. (전위) 따라서 후위 연산자를 오버로딩하려면 int 자료형의 더미 변수가 있어야 한다. #include class Digit { int digit_; public: Digit(const int& digit) { digit_ = digit; } // prefix Digit& operator ++ () { ++digit_; return (*this); } // postfix Digit ope..