Constructors in Java and C++

Constructors:

  1. Member function whose name is same as class.
  2. It doesn't return any value not even null. It is called at the time of the creation of the object.
  3. It is called explicitly.
  4. It is always public.
Types of constructors:
  • Default
  • Parameter
  • Copy
   Default constructor: It doesn't take any argument it is used when we want to initialize data members of an object.

   Parameterized constructor: This type of constructor accept the parameter and the value is used to initialize data members of the object.

   Copy constructor: This type of constructor takes object reference as a parameter and use the passing reference to assign value to that object.


C++ Constructor Example:
Code:

Output:
Sum is :0
Sum is :6
Sum is :6

Java Constructor Example:
Code:

Output:
Sum is :0
Sum is :6
Sum is :6

Comments

Popular posts from this blog

C++ programming toll plaza simulation program

C++ program to give you hug(pun intended)