Constructor Overloading in C++

05 min read

Constructor Overloading: Just like other member functions, constructors can also be overloaded. When you have both default and parameterized constructors defined in your class you are having Overloaded Constructors, one with no parameter and other with a parameter. You can have any number of Constructors in a class that differs in the parameter list.

Example:

Class Student
{
  int roll no;
  string name;
  public:
  Student ( int x)
  {
  roll no = x;
  }

  Student ( int x, string str )
  {
  roll no = x;
  name = str;
  }
  };

  int main ()
  {
  Student A(10);
  Student b( 11, “ Ram” );
  } 

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error