In C++, a class is a user-defined type that describes the data members and member functions its objects will have. An object is a concrete instance of that class, with its own storage and its own values. You write one class definition and can create any number of objects from it, such as Person alice; and Person bob;.
A class is a compile-time description. An object exists at run time, occupies memory (sizeof gives its size), and holds actual values.
A class defines a type: the data it stores, the operations it supports and who may access them. Defining a class does not create any Person yet.
The private section hides the data members, and the constructor initialises them. introduce() is marked const because it does not modify the object.
An object is created when you declare a variable of the class type or allocate one dynamically. Each object has its own copy of the non-static data members.
alice and bob do not affect each other. The std::unique_ptr destroys the heap object automatically; avoid raw new and delete in modern C++.
| Aspect | Class | Object |
|---|---|---|
| Nature | type definition (blueprint) | instance of that type |
| Created with | class Name { ... }; | Name x; or Name x{args}; |
| Memory | no storage for per-object data | storage for all data members |
| Quantity | one definition | as many as you need |
| Values | none (only default member initialisers) | concrete values |
| Lifetime | exists in the source and type system | constructed and destroyed at run time |
Static data members are the exception: there is one copy shared by the whole class, not one per object.
An object's life begins when a constructor finishes and ends when its destructor runs. For stack objects the destructor runs automatically at the end of the scope, in reverse order of construction. This is the basis of RAII, where a class owns a resource (memory, file, lock) and releases it in its destructor.
The only language-level difference is the default access: members of a struct are public by default, while members of a class are private. Both can have constructors, member functions and inheritance. By convention, struct is used for simple data aggregates and class for types that maintain invariants.
Yes. A class is just a type, and a program may define classes it never instantiates. Classes with only static members or abstract interfaces are common examples.
The class definition itself does not store per-object data, though the compiled program may contain code and, for polymorphic classes, a virtual table. Each object takes sizeof(ClassName) bytes, which is at least one byte even for an empty class.
A pointer holds the address of an object and is a separate object itself. Dereferencing it with *p or p->member reaches the actual instance.
In the C++ standard, an object is a region of storage, so even an int variable is an object. In everyday talk, "object" usually means an instance of a class type.
A class is the type definition that specifies data and behaviour; an object is a concrete instance that owns memory and values. Define the class once, create as many objects as needed, prefer stack objects and smart pointers, and let constructors and destructors manage lifetime. The only difference between struct and class is default access.