Does C++ compiler create default constructor when we write our own?
In C++, compiler by default creates default constructor for every class. But, if we define our own constructor, compiler doesn’t create the default constructor.
For example, program 1 compiles without any error, but compilation of program 2 fails with error “no matching function for call to `myInteger::myInteger()’ ”
Program 1
#include<iostream> using namespace std; class myInteger { private: int value; //...other things in class }; int main() { myInteger I1; getchar(); return 0; } |
chevron_right
filter_none
Program 2
#include<iostream> using namespace std; class myInteger { private: int value; public: myInteger(int v) // parametrized constructor { value = v; } //...other things in class }; int main() { myInteger I1; getchar(); return 0; } |
chevron_right
filter_none
Please write comments if you find anything incorrect in the above GFact or you want to share more information about the topic discussed above.
References:
http://en.wikipedia.org/wiki/Default_constructor
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr375.htm
Recommended Posts:
- When does compiler create default and copy constructors in C++?
- C++ default constructor | Built-in types
- Is body of a Default Constructor blank in C++?
- When should we write our own copy constructor?
- How to create a List with Constructor in C++ STL
- Input-output system calls in C | Create, Open, Close, Read, Write
- Default Arguments in C++
- Templates and Default Arguments
- C++ Internals | Default Constructors | Set 1
- Default Assignment Operator and References
- Some Interesting facts about default arguments in C++
- Default arguments and virtual function
- What are the default values of static variables in C?
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 4



