Classes and objects are the core features of Object-Oriented Programming (OOP). While procedural programming focuses on writing functions or blocks of code that perform computations, OOP focuses on creating objects that contain both data and the functions that operate on that data.
Theoretically, the relationship between a class and an object is the same as the relationship between a blueprint and a house.
A class is a user-defined data type. It doesn't occupy memory when it is defined. It acts as a template that describes:
Attributes (Data Members): What the object is or has (e.g., color, size, name).
Methods (Member Functions): What the object does (e.g., move, calculate, display).
An object is an instance of a class. When a class is instantiated (an object is created), the system allocates memory for it. You can create multiple objects from a single class, just as you can build many houses from one blueprint.
C++ provides three main levels of access control, which are fundamental to the theory of Encapsulation (hiding internal data from the outside world):
public: Members are accessible from outside the class. This is usually for methods (the "interface").
private: Members are accessible only within the class. This is the default state in C++. We hide data here to prevent accidental corruption.
protected: Members are accessible within the class and by classes derived from it (used in Inheritance).
When you create an object, memory is allocated for its Data Members. Interestingly, memory is not duplicated for the methods. All objects of the same class share a single copy of the member functions in the "Code Segment" of memory to save space.
Since all objects share the same function code, how does a function know which object's data to use?
Theory: Every time a member function is called, the compiler hiddenly passes a pointer called this.
Mechanism: The this pointer holds the address of the specific object that called the function. It acts as an internal link, ensuring that if Car1 calls drive(), the this pointer points to Car1's fuel and speed variables.
#include <iostream> #include <string> using namespace std; // 1. Class Definition class Student { private: // Private data: Cannot be accessed directly from main() string name; int age; public: // Public methods: The "Interface" to interact with the object void setData(string n, int a) { name = n; age = a; } void display() { cout << "Student Name: " << name << endl; cout << "Student Age: " << age << endl; } }; int main() { // 2. Object Creation (Instantiation) Student s1; Student s2; // 3. Interacting with Objects s1.setData("Alice", 20); s2.setData("Bob", 22); cout << "--- Student 1 ---" << endl; s1.display(); cout << "\n--- Student 2 ---" << endl; s2.display(); return 0; }
C++ allows you to define methods in two places:
Inside the Class: (As shown above). These are automatically treated as inline functions by the compiler.
Outside the Class: Using the Scope Resolution Operator (::).
Theoretical Advantage: Defining functions outside the class (usually in a .cpp file) while keeping the class structure in a header file (.h) separates the "Interface" from the "Implementation." This makes the code easier to maintain and speeds up compilation in large projects.
| Feature | Class | Object |
| Definition | A template or blueprint. | An instance of a class. |
| Memory | No memory allocated upon definition. | Memory allocated upon creation. |
| Existence | A logical entity. | A physical entity. |
| Quantity | Defined only once. | Can have many objects of one class. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION