Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
When are Constructors Called in C++?
Here we will see, when constructors are called. Here constructors are of different types. Global, local, static local, dynamic.
For the global object, the constructors are called before entering into the main function.
Example
#includeusing namespace std; class MyClass { public: MyClass() { cout Output
Calling Constructor Inside MainWhen the object is non-static, then, constructor is called when the execution reaches the point, where object is created.
Example
#includeusing namespace std; class MyClass { public: MyClass() { cout Output
Inside Main Calling Constructor After creating objectWhen the object is local static, then only for the first time, its constructor will be called, if the same function is used again, it will not affect.
Example
#includeusing namespace std; class MyClass { public: MyClass() { cout Output
Inside Main Calling Constructor After creating object After second timeFinally for the dynamic object, the constructor will be called, when object is created using new operator.
Example
#includeusing namespace std; class MyClass { public: MyClass() { cout Output
Inside Main Declaring pointer Calling Constructor After creating dynamic object
Advertisements
