0% found this document useful (0 votes)
5 views

Constructors

A constructor is a special member function used to initialize instance variables of a class, sharing the same name as the class. There are different types of constructors, including default constructors, parameterized constructors, and copy constructors, with characteristics such as being public, having no return type, and being invoked automatically. Constructor overloading allows multiple constructors with the same name but different parameter lists, distinguishing them from methods, which have different names and can return values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Constructors

A constructor is a special member function used to initialize instance variables of a class, sharing the same name as the class. There are different types of constructors, including default constructors, parameterized constructors, and copy constructors, with characteristics such as being public, having no return type, and being invoked automatically. Constructor overloading allows multiple constructors with the same name but different parameter lists, distinguishing them from methods, which have different names and can return values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Constructors

What is Constructor?
Constructor is a member function with a name same as that of the class name used to
initialize the instance variables of the object.

Explain Default constructor, parameterized constructor.


A constructor which initializes instance variables of an object with definite values readily
available is known as default constructor. It is a constructor called by default while
creating an object.
A parameterized constructor is a member function with same name as the class name
which is used to initialize the object variables by passing parametric values at the time of
its creation.

What is constructor overloading?


A process to use a number of constructors with the same name but different types of
parameter list is known as Constructor Overloading.

What is Copy Constructor?


The constructor which is used to copy the initial values of the instant variables of an
object to the instant variables of another object is known as Copy Constructor.

What are the Characteristics of Constructors?


The special characteristics associated with constructors are:
1. Constructors should be declared in the public section of the class.
2. They are invoked automatically when an object of the class is created.
3. They do not have any return type and cannot return any values.
4. Like any other functions, they can accept arguments.
5. A class can have more than one constructor.
6. Default constructors do not accept parameters.
7. If no constructor is present in the class, the compiler provides a default constructor.

State the difference between constructor and method.


Constructor Method
 A constructor is a method having same  A method has a valid name different
name as the class containing it. from the class name.
 No return type for constructor, not even  A method may have return type
void. modifier.
 A constructor only initializes.  A method serves various purposes.
 A constructor is a special method called  A method is always called explicitly
automatically whenever an instance of and is never called automatically.
the class is declared.

You might also like