Initialize member variable in constructor : member variable « Class « C++ Tutorial






#include <iostream>
using namespace std;
class MyClass
{
   public: 
     MyClass::MyClass(void);
     void show_MyClass(void);
   private:
     int a;
     int b;
     int c;
};

MyClass::MyClass(void) : a(1), b(2), c(3) { };

void MyClass::show_MyClass(void)
{
   cout << "a contains: " << a << endl;
   cout << "b contains: " << b << endl;
   cout << "c contains: " << c << endl;
}

int main(void)
{
   MyClass numbers;

   numbers.show_MyClass();
}








9.5.member variable
9.5.1.Create a getter for member variable
9.5.2.Initialize member variable in constructor
9.5.3.Share static variable among different class instances
9.5.4.Output variable initialization message
9.5.5.Assign value to member variable directly from assignment
9.5.6.increment counter variable with ++ operator
9.5.7.Initialize member variable