본문 바로가기

C++

(9)
[C++] 다중 상속(multiple inheritance) 이번 시간에는 다중 상속에 대해서 알아보겠습니다. #include class A { public: int a; A() { std::cout
[C++] 추상 클래스 (abstract class) 이번 시간에는 추상 클래스에 대해서 알아보겠습니다. #include class Animal { public: Animal() { std::cout
[C++] 상속(inheritance) - up casting, virtual up casting - 파생클래스의 인스턴스를 생성한 후, 기반 클래스의 포인터로 캐스팅하는 경우 #include #include class Base { std::string s; public: Base() : s("기반") { std::cout
[C++] 상속(inheritance) - protected, private 키워드 이번 시간에는 상속할 때 특정 변수를 보호하는 방법중에 하나인 protected 에 대해서 알아보겠습니다. 파생 클래스에서 기반 클래스 변수를 변경하려고 하면 기반 클래스의 해당 변수를 protected 라고 선언하면 변경이 가능하다. #include class Base { protected: // std::string parent_string; public: Base() : parent_string("기반") { std::cout
[C++] 상속(inheritance) 이번 시간에는 상속에 대해서 알아보겠습니다. 아래는 간단한 상속 관계의 샘플 코드입니다. #include class Base { std::string s; public: Base() : s("기반") { std::cout
[C++] 포인터 - 연산자 오버로딩(overloading) 이번에는 연산자 오버로딩에 대해서 아래의 샘플코드를 통해서 알아보자. #include class Complex { private: double real, img; double get_number(const char *str, int from, int to) const; // 함수에 const 를 사용하면 해당 함수내부에서는 멤버 변수값을 변경못함. public: Complex(double real, double img) : real(real), img(img) {} Complex(const Complex &c) { real = c.real, img = c.img; } Complex(const char *str); // a@b 는 a.operator@(b) 또는 operator@(a,b) 로 해석이 가능하..
[C++] 포인터 - 얕은복사/깊은복사(샘플3), 생성자, 소멸자 #include class string { char *str; int len; public: string(char c, int n); // 문자 c 가 n 개 있는 문자열로 정의 string(const char *s); // 일반 생성자 string(const string &s); // 복사 생성자 ~string(); // 소멸자 void add_string(const string &s); // str 뒤에 s 를 붙인다. void copy_string(const string &s); // str 에 s 를 복사한다. int strlen(); // int GetLen() { return len; } // char *GetStr() { return str; } void show(); }; int strin..
[C++] 포인터 - 얕은복사/깊은복사(샘플2) 포인터의 개념중에서 얕은복사, 깊은 복사에 대한 추가 샘플 코드입니다. #include class string { char *str; int len; public: string(char c, int n); // 문자 c 가 n 개 있는 문자열로 정의 string(const char *s); string(const string &s); ~string(); void add_string(const string &s); // str 뒤에 s 를 붙인다. void copy_string(const string &s); // str 에 s 를 복사한다. int strlen(); int GetLen() { return len; } char *GetStr() { return str; } // 문자열 길이 리턴 }; in..