The Wayback Machine - https://web.archive.org/web/20240727000803/https://www.geeksforgeeks.org/when-are-constructors-called/
Open In App

When are Constructors Called?

Last Updated : 29 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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: 
 

C




#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: 
 

C




using namespace std;
 
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:
 

C




#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.
 

C




// PROGRAM 1: Constructor without any parameter
#include<iostream>
using namespace std;
 
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
*/


C




// PROGRAM 2: Constructor with parameter (using initializer list)
#include <iostream>
using namespace std;
 
class A
{
public:
    int i;
    A(int );
};
 
A::A(int arg)
{
    i = arg;
    cout << "A's Constructor called: Value of i: " << i << endl;
}
 
// Class B contains object of A
class B
{
    A a;
public:
    B(int );
};
 
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.
 

C




#include<iostream>
 
using namespace std;
 
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
*/


References: 
http://web.cs.wpi.edu/~cs2303/c10/Protected/Lectures-C10/Week5_MoreClasses.ppt



Similar Reads

What happens when a function is called before its declaration in C?
In C, if a function is called before its declaration, the compiler assumes the return type of the function as int.For example, the following program fails in the compilation. C/C++ Code #include &lt;stdio.h&gt; int main(void) { // Note that fun() is not declared printf(&quot;%c\n&quot;, fun()); return 0; } char fun() { return 'G'; } if the function
2 min read
What happens when a virtual function is called inside a non-virtual function in C++
Predict the output of the following simple C++ program without any virtual function. C/C++ Code #include &amp;lt;iostream&amp;gt; using namespace std; class Base { public: void print() { cout &amp;lt;&amp;lt; &amp;quot;Base class print function \n&amp;quot;; } void invoke() { cout &amp;lt;&amp;lt; &amp;quot;Base class invoke function \n&amp;quot;;
2 min read
When is a Copy Constructor Called in C++?
A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object. In C++, a Copy Constructor may be called for the following cases: 1) When an object of the class is returned by value
2 min read
When Does Compiler Create Default and Copy Constructors in C++?
A constructor is a special type of member function of a class that initializes objects of a class. In C++, Constructor is automatically called when an object(instance of a class) is created. There are 3 types of constructors in C++ Default ConstructorCopy constructorParameterized ConstructorIn C++, the compiler creates a default constructor if we d
3 min read
C++ | Constructors | Question 2
When a copy constructor may be called? (A) When an object of the class is returned by value. (B) When an object of the class is passed (to a function) by value as an argument. (C) When an object is constructed based on another object of the same class (D) When compiler generates a temporary object. (E) All of the above Answer: (E) Explanation: See
1 min read
C++ | Constructors | Question 4
#include&lt;iostream&gt; using namespace std; class Point { public: Point() { cout &lt;&lt; &quot;Constructor called&quot;; } }; int main() { Point t1, *t2; return 0; } (A) Compiler Error (B) Constructor calledConstructor called (C) Constructor called Answer: (C) Explanation: Only one object t1 is constructed here. t2 is just a pointer variable, no
1 min read
C++ | Constructors | Question 5
Output of following program? #include&lt;iostream&gt; using namespace std; class Point { public: Point() { cout &lt;&lt; &quot;Normal Constructor called\n&quot;; } Point(const Point &amp;t) { cout &lt;&lt; &quot;Copy constructor called\n&quot;; } }; int main() { Point *t1, *t2; t1 = new Point(); t2 = new Point(*t1); Point t3 = *t1; Point t4; t4 = t
1 min read
C++ | Constructors | Question 6
Output of following C++ code will be? C/C++ Code #include using namespace std; class X { public: int x; }; int main() { X a = {10}; X b = a; cout (A) Compiler Error (B) 10 followed by Garbage Value (C) 10 10 (D) 10 0 Answer: (C) Explanation: The following may look like an error, but it works fine. X a = {10}; Like structures, class objects can be i
1 min read
C++ | Constructors | Question 7
What is the output of following program? #include &lt;iostream&gt; using namespace std; class Point { int x, y; public: Point(const Point &amp;p) { x = p.x; y = p.y; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1; Point p2 = p1; cout &lt;&lt; &quot;x = &quot; &lt;&lt; p2.getX() &lt;&lt; &quot; y = &quot; &lt;&lt; p2.ge
1 min read
C++ | Constructors | Question 8
#include &lt;iostream&gt; using namespace std; class Point { int x, y; public: Point(int i = 0, int j = 0) { x = i; y = j; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1; Point p2 = p1; cout &lt;&lt; &quot;x = &quot; &lt;&lt; p2.getX() &lt;&lt; &quot; y = &quot; &lt;&lt; p2.getY(); return 0; } (A) Compiler Error (B) x
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg