OOPS For Design Pattern
OOPS For Design Pattern
#include <iostream>
using namespace std;
class A {
public:
// Constructor of the class without
// any parameters
A() {
cout << "Constructor called" << endl;
}
};
int main() {
A obj1;
return 0;
}
Output
Constructor called
Explanation: In this code, we defined a constructor for class A. In main
function, when we create an object of that class, this constructor is called.
Types of Constructors in C++
Constructors can be classified based on in which situations they are being used.
There are 4 types of constructors in C++:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Move Constructor
1. Default Constructor
A default constructor is a constructor that doesn’t take any argument. It has no
parameters. It is also called a zero-argument constructor. The compiler
automatically creates an implicit default constructor if the programmer does not
define one.
Example:
#include <iostream>
using namespace std;
int main() {
Output
In Main
The object of the class A is created without any parameters, hence the default
constructor exists.
2. Parameterized Constructor
Parameterized constructors make it possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To create
a parameterized constructor, simply add parameters to it the way you would to
any other function. When you define the constructor’s body, use the parameters
to initialize the object.
Example:
#include <iostream>
using namespace std;
class A {
public:
int val;
// Parameterized Constructor
A(int x) {
val = x;
}
};
int main() {
Output
10
In this code, the parameterized constructor is called when we create
object a with integer argument 10 . As defined, it initializes the member
variable val with the value 10.
Note: If a parameterized constructor is defined, the non-parameterized
constructor should also be defined as compiler does not create the default
constructor.
3. Copy Constructor
A copy constructor is a member function that initializes an object using another
object of the same class. Copy constructor takes a reference to an object of the
same class as an argument.
Example:
#include <iostream>
using namespace std;
class A {
public:
int val;
// Parameterized constructor
A(int x) {
val = x;
}
// Copy constructor
A(A& a) {
val = a.val;
}
};
int main() {
A a1(10);
Output
10
In this example, the copy constructor is used to create a new object a2 as a copy
of the object a1. It is called automatically when the object of class A is passed
as constructor argument.
Just like the default constructor, the C++ compiler also provides an implicit
copy constructor if the explicit copy constructor definition is not present.
Here, it is to be noted that, unlike the default constructor where the presence of
any type of explicit constructor results in the deletion of the implicit default
constructor, the implicit copy constructor will always be created by the compiler
if there is no explicit copy constructor or explicit move constructor is present.
4. Move Constructor
The move constructor is a recent addition to the family of constructors in C++.
It is like a copy constructor that constructs the object from the already existing
objects., but instead of copying the object in the new memory, it makes use of
move semantics to transfer the ownership of the already created object to the
new object without creating extra copies.
It can be seen as stealing the resources from other objects.
Syntax:
className (className&& obj) {
// body of the constructor
}
The move constructor takes the rvalue reference of the object of the same class
and transfers the ownership of this object to the newly created object.
Like a copy constructor, the compiler will create a move constructor for each
class that does not have any explicit move constructor.
When are the constructors called for different types of objects like global, local,
static local, dynamic?
1) Global objects: For a global object, constructor is called before main() is
called. For example, see the following program and output:
#include<iostream>
using namespace std;
class Test
{
public:
Test();
};
Test::Test() {
cout << "Constructor Called \n";
}
Test t1;
int main() {
cout << "main() started\n";
return 0;
}
/* OUTPUT:
Constructor Called
main() started
*/
2) Function or Block Scope ( automatic variables and constants ) For a non-
static local object, constructor is called when execution reaches point where
object is declared. For example, see the following program and output:
class Test
{
public:
Test();
};
Test::Test() {
cout << "Constructor Called \n";
}
void fun() {
Test t1;
}
int main() {
cout << "Before fun() called\n";
fun();
cout << "After fun() called\n";
return 0;
}
/* OUTPUT:
Before fun() called
Constructor Called
After fun() called
*/
For a local static object, the first time (and only the first time) execution
reaches point where object is declared. For example, output of the following
program is:
#include<iostream>
using namespace std;
class Test
{
public:
Test();
};
Test::Test() {
cout << "Constructor Called \n";
}
void fun() {
static Test t1;
}
int main() {
cout << "Before fun() called\n";
fun();
cout << "After fun() called\n";
fun(); //constructor is not called this time.
return 0;
}
/* OUTPUT
Before fun() called
Constructor Called
After fun() called
*/
3) Class Scope: When an object is created, compiler makes sure that
constructors for all of its subobjects (its member and inherited objects) are
called. If members have default constructors or constructor without parameter
then these constructors are called automatically, otherwise parameterized
constructors can be called using Initializer List. For example, see PROGRAM 1
and PROGRAM 2 and their output.
class A
{
public:
A();
};
A::A() {
cout << "A's Constructor Called \n";
}
class B
{
A t1;
public:
B();
};
B::B() {
cout << "B's Constructor Called \n";
}
int main() {
B b;
return 0;
}
/* OUTPUT:
A's Constructor Called
B's Constructor Called
*/
class A
{
public:
int i;
A(int );
};
A::A(int arg)
{
i = arg;
cout << "A's Constructor called: Value of i: " << i << endl;
}
B::B(int x):a(x)
{
cout << "B's Constructor called";
}
int main()
{
B obj(10);
return 0;
}
/* OUTPUT
A's Constructor called: Value of i: 10
B's Constructor called
*/
4) Dynamic objects: For a dynamically allocated object, constructor is invoked
by new operator. For example, see the following program and output.
#include<iostream>
class Test
{
public:
Test();
};
Test::Test() {
cout << "Constructor Called \n";
}
int main()
{
cout << "Before new called\n";
Test *t1 = new Test;
cout << "After new called\n";
return 0;
}
/* OUTPUT
Before new called
Constructor Called
After new called
*/
Characteristics of Constructors
The name of the constructor is the same as its class name.
Constructors are mostly declared as public member of the class though they
can be declared as private.
Constructors do not return values, hence they do not have a return type.
A constructor gets called automatically when we create the object of the
class.
Multiple constructors can be declared in a single class (it is even
recommended – Rule Of Three, The Rule of Five).
In case of multiple constructors, the one with matching function signature
will be called.
Relationship
Type Description When to Use