Struct vs Class

What Are Classes in C++? A Beginner's Guide

A class in C++ bundles data and functions into one user-defined type. This beginner's guide explains objects, members and access labels, with an example.

On this page
  1. What a class is
  2. A simple example
  3. Data members and member functions
  4. Access labels: public and private
  5. Constructors and destructors
  6. Why use classes?
  7. Classes and structs
  8. Common beginner mistakes

What a class is

What are classes in C++? A class is a user-defined type that groups data and the functions that work on that data into one unit. Each object you create from a class is an instance of it. A useful way to picture this is a blueprint: the class describes what every object looks like and what it can do, while each object holds its own values.

A simple example

This small class counts. It keeps its count private and changes it only through two public functions:

#include <iostream>

class Counter {
public:
    void increment() {
        ++count_;
    }

    int value() const {
        return count_;
    }

private:
    int count_ = 0;
};

int main() {
    Counter clicks;  // create an object
    clicks.increment();
    clicks.increment();
    std::cout << clicks.value() << '\n';  // prints 2
}

Here is what each part does:

  • class Counter { ... }; defines the type. The semicolon after the closing brace is required, and forgetting it is a common beginner error.
  • Counter clicks; creates an object named clicks.
  • clicks.increment() calls a member function. The dot connects the object to the function.
  • count_ is a data member. The trailing underscore is a naming habit many programmers use for members. It is not a language rule.
  • The const after value() promises that the function does not change the object.

Data members and member functions

Members come in two kinds. Data members store values, like count_. Member functions act on those values, like increment() and value(). Inside a member function, you can use member names directly, so count_ refers to the count of the object the function was called on.

Access labels: public and private

public: makes the members below it usable from any code. private: hides them from code outside the class. Class members are private unless you say otherwise, but writing the labels makes the design clear to readers. The full set of rules, including protected, is covered in access modifiers in c.

Constructors and destructors

A constructor runs when an object is created. It has the same name as the class and no return type. A destructor runs when the object’s lifetime ends, and its name starts with a tilde, as in ~Counter().

Counter does not declare a constructor, so the compiler provides a default one. The count_ member starts at 0 because of its default member initializer. Constructors can take arguments and set up objects in other ways. The syntax is covered in c class constructor.

Why use classes?

Hiding details behind a small set of public functions is the core idea of encapsulation in c. If you later change how the count is stored, code that calls increment() and value() does not need to change. Classes can also build on one another through inheritance in c.

Classes and structs

In C++, a struct is also a class type. The main difference is default access, which is covered in c struct vs class.

Common beginner mistakes

  • Forgetting the semicolon after the closing brace of the class.
  • Trying to use a private member from outside the class, such as writing clicks.count_ = 5; in main. The compiler rejects it.
  • Writing clicks.increment; without parentheses. This does not call the function, and the compiler rejects it.

The complete guide to struct vs class

Struct vs Class in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Unary Operators in C++

Learn what unary operators in C++ are, from the one-operand symbols like !, ~, ++ and * to prefix vs postfix, precedence and common mistakes, with examples.