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;
}
|
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;
}
|
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();
return 0;
}
|
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
#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;
}
|
C
#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
{
A a;
public :
B( int );
};
B::B( int x):a(x)
{
cout << "B's Constructor called" ;
}
int main()
{
B obj(10);
return 0;
}
|
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;
}
|
References:
http://web.cs.wpi.edu/~cs2303/c10/Protected/Lectures-C10/Week5_MoreClasses.ppt
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!