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,BankAccountorHighScore. 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:
- Write the class above the
Solutionclass. C++ reads a file from top to bottom, and a name must be declared before the code that uses it. IfDogwere defined belowSolution, the lineDog dog;would fail with an error sayingDogwas not declared. - Use the keyword
classand the exact nameDog. C++ names are case-sensitive, sodogandDogare different identifiers. - 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. Nonewis needed. The next lesson covers this. - The class
Solutionfollows the same shape. It also ends with};, and itspublic: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,DogandDOGare three different names. - Writing the keyword in another language's style. C++ has no
public classprefix 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.