Constructors

In this lesson, we'll learn how to create an instance of a class.

In the previous lesson, we learned what classes and objects are.

Constructors are special methods for the instantiation of an object in the class.

It is declared inside the class but can be defined outside as well. The constructor:

  • has the exact same name as the class.

  • does not have a return type.

Default constructors #

Let’s revisit the Account class from the previous lesson:

class Account{
    public:
        Account(); // Constructor
        void deposit(double amt);
        void withdraw(double amt);
        double getBalance() const;
    private:
...