Initialize member variable in constructor Initialization list - C++ Class

C++ examples for Class:Constructor

Description

Initialize member variable in constructor Initialization list

Demo Code

#include <iostream>

using namespace std;

class MyNumber/* w w w .  j a va 2  s .  co m*/
{
public:
    int First;
    MyNumber(int TheFirst) : First(TheFirst) {}
};

void WriteNumber(MyNumber num)
{
    cout << num.First << endl;
}

int main()
{
    MyNumber prime = 17;
    WriteNumber(prime);
    WriteNumber(MyNumber(23));
    WriteNumber(29);
}

Result


Related Tutorials