Skip to content

Part 3: Object-Oriented Programming Intermediate

Từ tư duy thủ tục sang tư duy đối tượng — Mô hình hóa thế giới thực bằng C++ Classes

Tại sao OOP?

OOP không chỉ là "cách viết code khác" — nó là cách tư duy về phần mềm. Thay vì nghĩ "làm gì" (functions), ta nghĩ "ai làm gì" (objects).

┌─────────────────────────────────────────────────────────────────┐
│              PROCEDURAL vs OOP THINKING                         │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  PROCEDURAL (C-style)          OOP (C++ Classes)               │
│  ─────────────────────         ─────────────────               │
│                                                                 │
│  struct Player {               class Player {                  │
│    int health;                 private:                        │
│    int mana;                     int health_;                  │
│  };                              int mana_;                    │
│                                public:                         │
│  void attack(Player* p,          void attack(Enemy& target);  │
│               Enemy* e);         void heal(int amount);       │
│  void heal(Player* p,          };                             │
│             int amount);                                       │
│                                                                 │
│  // Data và functions tách rời  // Data + behavior = Object   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Module Structure

📘 Part 1: Encapsulation & Abstraction

TopicDescription
Tại sao OOP?Philosophy, mapping real world to code
Class AnatomyMembers, Methods, Constructor/Destructor
Access Modifierspublic, private, protected, Data Hiding
The this PointerWhat it is and when to use
Member InitializationModern initialization list
🎮 RPG Character ProjectPractical example

📙 Part 2: Inheritance & Polymorphism

TopicDescription
Inheritance BasicsBase/Derived classes, access specifiers
Virtual Functionsvirtual, override, final, vtable
PolymorphismRuntime dispatch, OCP principle
Abstract ClassesPure virtual, interfaces
🎮 RPG Class SystemWarrior/Mage/Rogue implementation

Prerequisites

Trước khi bắt đầu Part 3, bạn cần nắm vững:

📋 YÊU CẦU


Learning Path

Part 1: Encapsulation

  1. 🎯 Tại sao OOP? — Philosophy
  2. 🏗️ Class Anatomy — Cấu trúc class
  3. 🔒 Access Modifiers — Data hiding
  4. 👆 The this Pointer — Self-reference
  5. Member Initialization — Modern patterns
  6. 🎮 RPG Character Project — Thực hành

Part 2: Inheritance

  1. 🧬 Inheritance — Base/Derived classes
  2. 🎭 Virtual Functions — Runtime dispatch
  3. 🔮 Polymorphism — One interface, many forms
  4. 📐 Abstract Classes — Interfaces
  5. 🎮 RPG Class System — Warrior/Mage/Rogue

"Object-oriented programming lets you think in terms of problems rather than the machine." — Alan Kay