diff --git a/SampleCodes/Polymorphism/Sample of Polymorphism.cpp b/SampleCodes/Polymorphism/Sample of Polymorphism.cpp new file mode 100644 index 00000000..99a733b6 --- /dev/null +++ b/SampleCodes/Polymorphism/Sample of Polymorphism.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +// 20193496 ³ª¿ë¼º +// ´ÙÇü¼º, Ãß»ó Ŭ·¡½º¸¦ ÀÌ¿ëÇÑ »ùÇà ÄÚµå + +/* °£´ÜÇÑ ¿¹Á¦ Äڵ带 ÅëÇØ Àڽݴü¸¦ ÀÌ¿ëÇÏ¿© Ãß»ó ºÎ¸ð Ŭ·¡½ºÀÇ ¸â¹öº¯¼ö ¹× ¸â¹öÇÔ¼ö¸¦ + ¹®Á¦¾øÀÌ Ãâ·ÂÇÒ ¼ö ÀÖÀ½À» È®ÀÎÇßÀ¸¸ç ºÎ¸ðŬ·¡½ºÀÇ ¼ø¼ö°¡»óÇÔ¼ö¸¦ Ãâ·ÂÇÏ·ÁÇÏ´Ï + ¿¡·¯°¡ ³²À» È®ÀÎ ÇÒ ¼ö ÀÖ¾ú´Ù.*/ + +class ghost { +public: + int x = 5, y = 3; + + virtual void hide() { + cout << "¼ûÀ½" << endl; + } + virtual void move() = 0; +}; + +class monster : public ghost { +public: + int x = 7, y = 8; + + void hide() { + cout << "Å« ¹°Ã¼°¡ ¼ûÀ½" << endl; + } + void move() { + cout << "¿òÁ÷ÀÓ" << endl; + } +}; + +class zombie : public ghost { +public: + int x = 2, y = 2; + + void hide() { + cout << "ÀÌ»óÇÑ ¹°Ã¼°¡ ¼ûÀ½" << endl; + } + void move() { + cout << "´À¸®°Ô ¿òÁ÷ÀÓ" << endl; + } +}; + +int main() +{ + // ghost Ghost; -> °´Ã¼ »ý¼º ºÒ°¡ + + monster Monster; + + Monster.ghost::hide(); + // Monster.ghost::move(); -> ¹®¹ýÀûÀ¸·Î´Â ¿À·ù°¡ ¾ø¾úÀ¸³ª ÄÄÆÄÀÏ µµÁß "ÂüÁ¶ÇÒ ¼ö ¾ø´Ù" ¶ó´Â ¿À·ù°¡ ¶ä + Monster.hide(); + Monster.move(); + cout << Monster.ghost::x << Monster.y << endl; + + zombie Zombie; + + Zombie.ghost::hide(); + // Zombie.ghost::move(); -> ¹®¹ýÀûÀ¸·Î´Â ¿À·ù°¡ ¾ø¾úÀ¸³ª ÄÄÆÄÀÏ µµÁß "ÂüÁ¶ÇÒ ¼ö ¾ø´Ù" ¶ó´Â ¿À·ù°¡ ¶ä + Zombie.hide(); + Zombie.move(); + cout << Zombie.x << Zombie.ghost::y << endl; + + return 0; +} \ No newline at end of file