Start learning free

LESSON 175 OF 343 · OBJECT-ORIENTED PROGRAMMING

Defining a Class in C++: Syntax and the Trailing Semicolon

Lesson Read the full explanation

TL ; DR:

  • A class is a blueprint for creating objects. It groups together related data and behavior under a single name.


  • In C++, you define a class with the class keyword, followed by the class name and a pair of curly braces. Don't forget the trailing semicolon:

    class Dog {
    };
    

    The body of the class can be empty — that's still a valid class. We'll add data and behavior to it in the next lessons.



Full lesson:

So far, you've worked with built-in types like int, string, and arrays. But what if you want to model something the language doesn't have a type for — like a Dog, a BankAccount, or a Game?

That's what classes are for. A class lets you define your own type by listing what data it holds and what actions it can perform.


Why classes?

Imagine you're writing a program about dogs. Each dog has a name, an age, and can bark. Without classes, you'd have to track each dog's name in one variable, age in another, and write a separate bark() function for each. That gets messy fast.

With a class, you bundle all of that into one neat package called Dog:

class Dog {
    // We'll add member variables and methods in upcoming lessons.
};

The syntax

Defining an empty class in C++ looks like this:

class ClassName {
};

Three things to notice:

  • The keyword class tells C++ you're defining a new type.

  • ClassName is the name you choose. By convention, C++ class names use PascalCase — capitalized words with no underscores: Dog, BankAccount, HighScore.

  • The curly braces { } hold the body of the class, and a semicolon goes after the closing brace. This is a quirk of C++ — forget it and you'll get a confusing compiler error.


Your task

Define an empty class called Dog. The pre-filled code in mainFunction will create an instance of your class and print a confirmation message. If you define the class correctly, the program will compile and print:

Defined the Dog class!
STEP 1 OF 1
    Ctrl + Enter to check

    C++ · OBJECT-ORIENTED PROGRAMMING

    Defining A Class in C++, explained

    What a class is

    A class is a blueprint for a new type. The types you have used so far, such as int, string and arrays, come with the language. A class lets you invent your own: a Dog, a BankAccount, a Game. Once the type exists, you can create variables of it and, in later lessons, give it data and behaviour.

    The blueprint is not the thing itself. Writing a class describes what a dog would look like, but no dog exists until you create an object from it. This lesson only covers the first half: writing the definition.

    The syntax

    The smallest class definition looks like this:

    class Dog {
    };
    

    It has four parts:

    • The keyword class, which tells the compiler a new type is being defined.
    • The name of the type. By convention, class names use PascalCase: each word starts with a capital letter and there are no underscores, as in Dog, BankAccount or HighScore. The compiler doesn't enforce this, but other C++ programmers expect it.
    • A pair of curly braces holding the body of the class. Here the body is empty, which is still a valid class.
    • A semicolon after the closing brace.

    The body will later hold member variables and member functions. For now, a comment can mark the spot where they will go:

    class Dog {
        // member variables and member functions will go here
    };
    

    Why the semicolon is required

    Almost every other block in C++ ends at its closing brace: a function body, an if, a loop. A class definition is different. It is a declaration, and a declaration ends with a semicolon, like int x; does.

    The rule exists because C++ allows you to declare variables right after the closing brace and before the semicolon:

    class Cat {
    } tom;
    

    This defines the type Cat and, in the same statement, a variable tom of that type. Writing it this way is rare today, but the semicolon is the reason the syntax is unambiguous, and the compiler needs it either way.

    If you forget it, the error often points at the line after the class. For this code:

    class Dog {
    }
    
    int main() { }
    

    GCC reports expected ';' after class definition. Other compilers word it differently, and some report a confusing complaint about the next declaration instead. When you see a strange error right below a class, check the closing brace of the class first.

    The lesson's exercise

    The task is to define an empty class named Dog. The code that follows creates an instance and prints a message, so it only compiles once the type exists. The expected output is:

    Defined the Dog class!

    The steps are short:

    1. Write the class above the Solution class. C++ reads a file from top to bottom, and a name must be declared before the code that uses it. If Dog were defined below Solution, the line Dog dog; would fail with an error saying Dog was not declared.
    2. Use the keyword class and the exact name Dog. C++ names are case-sensitive, so dog and Dog are different identifiers.
    3. Add the braces and finish with };.

    The solution in C++

    class Dog {
    };
    
    class Solution {
    public:
        void mainFunction() {
            Dog dog;
            cout << "Defined the Dog class!" << endl;
        }
    };
    

    Some details worth noticing:

    • An empty class is not zero bytes. Every object needs its own address, so sizeof(Dog) is at least 1. On common compilers it is exactly 1.
    • The line Dog dog; creates an object. The type comes first, then the variable name. No new is needed. The next lesson covers this.
    • The class Solution follows the same shape. It also ends with };, and its public: label is the access label that the lessons on member variables and access specifiers explain.

    Common mistakes

    • Forgetting the semicolon after the closing brace. This is the most common error, and the message may point to the next line.
    • Putting parentheses after the name. A class is not a function, so class Dog() { }; is invalid.
    • Defining the class after the code that uses it. Put it above Solution.
    • Misspelling the name or changing its case. dog, Dog and DOG are three different names.
    • Writing the keyword in another language's style. C++ has no public class prefix as in Java or C#. Access is controlled by labels inside the class.

    Key takeaways

    • A class defines a new type; objects made from it come later.
    • The form is class Name { };, with the name in PascalCase and a semicolon after the closing brace.
    • An empty body is a valid class, and it still takes at least one byte per object.
    • Define the class before the code that uses it.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free