Constructor
Constructor
Constructor
A constructor is a special member function automatically called when an
object is created. In C++, the constructor is automatically called when an
object is created. It is a special class method because it does not have any
return type. It has the same name as the class itself.
A constructor initializes the class data members with garbage value if we don’t
put any value to it explicitly.
The constructor must be placed in the public section of the class because we
want the class to be instantiated anywhere. For every object in its lifetime
constructor is called only once at the time of creation.
Example:
class class_name
{
int data_member1;
string data_member2;
//creating constructor
public:
class_name()
{
// initialize data members with garbage value
}
};
Types of Constructors:
There are three types of constructors in C++:
Default constructor
Parameterized Constructor
Copy Constructor
Default constructor:-
A constructor that doesn't take any argument or has no parameters is known as
a default constructor. In the example above, class_name() is a default
constructor.
Syntax:
class class_name{
int data_member1;
string data_member2;
//default constructor
public:
class_name()
{
// initializing data members with their default
values
data_member1 = 69;
data_member2 = "Coding Ninjas";
}
};
Here, the class_name() constructor will be called when the object is created.
This sets the data_member1 variable of the object to 69 and the data_member2
variable of the object to “Coding Ninjas”.
Note: If we have not defined a constructor in our class, the C++ compiler will
automatically create a default constructor with an empty code and no
parameters, which will initialize data members with garbage values.
When we write our constructor explicitly, the inbuilt constructor will not be
available for us.
Parameterized Constructor:-
This is another type of Constructor with parameters. The parameterized
constructor takes its arguments provided by the programmer. These arguments
help initialize an object when it is created.
Using this Constructor, you can provide different values to data members of
different objects by passing the appropriate values as arguments.
Syntax:
class class_name{
int data_member1;
string data_member2;
//parameterized constructor
public:
Copy Constructor:-
These are a particular type of constructor that takes an object as an argument
and copies values of one object’s data members into another object. We pass
the class object into another object of the same class in this constructor. As the
name suggests, you Copy means to copy the values of one Object into another
Object of Class. This is used for Copying the values of a class object into
another object of a class, so we call them Copy constructor and for copying the
values.
We have to pass the object’s name whose values we want to copy, and when
we are using or passing an object to a constructor, we must use the &
ampersand or address operator.
Syntax:
class class_name{
int data_member1;
string data_member2;
//copy constructor
public:
class_name(class_name &obj)
{
// copies data of the obj parameter
data_member1 = obj.data_member1;
data_member2 = obj.data_member2;
}
};
In this program, we have used a copy constructor to copy the contents of one
object of the class ‘class_name’ to another. The code of the copy constructor
is:
class_name(class_name &obj){
// copies data of the obj parameter
data_member1 = obj.data_member1;
data_member2 = obj.data_member2;
}
If we don’t define our own copy constructor, the C++ compiler creates a
default copy constructor for each class which does a memberwise copy
between objects.
public:
// Default constructor
smartphone() {
model = "unknown";
year_of_manufacture = 0;
_5g_supported = false;
}
// Parameterized constructor
smartphone(string model_string, int manufacture,
bool _5g_) {
// Initialising data members
model = model_string;
year_of_manufacture = manufacture;
_5g_supported = _5g_;
}
// Copy constructor
smartphone(smartphone &obj) {
// Copies data of the obj parameter
model = obj.model;
year_of_manufacture =
obj.year_of_manufacture;
_5g_supported = obj._5g_supported;
}
};
int main() {
// Creating objects of smartphone class
// Using default constructor
smartphone unknown;
PrevNext