Initialize variables from base class using initialization syntax : initialization syntax « Class « C++ Tutorial






#include <iostream>  
using namespace std;  
  
class MyClass {  
  const int numA; // const member 
  const int numB; // const member 
public:  
   
  MyClass(int x, int y) : numA(x), numB(y) { }  
  
  int getNumA() { return numA; }  
  int getNumB() { return numB; }  
};  
  
int main()  
{  
  MyClass ob1(7, 9), ob2(5, 2);  
  
  cout << "Values in ob1 are " << ob1.getNumB() << 
          " and " << ob1.getNumA() << endl;  
  
  cout << "Values in ob2 are " << ob2.getNumB() << 
          " and " << ob2.getNumA() << endl; 
   
  return 0;  
}
Values in ob1 are 9 and 7
Values in ob2 are 2 and 5








9.8.initialization syntax
9.8.1.Initialize variables from base class using initialization syntax
9.8.2.Initialize numA and numB inside the MyClass constructor using normal syntax
9.8.3.Initialize parameters in base class