Use constructor to init member variables : Constructor « Class « C++






Use constructor to init member variables

Use constructor to init member variables
  

#include <iostream>
using namespace std;

class myclass {
  int i, j;
public:
  myclass(int x, int y) { 
    i = x; 
    j = y; 
  }
  void show() { 
    cout << i << " " << j; 
  }
};

int main()
{
  myclass count(2, 3);

  count.show();

  return 0;
}

           
         
    
  








Related examples in the same category

1.Constructing and Destructing sequence for three level inheritance
2.Parameterized ConstructorsParameterized Constructors
3.string type constructorstring type constructor
4.Use Double value as the constructor parameterUse Double value as the constructor parameter
5.Constructor: different parameter typeConstructor: different parameter type
6.Use automatic conversions to assign new valuesUse automatic conversions to assign new values
7.Call constructor from base classCall constructor from base class
8.Constructor with 2 parametersConstructor with 2 parameters
9.Define constructor outside a class definition
10.overloading class constructors
11.Constructor with parameter value checking