Constructor


#include "stdafx.h"
#include <iostream>
#include<string>

using namespace std;

class MyClass
{
  // Constructor is used to initialize objects. It has the same name as its class.

  public
    MyClass()    // default constructor
    {
      cout<< "Inside the constructor which accepts no parameter" << endl;
    }
    MyClass(int a)
    {
      cout<< "Inside the constructor which accepts an integer parameter :" << a << endl;
    }
    MyClass(string message)
    {
      cout<< "Inside the constructor which accepts a string parameter : " << message << endl;
    }
};

int main ()
{  
  MyClass mc1;  // Calls the default constructor
  MyClass mc2(100);  // Calls the constructor which accepts integer parameter.
  MyClass mc3("HELLO WORLD..!!");  // Calls the constructor which accepts string parameter.  

  return 0;
}

Output:
Inside the constructor which accepts no parameter
Inside the constructor which accepts an integer parameter :100
Inside the constructor which accepts a string parameter :HELLO WORLD..!!