c++ - Copy Constructor and default constructor -


Do we want to define a default constructor when we define per constructor for a class ?? Please give reason.

Example:

  class A {int i; Public: A (A and A) {i = AI; // OK it's right ....} A () {} // Is it necessary that we write the above copy constructor ?? };  

In addition, if we define another parameterized constructor for another category other than the copy maker, then do we also have to define the default constructor ?? Without the copy constructor, consider the code above and

  A (int z) {z.i = 10; }  

Alert. After watching the answer, I wrote the following program.

  #include & lt; Iostream & gt; using namespace std; Class X {int i; Public: // X (); X (Int II); Zero print (); }; X: X () {} X :: X (int ii) {i = ii; } Zero X :: print () {cout & lt; & Lt; "I =" & lt; & Lt; I & lt; & Lt; Endl; } Int main (zero) {X (10); // X x1; X.print (); //x1.print (); }  

and this program is working fine without the default constructor. Please tell why this is the matter? I'm really confused with the concept ...

Yes once you have a class For obviously announcing absolutely any constructor, the compiler stops providing the underlying default constructor. If you still need the default constructor, you have to explicitly declare it and it must be defined manually.

It is possible to write the PS copy constructor (or the conversion constructor, or any other constructor) which is also the default constructor if your new constructor comes into that category, now no more Constructor is not required to provide :) For example:

  // A sketch structure of just one possible technique is S (const S & amp;;); S (int) {}}; S. Dummy (0); S: S (const S & amp; dummy) {}  

In the above example, copy constructor is the default constructor at the same time.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -