The Member Initialization Syntax : Class Member « Class « C++






The Member Initialization Syntax

The Member Initialization Syntax
  
#include <iostream>
using namespace std;
class MyClass {
  int numA;
  int numB;
public:
  MyClass(int x, int y) {
    numA = x;
    numB = y;
  }
  int getNumA() { 
    return numA; 
  }
  int getNumB() { 
    return numB; 
  }
};
int main()
{
  MyClass object1(7, 9), object2(5, 2);
  cout << "Values in object1 are " << object1.getNumB() << " and " << object1.getNumA() << endl;
  cout << "Values in object2 are " << object2.getNumB() << " and " << object2.getNumA() << endl;
  return 0;
}

           
         
    
  








Related examples in the same category

1.Assign values using the member initialization syntaxAssign values using the member initialization syntax
2.Use class name to reference field name
3.getName member function reads from the name member variable and the setName member function writes to it
4.enum field