Constructor with 2 parameters : Constructor « Class « C++






Constructor with 2 parameters

Constructor with 2 parameters
  
#include <iostream>
using namespace std;
class MyClass {
  int h;
  int i;
public:
  MyClass(int j, int k) { 
     h = j; 
     i = k; 
  } 
  int getInt() {
     return i;
  }
  int getHeight() {
     return h;
  }
};
int main()
{
  MyClass myObject[3] = {
    MyClass(1, 2), // initialize
    MyClass(3, 4),
    MyClass(5, 6)
  };
  int i;
  for(i=0; i<3; i++) {
    cout << myObject[i].getHeight();
    cout << ", ";
    cout << myObject[i].getInt() << "\n";
  }
  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 constructor to init member variablesUse constructor to init member variables
5.Use Double value as the constructor parameterUse Double value as the constructor parameter
6.Constructor: different parameter typeConstructor: different parameter type
7.Use automatic conversions to assign new valuesUse automatic conversions to assign new values
8.Call constructor from base classCall constructor from base class
9.Define constructor outside a class definition
10.overloading class constructors
11.Constructor with parameter value checking