목록프로그래밍언어/C++ (55)
nakka soft world !
함수 바인딩과 가상함수의 원리 함수 바인딩 (function binding)class Animal{public:void Cry() {}};class Dog : public Animal{public:void Cry() {}}; int main(){Animal* p = 0;p = new Dog; int n;cin >> n; if (n == 1)p = new Animal; // 이순간 컴파일러는 p가 누구를 가리키는지 알수 있을까? p->Cry(); // 함수 바인딩 (function binding)} // 1. static binding : 컴파일 시간에 컴파일러가 결정하는 것. 포인터 타입으로 결정. Animal Cry 호출. 속도가 빠름.// C++ 일반 멤버 함수.// 2. dynamic binding..
추상클래스와 인터페이스 Interface class Camera{public:void startRecord() { cout
순수 가상 함수와 추상 클래스 // 추상 클래스class Shape{public:virtual void Draw() = 0; // Pure virtual function, 구현부가 없다.-> 반드시 해당 함수는 포함하라는 의도.} class Rect : public Shape{public:virtual void Draw() { } // Draw의 구현부가 없으면 역시 추상 클래스}; int main(){Rect r; // errorShape s; // error, 추상클래스는 객체를 만들수 없다s.Draw(); Shape* p; // OK}
가상 소멸자 ( virtual destructor )class Base{public:void foo() { cout
#include #include using namespace sd; // #1. 각도형을 타입으로 만들면 편리 하다.class Rect{public:void Draw(){ cout
Upcasting class Animal{public:int age;string name;};class Dog : public Animal{public:int color}; int main(){double d = 3.4;int* pn = &d; // error. void ptr은 가능 Dog dogAnimal*p = &dog; // OK. 기반 클래스 포인터에 파생클래스 주소를 가르킬수 있다.// upcasting p->age = 2;p->name = "kim"; p->color = 2; // error// 기반 클래스 포인터로는 기반클래스 멤버만 접근 할수 있다. Dog pDog = static_casting(p);pDog->color = 2; // ok} Upcasting 활용class Animal{..
상속(inheritance)의 개념 객체지향 프로그래밍의 3가지 특징은 "캡슐화(encapsulation)", "상속성(inheritance)", "다형성(polymorphism)"입니다.// Base, Superclass People{string name;int age;};// Derived, Sub classclass Student : public People // 상속(inheritance){int id;};class Professor : public People{ int major;};int main(){} class Base{private:int a; // 자신의 멤버 함수와 friend함수만 접근 protected:int b; // 자신의 멤버 함수와 friend함수만 접근 // 파생클래스의 ..
STL 설계 철학 컨테이너(자료 구조)listvectortreehash 반복자(iterator)컨테이너의 내부구조에 상관없이 동일한 방식으로 요소를 열거--------------------------------------> 알고리즘(일밤함수)선형검색이진검색정렬순열 정책변경함수, 함수객체, 람다 표현식
알고리즘의 정책 변경 1. 함수, 함수 객체, 람다 표현식 #include #include #include #include // STL의 함수 객체 사용 using namespace std; bool compare( int a, int b) ( retrun a > b; } int main(){vector v = {1,2,3,4,5,6,7,8,9,10}; sort(v.begin(), v.end()); // 크기비교 < for (auto n : v)cout
STL 알고리즘 ( algorithm ) 1. 일반 함수 #include #include #include #include using namespace std; int main(){list s = {1,2,3,4,5,6,7,8,9,10}; vector v = {1,2,3,4,5,6,7,8,9,10}; //s.find(5);//v.find(5); // 같은 알고리즘을 컨테이너마다 가지고 있어야 하나는 의문으로 멤버 함수로 있지 않고 일반 함수로 있음. // 핵심 1. 멤버 함수가 아니고 일반함수 이다. find(s.begin(), s.end(), 5);find(v.begin(), v.end(), 5); } 2. 검색 리턴 값 int main(){vector v = {1,2,3,4,5,6,7,8,9,10};v..