Quiz Entry - updated: 2026.07.06
How do you declare a class in C++?
Write class Name { public: … private: … }; — and don't forget the trailing semicolon.
class ClassName {
public:
// Public members (accessible from outside)
ClassName(int x); // constructor
void method() const; // method
private:
// Private members (only accessible inside the class)
int _member;
}; // <-- the semicolon is required
Example:
class Vehicle {
public:
Vehicle(int nwheels);
void show() const;
int getNWheels() const;
private:
int _nwheels;
};
Tip: The semicolon after the closing brace is mandatory (a class declaration is a statement). Forgetting it is one of the most common — and confusingly worded — beginner compiler errors.
Go deeper:
Classes — cppreference — the authoritative reference for class syntax and members.
What is a class? — isocpp FAQ — a plain-language intro to classes and objects.