목록분류 전체보기 (257)
nakka soft world !
다중 상속 (multiple inheritance) struct A{int a;};struct B{int a;int b;};struct C : public A, public B{int a;int b;int c;}; int main(){C ccc; ccc.a = 10; // errorccc.A::a = 10; // okccc.B::a = 10;} 다이아몬드 상속 struct X{int x;};struct A : virtual public X // 가상상속, x 가 메모리 영역에 두번 들어 가지 앟고 한번만 들어 감{int a;};struct B : virtual public X // 가상상속, x 가 메모리 영역에 두번 들어 가지 앟고 한번만 들어 감{int b;};struct C : public A, p..
RTTI ( Rut Time Type Information ) #include void foo(Animal* p){// p가 실제로 어떤 객체를 가리킬까?// RTTIconst type_info& t1 = typeid(*p);const type_info& t2 = typeid(Dog); if (t1 == t2) {} if(typeid(*p) == typeid(Dog)){// p를 Dog타입으로 캐스팅 해서 사용Dog* pDog = static_cast(p);// pDog->Dog고유 멤버 = 10;}} int main(){Animal a;foo(&a); Dog d;foo(&d);} RTTI는 사용하지 않는 것이 좋은 디자인이다. dynamic_cast class Animal{public:int age;v..
함수 바인딩과 가상함수의 원리 함수 바인딩 (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함수만 접근 // 파생클래스의 ..