CodeBlanks Logo Icon CodeBlanks

Summary - Constructor

Combine overloading and initializer list to write a Product class.

Summary - Constructor

In this section you learned constructors and destructors. A constructor has the same name as the class, no return type and is called automatically when an object is created. A parameterized constructor takes values from outside; the member initializer list initializes members directly after a colon.

You can define more than one constructor in a class (overloading); the compiler selects by parameters. A destructor starts with a tilde, is called automatically when an object leaves scope and cleans up resources. Now we will bring these together in a task.

Practice Add a constructor and destructor to the Counter class

Define a Counter class. Under public: add int count;, the constructor Counter() { count = 0; std::cout << "Built"; } and the destructor ~Counter() { std::cout << "Destroyed"; }. In main create a Counter c; instance.

Starter code

main.cpp
#include <iostream>

// Define the Counter class here

int main() {
    // Create a Counter instance
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Counter {
public:
    int count;
    Counter() {
        count = 0;
        std::cout << "Built";
    }
    ~Counter() {
        std::cout << "Destroyed";
    }
};

int main() {
    Counter c;
    return 0;
}

Expected output

BuiltDestroyed

Practice Add a parameterized constructor to Counter

Add the parameterized constructor Counter(int start) : count(start) { std::cout << "Built"; } to the Counter class above with an initializer list (keep the default constructor too). In main create Counter c(10); and print its value with std::cout << c.count;.

Starter code

main.cpp
#include <iostream>

class Counter {
public:
    int count;
    Counter() {
        count = 0;
        std::cout << "Built";
    }
    ~Counter() {
        std::cout << "Destroyed";
    }
    // Add the parameterized constructor with an initializer list here
};

int main() {
    // Create a Counter with the parameterized ctor and print count
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Counter {
public:
    int count;
    Counter() {
        count = 0;
        std::cout << "Built";
    }
    Counter(int start) : count(start) {
        std::cout << "Built";
    }
    ~Counter() {
        std::cout << "Destroyed";
    }
};

int main() {
    Counter c(10);
    std::cout << c.count;
    return 0;
}

Expected output

Built10Destroyed

What's next?

You learned the object lifecycle with constructors, initializer lists and destructors. Objects are powerful on their own, but C++ truly shines with inheritance.

In the next section you will learn to derive a new class from an existing one, achieve runtime polymorphism with virtual and override, and close inheritance with final. Inheritance reduces code duplication and lets you build class hierarchies.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the colon do in Player(std::string n, int l) : name(n), level(l) { }?

main.cpp
Player(std::string n, int l) : name(n), level(l) { }
  • Marks a class hierarchy
  • Starts the member initializer list Correct answer
  • Separates two statements
  • Marks a label

Explanation: The colon starts the initializer list, before the body.

2 Write the destructor mark

Question
class Logger { public: ____Logger() { } };
Answer
class Logger { public: ~Logger() { } };

Explanation: The tilde marks a destructor: ~Logger().

All lessons in Intermediate