C++ Class Generator
List the members of a class and get modern C++ split into a header and a source file, or header-only. The generator writes include guards, namespaces and includes, default member initialisers, constructors that take values and move them into place, const and noexcept getters with [[nodiscard]], setters, a defaulted operator== and std::optional for optional members.
- Runs in your browser
- No sign-up
- Free to use
How to use C++ Class Generator
- Enter the class name and namespace.
- List the members as name: type = default.
- Choose constructors, accessors, comparison and file layout.
- Copy or download the .h and .cpp files.
C++ Class Generator features
Header and source
Separate .h/.cpp or header-only.
Guards
#pragma once or #ifndef include guards.
Efficient constructors
Pass by value, then std::move into members.
Const-correct getters
const references for heavy types, [[nodiscard]], noexcept.
C++20 comparison
Defaulted operator== for all members.
Standard types
std::string, std::vector, std::map, std::optional, chrono.
When to use C++ Class Generator
- Creating data classes for C++ applications and games.
- Starting new modules with consistent structure.
- Learning modern C++ class design.
- Converting a data model from another language to C++.
C++ Class Generator FAQ
What is the rule of zero?
If the members manage their own resources, as standard containers and strings do, the class should declare no destructor, copy or move operations; the compiler-generated ones are correct.
Why do constructors take values and use std::move?
Taking a parameter by value and moving it into the member is efficient both for temporaries (one move) and for lvalues (one copy), with a single constructor.
Why return const references from getters?
For strings and containers, returning a const reference avoids a copy. Cheap types such as int and double are returned by value.
Which C++ standard is needed?
std::optional needs C++17; the defaulted operator== needs C++20. Without those options, the code works with C++17 or C++11 depending on the types used.
#pragma once or include guards?
Every major compiler supports #pragma once and it is shorter. Include guards are standard C++ and work everywhere.
Is anything uploaded?
No. The code is generated in your browser.
Writing modern C++ classes
C++ classes involve more decisions than in most languages: where to put declarations and definitions, how to pass and return values, which special member functions to write and which headers to include. Modern C++ has clear answers to most of these, and the generator applies them consistently.
Members are listed with neutral types that map to the standard library: std::string, int, double, bool, std::chrono time points, std::vector, std::map and std::optional for optional members. The necessary headers are included automatically. Members get default member initialisers, so numbers start at zero and booleans at false rather than holding indeterminate values.
Constructors take each argument by value and move it into the member in the initialiser list. This single constructor is efficient for all callers. A defaulted default constructor is provided too, and a constructor with a single parameter is marked explicit to prevent accidental conversions.
Getters return cheap types by value and everything else by const reference, are marked const and noexcept, and optionally [[nodiscard]], so ignoring a returned value produces a warning. Setters take a value and move it. With C++20, operator== can be defaulted to compare all members, and != follows automatically.
Following the rule of zero, the class declares no destructor, copy or move operations: the members already manage their resources, so the compiler-generated versions are correct and the class is copyable and movable. Choose header-only output for small classes, or separate files to keep compile times down in larger projects.