티스토리 뷰

#include <stdio.h>


class Human {

public:

        int age;


        virtual void printme() {

                printf("age human: %d\n", age);

        }

};


class Person : public Human {

        virtual void printme() {

                printf("age Person: %d\n", age);

        }

};


class Monkey {

public:

        int age;

        int position;   


        virtual void printme() {

                printf("age monkey: %d\n", age);

        }

};


int main() {

        // unpredictable

        Human* human = new Human();

        Monkey* monkey = (Monkey*) human;


        human->age=100;

        printf("monkey?? age??: %d\n",monkey->age);

        monkey->printme();


        // predictable

        human = new Person();

        human->printme();


        return 0;

}



결과
monkey?? age??: 100
age human: 100
age Person: 0

'Programing > C/C++' 카테고리의 다른 글

자료 오버 플로우 실험  (0) 2012.07.12
메모리 최소 접근 가능  (0) 2012.07.12
메모리 주소 엑세스 관련 간단 예제  (0) 2012.05.10
Circula Queue(String)  (0) 2012.05.10
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함