Skip to content

Class vs Object

Class — The Blueprint: A class is a template/blueprint that defines properties and behaviors. It doesn't occupy memory by itself.

Object — The Instance: An object is a real instance of a class. It actually occupies memory.

Think: Class = Blueprint of a house. Object = Actual house built from that blueprint.

Example

cpp
// CLASS — just a blueprint
class Car {
public:
    string brand;
    int speed;
    void drive() {
        cout << brand << " is driving at " << speed << " km/h" << endl;
    }
};

int main() {
    // OBJECTS — actual instances
    Car car1;
    car1.brand = "BMW"; car1.speed = 200;
    car1.drive(); // BMW is driving at 200 km/h

    Car car2;
    car2.brand = "Audi"; car2.speed = 180;
    car2.drive(); // Audi is driving at 180 km/h
}

Key Differences

ClassObject
What it isBlueprint / TemplateInstance of a class
MemoryNo memory allocatedMemory allocated
DeclaredOnceMultiple times
KeywordclassNo keyword needed
ExistsLogicalPhysical

Ways to Create Objects

cpp
Car car1;               // Stack — auto destroyed
Car* car2 = new Car();  // Heap — must delete manually
delete car2;
Car cars[3];             // Array of objects

Memory Layout

CLASS (no memory) — just a definition in code

OBJECT (memory allocated):
 ├── brand → "BMW"
 ├── speed → 200
 └── drive() → shared among all objects (code segment)

Note: Member variables are separate per object, but member functions are shared among all objects.

One-liner: "A class is a blueprint that defines structure and behavior, while an object is a concrete instance of that class with actual memory allocated."