Many beginners in C++ often feel confused: “What exactly are classes and objects? How do they differ from the procedural approach of C language?” In fact, you don’t need to memorize concepts rigidly; you can easily understand the core logic of object-oriented programming through something we do every day – “playing games”. Today, I will use the most straightforward examples to introduce you to C++ classes and objects.
1. First, Understand: Procedural vs. Object-Oriented, the Difference is Not in “Steps” but in “Thinking”
C++, as an object-oriented programming language, fundamentally differs from C language (which is procedural) in the “perspective” of problem-solving. Let’s intuitively compare the two through the scenario of “playing a new game”:
1. Procedural: Like “Unpacking a Package Step by Step”, Emphasizing “Order”
If you approach playing a game with the procedural mindset of C language, you need to break the entire process down into steps executed in order, just like unpacking a package requires first finding scissors, then tearing tape, and finally taking out the items:
- Open the app store → Search for the game name
- Click download → Wait for installation to complete
- Register an account → Log into the game
- Select a server → Create a character
- Click “Start Game” → Control the character to complete tasks
Each step relies on the result of the previous one; if any step fails (for example, if the download fails), the entire process gets stuck. This mindset is more suitable for simple, fixed-step tasks, but when faced with complex scenarios (like playing multiple games simultaneously or managing game saves), the steps become chaotic and hard to maintain.
2. Object-Oriented: Like “Finding Specialists for Specific Tasks”, Emphasizing “Objects”
In contrast, the object-oriented mindset of C++ breaks the entire scenario down into “objects with attributes and behaviors”, where each object is responsible for its own tasks, just like you don’t have to run through the process yourself but instead find “game APP”, “account system”, and “character” as “specialists” to collaborate:
- “Game APP” Object: Attributes (name, size, version), Behaviors (download, install, start, update)
- “Account System” Object: Attributes (username, password, level), Behaviors (register, login, recover password)
- “Game Character” Object: Attributes (nickname, profession, health), Behaviors (move, attack, cast skills)
You don’t need to worry about “how to install after downloading”; you just need to tell the “Game APP” object “I want to start”; you also don’t need to care about “how to verify the password during login”; you just let the “Account System” object handle it – each object encapsulates its own logic, and you only need to call their “behaviors” to complete tasks.The core advantage of this mindset is **”Encapsulation”**: the internal logic of an object (like how the game APP implements downloading) does not need to be exposed externally; it only needs to provide simple interfaces like “start” and “update”, which simplifies operations and makes the code easier to maintain.
2. Classes and Objects: Not “Parent and Child”, but “Blueprint and House”
Once you understand the object-oriented mindset, understanding “classes” and “objects” becomes very simple – their relationship is like “architectural blueprints” and “the actual house built”:
1. Class: The “Blueprint” of an Object
A “class” (Class) is an abstract description of the common attributes and behaviors of a certain type of thing, just like architectural blueprints specify the layout of a house (attributes: number of rooms, area) and functions (behaviors: living, storage), but the blueprint itself is not a house and cannot be lived in directly.
For example, if we define a “Game APP” class, it will include:
Attributes (data): Game Name (string), Installation Size (int, in MB), Current Version (string) Behaviors (functions): Download (download ()), Install (install ()), Start (start ()), Update (update ())
In C++ code, it looks like this (don’t worry about syntax details, just focus on the logic):
// GameAPP class: describes the common attributes and behaviors of "Game APP"class GameApp { // Attributes (the "features" of the object) string name; // Game Name int size; // Installation Size (MB) string version; // Current Version // Behaviors (what the object can do) void download() { // Download game cout << "Downloading " << name << "..." << endl; } void install() { // Install game cout << name << " installation complete, size: " << size << "MB" << endl; } void start() { // Start game cout << "Starting " << name << " V" << version << endl; }};
2. Object: The “Actual Product” of a Class
An “object” (Object) is a concrete instance created based on a class, just like a house built according to blueprints – each house conforms to the specifications of the blueprints but has its own details (like decoration style, residents); each object also conforms to the class definition but has its own attribute values.
For example, based on the “GameApp” class, we can create two specific objects:
"Honor of Kings" object: Attributes (name = Honor of Kings, size=10000, version=3.81.1.8), can execute download (), start () and other behaviors"Genshin Impact" object: Attributes (name = Genshin Impact, size=15000, version=4.2.0), can also execute these behaviors, but will use its own attribute values during execution
Creating objects and calling behaviors in C++ code looks like this:
int main() { // Create "Honor of Kings" object based on GameApp class GameApp honorOfKings; honorOfKings.name = "Honor of Kings"; honorOfKings.size = 10000; honorOfKings.version = "3.81.1.8"; // Call object's behaviors honorOfKings.download(); // Output: Downloading Honor of Kings... honorOfKings.start(); // Output: Starting Honor of Kings V3.81.1.8 // Create "Genshin Impact" object GameApp genshinImpact; genshinImpact.name = "Genshin Impact"; genshinImpact.size = 15000; genshinImpact.version = "4.2.0"; genshinImpact.install(); // Output: Genshin Impact installation complete, size: 15000MB return 0;}
In simple terms:A class is a “template”, and an object is “something specific made from that template” – one class can create countless objects, just like one blueprint can build countless houses.