Constructor overloading and constructor overloading in C ++
# Include <stdio. h>
Class Test
{
Private:
Int I;
Int j;
Int k;
Public:
Test ()
{
I = j = k = 0;
}
Test (int v)
{
I = j = k = v;
}
Void print ()
{
Printf ("I = % d, j = % d, k = % d \ n", I, j, k );
}
};
Int main ()
{
Test t1 (1 );
Test t2 = 2;
Test t3 = Test (3 );
T1.print ();
T2.print ();
T3.print ();
Test TA [3];
For (int I = 0; I <3; I ++)
{
TA [I]. print ();
}
}
When no constructor is defined in the class, the C ++ compiler automatically provides the non-argument constructor and the copy constructor.
When any copy constructor is defined in the class, C ++ does not provide a non-argument constructor.
Automatically provided Constructors
No-argument Constructor
The function body is empty.
Copy constructor
Simply copy the value of a member variable
Test t1;
Test t2 = t1; // t2 simply copies the value of the member variable in t1