Constructors in Java and C++
Constructors:
Java Constructor Example:
Code:
Output:
- Member function whose name is same as class.
- It doesn't return any value not even null. It is called at the time of the creation of the object.
- It is called explicitly.
- 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
Post a Comment
Feel free to leave a comment...