전체 글

전체 글

    [C++] 연산자 재정의 (operator overloading)

    [C++] 연산자 재정의 (operator overloading)

    1. 연산자 재정의 기본 개념 #include class Point { int x; int y; public: Point( int a = 0, int b = 0 ) : x(a), y(b) {} }; int main() { int n = 1 + 2; // 3 Point p1(1, 1); Point p2(2, 2); Point p3 = p1 + p2; // ? // operator+(p1, p2) -> operator+(Point, Point) } ① +, -, *, 등의 연산자도 함수로 만들 수 있다. => operator+, operator-, operator* 2. a + b 를 컴파일러가 해석하는 방법 ① a, b가 모두 primitive type (int, double 등) 인 경우 => 일반적인 ..

    [C#] 기본 코드

    [C#] 기본 코드

    1. 핵심 정리 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IO { class Program { static void Main(string[] args) { } } } ① Main 함수도 클래스의 멤버이다. => static method로 작성 ② Main 함수의 모양 => static void Main() => static void Main(string[] args) => static int Main() => static int Main(string[] args) ③ 용어 => 멤버 함수 → 메소드 => 멤버 데이터 → 필드

    [C++] 다중 상속 (multiple inheritance)

    [C++] 다중 상속 (multiple inheritance)

    1. 핵심 정리 class InputFile { public: void read() {} void open() {} }; class OutputFile { public: void write() {} void open() {} }; class IOFile : public InputFile, public OutputFile { }; int main() { IOFile file; file.open(); // error file.InputFile::open(); // ok } ① 다중 상속 이란? (multiple inheritance) => 클래스가 2개 이상의 기반 클래스로부터 상속 되는 것 => C++, Lisp, Curl 등은 다중상속을 지원하지만 Java, C#등은 다중 상속을 지원하지 않는다. ② 다..

    [C++] RTTI (Run Time Type Information)

    [C++] RTTI (Run Time Type Information)

    1. 핵심 개념 #include #include int main() { int n1 = 10; auto n2 = n1; // n2의 타입은? int const std::type_info& t1 = typeid(n2); std::cout typeid 연산자의 결과로 const std::type_info&가 반환된다. ② std::type_info => 타입의 정보를 담고 있는 클래스 => 사용자가 직접 객체를 만들 수는 없고, typeid() 연산자를 통해서만 얻을 수 있다. => 멤버 함수인 name을 통해서 타입의 이름을 얻을 수 있다. 4. 타입 조사, 비교 #include #include int main() { auto n1 = 10; const std::type_info& t1 = typeid(n..

    [C++] 인터페이스 (interface)

    [C++] 인터페이스 (interface)

    1. 인터페이스 (interface) 개념 #include class Camera { public: void take() { std::cout 모든 카메라는 ICamera로부터 파생 되어야 한다. #include class ICamera { public: virtual void take() = 0; }; class People { public: void useCamera(ICamera* p) { p->take(); } }; int main() { } ③ 카메라 사용자 (People 클래스) => 규칙대로만 사용하면 된다. => 순수 가상 함수로 되어 실물 카메라가 없어도 People클래스를 먼저 만들 수 있다. ④ 모든 카메라 제작자 (Camera 클래스) => 반드시 규칙을 지켜야 한다. #includ..

    [C++] 추상 클래스 (abstract class)

    [C++] 추상 클래스 (abstract class)

    1. 추상 클래스 (abstract class) class Shape { public: virtual void Draw() = 0; }; class Rect : public Shape { public: virtual void Draw() { } // 구현 }; int main() { Shape s; // error Shape* p; // ok Rect r; // 구현부가 있으므로 ok } ① 순수 가상 함수 (pure virtual function) => 함수의 구현부가 없고, 선언부가 =0 으로 끝나는 가상함수 ② 추상 클래스 (Abstract Class) => 순수 가상 함수가 한 개 이상 있는 클래스 ③ 추상 클래스 특징 => 객체를 생성할 수 없다. => 포인터 변수는 만들 수 있다. ④ 추상 클..