البرمجة كائنية التوجه: الفئات والكائنات في C++: دليل مفصل
What is it and why? (a real-life analogy)
Imagine you are an architect who needs to build 1000 identical houses. What will you do? Will you come up with a new blueprint, calculate the number of bricks, and draw a plan from scratch every time? Of course not! You will create one blueprint (a house design), and then the builders will simply take that blueprint and build 1000 real houses from it.
The same thing happens in programming. A class is exactly that "blueprint" or "template". It describes what properties (characteristics) an object will have and what actions it can perform. An object is the real "house" built from that blueprint. Each object has its own data (for example, its own wall color, its own address), but they all work according to the same rules defined in the class.
Let's consider another analogy — cookies and a cookie cutter. The cookie cutter is the class. It defines the shape, size, and pattern. Each specific cookie you cut out with that cutter from the dough is an object. The dough can be different (chocolate, vanilla), but the shape of all cookies will be the same because it is defined by the cutter.
💡 Important to remember: A class is a data type that you create yourself. An object is a variable of this type. If int is a built-in type for integers, then your class is your own custom type for anything: for a Student, for a Car, for a Game Character.
Why is this needed? Object-Oriented Programming (OOP) helps to:
- Simplify complex programs. Instead of keeping a thousand variables in your head, you work with understandable objects: "Student takes an exam", "Car drives", "Order is being processed".
- Reuse code. Write a "User" class once — use it in a hundred projects.
- Easily make changes. Need to add a new property to all users? Change the class — and all objects automatically get that property.
- Model the real world. The program becomes like a description of reality, where there are entities (objects) that interact with each other.
Theory: how it works internally
Let's look "under the hood" and understand what happens in the computer's memory when we create classes and objects.
What does a class contain?
Any class consists of two main parts:
- Fields (properties, attributes, data) — these are variables that store the state of the object. For example, for the "Student" class, the fields would be: name, age, group number, GPA.
- Methods (member functions, behavior) — these are functions that can perform actions with the object's data. For example: "study()", "take_exam()", "get_scholarship()".
Access modifiers: who can see?
In C++, there are three important keywords that determine who can access the fields and methods of a class:
| Modifier | Access | Analogy |
|---|---|---|
public |
Accessible to everyone: from any part of the program | Shop window — everyone can see |
private |
Accessible only within the class itself | Safe in the back room — only the owner has access |
protected |
Accessible within the class and in derived classes | Warehouse for employees and their children |
⚠️ Most important rule: All class fields should be private! Access to them is done through special public methods (getters and setters). This is called encapsulation — hiding the internal structure of an object.
How does an object live in memory?
When you create an object, the compiler allocates exactly as much memory as needed to store all its fields. Methods are not stored in the object's memory — they exist in a single instance for the entire class. When you call a method on a specific object, the compiler implicitly passes a pointer to that object into the method (the hidden this parameter).
For example, if you have a class:
class Point {
private:
int x; // 4 bytes
int y; // 4 bytes
public:
void show() { /* ... */ }
};
Each object of the Point class will occupy 8 bytes (4 bytes for each field). The show() method exists in a single instance and knows which object to work with thanks to the hidden this pointer.
Syntax in C++: all forms and variations
Now let's move to practice. Here is what a class declaration looks like in C++:
// The keyword class, followed by the class name (capitalized)
class Student {
private: // Private member section (accessible only within the class)
// Fields (properties) of the class
std::string name;
int age;
double gpa;
public: // Public member section (accessible to everyone)
// Constructor — a special method called when an object is created
Student(std::string student_name, int student_age) {
name = student_name;
age = student_age;
gpa = 0.0; // Initial value
}
// Method (member function) — an action the object can perform
void study() {
std::cout << name << " is studying!" << std::endl;
}
// Getter — a method for getting the value of a private field
double getGpa() {
return gpa;
}
// Setter — a method for changing the value of a private field
void setGpa(double newG