C++: Constructor in C++ 11

This entry is part 50 of 61 in the series C++

C++ 11 introduces a new syntax for creating the constructor.

Prior to C++ 11, this example in an earlier post shows how we would create a constructor.

#include <iostream>

using namespace std;

// Base class

class Music {
    string singer;
    int top10;


    string printOut() {
        return this->singer;
    Music() : singer("Katy Perry"), top10(10) {};

int main(void) {

    Music music;
    cout << music.printOut();
    return 0;

The C++ 11 constructor is shown in line 20.

Notice that there are 2 curly braces {} at the end of the constructor.


Series Navigation<< C++: Assessing private members of the base class
C++: Applying the 3 classes to the base class >>