C++ Constructor Initialize member variable in constructor Initialization list

Description

C++ Constructor Initialize member variable in constructor Initialization list

#include <iostream>

using namespace std;

class MyNumber/*from   w  w  w . j a va  2s .  c  om*/
{
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);
}



PreviousNext

Related