Classes & Constructors in C++
From the C++ cheat sheet · Classes & Objects · verified Jul 2026
Classes & Constructors
Members, access, and the member initializer list.
cpp
// Quick Reference
class Person {
public:
Person(std::string n) : name(n) {}
std::string getName() const { return name; }
private:
std::string name;
};💡 Initialize members in the constructor initializer list (: a(x)), not in the body - it avoids double-init.
⚡ Mark member functions const when they do not modify the object - const objects can only call those.
📌 class members are private by default; put the public interface first for readability.
🟢 Give members default initializers so every constructor starts from a known state.
classesconstructors