Does 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

 1 #include<iostream>
 2  
 3 using namespace std;
 4  
 5 class myInteger
 6 {
 7    private:
 8      int value;
 9       
10      //...other things in class  
11 };
12  
13 int main()
14 {
15   myInteger I1;
16   getchar();
17   return 0;
18 }

 


  Program 2

 1 #include<iostream>
 2  
 3 using namespace std;
 4  
 5 class myInteger
 6 {
 7    private:
 8      int value;
 9    public: 
10      myInteger(int v)  // parametrized constructor
11      {  value = v;  }
12     
13      //...other things in class  
14 };
15  
16 int main()
17 {
18   myInteger I1;
19   getchar();
20   return 0;
21 }

 

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  10:11:05

posted @ 2013-11-26 10:11  虔诚的学习者  阅读(184)  评论(0编辑  收藏  举报