C++ Structs, Classes, and Objects
Group related state and behavior into domain types, understand value semantics, and choose struct or class from interface intent rather than habit.
Model a domain concept as a type
Explain object copying and value semantics
Choose public data or a protected interface deliberately
The short answer
A class or struct defines a type whose objects combine state and operations. Their only language difference is default access; use a struct for simple public value-like data and a class when an invariant needs a protected interface.
Types give rules a home
Separate variables can represent one concept, but the relationship remains informal. A type makes that relationship explicit: a rectangle owns dimensions and provides operations that use them. Callers no longer need to remember which free function accepts which loose values.
Model concepts that have rules, behaviors, or recurring combinations. Do not create a class for every primitive variable; an abstraction should reduce the number of facts callers must coordinate.
#include <iostream>
#include <string>
struct StudySession
{
std::string topic;
int minutes{};
bool is_long() const { return minutes >= 60; }
};
int main()
{
StudySession session{"C++", 75};
std::cout << session.topic << ": " << std::boolalpha
<< session.is_long() << '\n';
}Objects have state behavior and lifetime
An object is an instance of a type. Its data members hold state, member functions expose behavior, and its lifetime begins after initialization and ends at destruction. Member order and ownership determine how composed resources are created and released.
A const member function can be called on const objects and documents observation. Mark accessors and calculations const when they do not modify the object's logical state.
Struct and class differ by default access
Members of a struct are public by default; members of a class are private by default. Both can have constructors, methods, inheritance, operators, templates, and destructors. The common convention is a struct for simple aggregates or value-like data and a class for an invariant-protecting interface.
| Shape | Useful default | Reason |
|---|---|---|
| Coordinate with public x and y | struct | Transparent aggregate |
| Bank account with withdrawal rules | class | State changes need validation |
| Configuration snapshot | struct | Data is the public contract |
| Resource owner | class | Construction and destruction enforce lifetime |
Value semantics make composition easier
Objects are copied by value unless an interface borrows them or the type disables copying. Standard-library members already know how to copy or move themselves, so a type composed from strings and vectors often needs no custom destructor or copy code.
This is the Rule of Zero: let well-designed members manage resources. Write custom copy, move, or destruction behavior only when the type directly owns a lower-level resource that requires it.
Quick knowledge check
Answer before you reveal.
01Is struct a value type and class a reference type in C++?
No. Both define object types with the same value/reference possibilities; their default member and inheritance access differ.
02What does const after a member function mean?
It promises not to modify the observable state of that object through this call.
Exercise
Practice challenge
Create a Rectangle type with width and height, an area member function, and a validity function. Store several rectangles in a vector and sum valid areas.
Requirements
- Rectangle data and related calculations share one type
- Invalid dimensions are detected explicitly
- Copying a rectangle produces an independent value
Optional extension: Protect validity through a constructor in the next lesson.
Open in C++ compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Must one class live in one file?
No language rule requires it, but focused headers and source files often improve navigation and build boundaries.
Should every data field be private?
Simple aggregates can use public fields. Protect data when changes must preserve an invariant or trigger behavior.
What is the this pointer?
Inside a non-static member function, this points to the object on which the function was called.