The answer to this question depends upon 2 scenarios:
Scenario 1: When there is a Virtual function in the program: In this scenario, compiler automatically creates virtual table(known as V-Table) and VVPTR(Virtual Void Pointer). V-Table contains the virtual method calls whereas VVPTR contain the address of virtual methods which are present in V-Table, so VVPTR points to methods of V-Table.
The compiler takes the following steps to initialize VVPTR:-
- When compiler gets an indication that virtual functions are used in the code, it will create V-table and VVPTR.
- Now to initialise VVPTR compiler generate 7 lines of code which should run whenever compiler comes to know that virtual function is going to use
- So the compiler will copy this 7 lines of code inside the constructor, so that just after the execution of the object VVPTR should get initialised so that it can point to V-Table.
- Now if no constructor is defined explicitly, then it will copy this 7 lines of code inside default constructor(which compiler will create of it’s own).
- Now, it will call the virtual methods.
Therefore, this makes it clear that the body of Default Constructor is not blank when the code contains virtual functions.
- Scenario 2: When there is no Virtual function in the program: In this scenario, compiler doesn’t create any V-table or VVPTR. Hence the default constructor reamins empty.
- Does C++ compiler create default constructor when we write our own?
- C++ default constructor | Built-in types
- How to use getline() in C++ when there are blank lines in input?
- OpenCV C++ Program to create a single colored blank image
- Print "Hello World" with empty or blank main in C++
- C++ Internals | Default Constructors | Set 1
- Templates and Default Arguments
- Default arguments and virtual function
- Default Assignment Operator and References
- When does compiler create default and copy constructors in C++?
- Some Interesting facts about default arguments in C++
- Default Arguments in C++
- Default values in a Map in C++ STL
- Default Methods in C++ with Examples
- Default value of Vector in C++ STL
- Copy constructor vs assignment operator in C++
- When is copy constructor called?
- When should we write our own copy constructor?
- What is conversion constructor in C++?
- Advanced C++ | Virtual Constructor
Conclusion: If program contain virtual functions, then the body of default constructor is not blank and if not, then the body of default constructor is blank
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.