C++ Constructors

A C++ constructor is a special class method that runs automatically when an object is created. Constructors are commonly used to give member variables safe starting values.

Without a constructor, you often create an object first and then assign values to its members. With a constructor, the object can be ready to use immediately.

Constructor Syntax

A constructor has the same name as the class and has no return type, not even void. It is usually placed in the public section so code outside the class can create objects.

  • The constructor name must exactly match the class name.
  • A constructor runs once for each object when that object is created.
  • A constructor can have no parameters or it can accept parameters.
  • A constructor does not return a value.

Default Constructor

A constructor with no parameters is often called a default constructor. It lets you create an object without passing arguments, while still giving the object useful starting values.

#include <iostream>
#include <string>

class Player {
public:
    std::string name;
    int score;

    Player() {
        name = "Guest";
        score = 0;
    }

    void print() const {
        std::cout << name << " has " << score << " points." << std::endl;
    }
};

int main(void) {
    Player first;
    first.print();

    return 0;
}

Output:

Guest has 0 points.

When Player first; creates the object, C++ automatically calls Player(). The constructor sets name to "Guest" and score to 0 before print() is called.

Constructors With Parameters

A constructor can accept parameters just like other methods. This lets each new object start with different values.

#include <iostream>
#include <string>

class Book {
public:
    std::string title;
    int pages;

    Book(std::string bookTitle, int pageCount) {
        title = bookTitle;
        pages = pageCount;
    }

    void print() const {
        std::cout << title << ": " << pages << " pages" << std::endl;
    }
};

int main(void) {
    Book first("C++ Basics", 180);
    Book second("Object Guide", 240);

    first.print();
    second.print();

    return 0;
}

Output:

C++ Basics: 180 pages
Object Guide: 240 pages

The statement Book first("C++ Basics", 180); creates a Book object and passes two arguments to the constructor. Inside the constructor, those arguments are copied into the object’s member variables.

Why Constructors Matter

Constructors help prevent objects from being used before their data is set. For example, a Book object should not have an unknown title or an unpredictable page count. A constructor keeps the setup code close to the class definition, so every object is initialized in a consistent way.

Constructors also make code shorter. Instead of creating an object and then writing several assignment statements, you can provide the important values at the moment the object is created.

Common Mistakes

  • Writing a return type for a constructor, such as void Book(). That creates a method, not a constructor.
  • Spelling the constructor name differently from the class name.
  • Forgetting that a constructor runs automatically; you normally do not call it like a regular method.
  • Trying to create an object without arguments when the class only has a constructor that requires arguments.

Takeaway: constructors initialize new objects automatically, making class objects safer and easier to use.