C ++ Constructor
Constructor is a special member function. If you create a new object of the class type, you must execute the constructor. The constructor ensures that the data members of each object have the appropriate initial values.
URL: http://www.cnblogs.com/archimedes/p/cpp-constructor.html
C ++ uses constructor for object initialization:
Function: Initialize an object.
Function Name: Same as the class name
Parameter: None
Return Value: do not specify the return value (void cannot be entered)
Location: usually as a public Member
Content: arbitrary, usually containing only the Member Assignment Statement
Call method: it is usually automatically called when an object is created.
Time time;Time time = Time();//OK
Constructor in C ++ is mainly divided into four types:
No-parameter constructor, parameter constructor, constructor and parameter initialization list, default constructor,
No-argument Constructor
Note:
When instantiating an object, the automatically called object name cannot contain brackets "Class Name Object Name ()". In the constructor, you can not only assign initial values to data members, other statements can also be included (Statements irrelevant to initialization are not recommended)
#include<iostream>using namespace std;class Time{ public: Time(); void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};Time::Time(){ m_iHour = 0; m_iMinute = 0; m_iSec = 0;}void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time; time.display(); Time obj = Time(); obj.display(); Time *p = new Time; p->display(); delete p; p = NULL;return 0;} Constructor with Parameters
#include<iostream>using namespace std;class Time{ public: Time(int aHour, int aMinute, int aSec); void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};Time::Time(int aHour, int aMinute, int aSec){ m_iHour = aHour; m_iMinute = aMinute; m_iSec = aSec;}void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time(12,30,20); time.display(); time = Time(0,0,0); time.display(); Time *p = new Time(1,2,3); p->display(); delete p; p = NULL;return 0;}
Because constructors can contain parameters, they can be overloaded.
#include<iostream>using namespace std;class Time{ public: Time(); Time(int aHour, int aMinute, int aSec); void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};Time::Time(){ m_iHour = 0; m_iMinute = 0; m_iSec = 0;}Time::Time(int aHour, int aMinute, int aSec){ m_iHour = aHour; m_iMinute = aMinute; m_iSec = aSec;}void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time; time.display(); Time Time(12,30,20); Time.display(); system("pause"); return 0;} When you overload constructors, you must avoid ambiguity.
Time (); // declare the Time (int, int) of the constructor without parameters; // Time (int, int = 10, int = 10) of the constructor with two parameters ); // one parameter is not the default parameter... ... Time time1; Time time2 (15); // call the third Time time3 (15, 30); // Error, call the second or third Time?
Constructor and initialization list
Time::Time(int aHour, int aMinute, int aSec):m_iHour(aHour),m_iMinute(aMinute),m_iSec(aSec){}
Note:
The const, referenced, and sub-object members must be initialized using the initialization list.
class ConstRef{ public: ConstRef(int aVal); private: int m_iVal; const int m_ci; int &m_r;};ConstRef::ConstRef(int aVal):m_ival(aVal){ m_ci = aVal; //Error m_r = m_iVal; //Error}
The initialization list only appears during implementation;
class ConstRef{ public: ConstRef(int aVal); private: int m_iVal; const int m_ci; int &m_r;};ConstRef::ConstRef(int aVal):m_iVal(aVal), m_ci(aVal), m_r(m_iVal){}int main(){ ConstRef obj(4); return 0;}
The initialization order cannot be specified in the constructor initialization list.
class Point{ public: Point(int aVal):m_iY(aVal), m_iX(m_iY){} //Error private: int m_iX; int m_iY;};
Compile the constructor initialization list in the order of member declarations. Avoid using members to initialize members.
class Point{ public: Point(int aVal):m_iX(aVal), m_iY(m_iX){} private: int m_iX; int m_iY;};Default constructor
Concept: constructor called when no parameter is provided during object initialization
int main(){ Time time1; Time time2(); //error Time &r = *new Time; delete Time; return 0;}
Custom constructor, constructor created by the system, constructor with default parameters
Custom constructor:
#include<iostream>using namespace std;class Time{ public: Time(); void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};Time::Time(){ m_iHour = 0; m_iMinute = 0; m_iSec = 0;}void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time; time.display(); Time obj = Time(); obj.display(); Time *p = new Time; p->display(); delete p; p = NULL; system("pause"); return 0;}
Custom constructor:
If no constructor exists in the class body, the system generates a default constructor.
#include<iostream>using namespace std;class Time{ public: void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time; time.display(); system("pause"); return 0;}
If the class body contains a constructor, the system does not generate a default constructor. The default constructor created by the system is a non-parametric constructor with the function body empty. Time: Time (){}
Constructor with default parameters:
#include<iostream>using namespace std;class Time{ public: Time(int aHour=0, int aMinute=0, int aSec=0); void display() const; private: int m_iHour; int m_iMinute; int m_iSec;};Time::Time(int aHour, int aMinute, int aSec){ m_iHour = aHour; m_iMinute = aMinute; m_iSec = aSec;}void Time::display()const{ cout << m_iHour << ":" << m_iMinute << ":" << m_iSec << endl;}int main(){ Time time1; time1.display(); Time time2(12,30,20); time2.display(); system("pause"); return 0;}
Note:
The parameter name can be omitted when the constructor is declared (not recommended)
Time (int = 10, int = 10, int = 10 );
A class can have only one default constructor.
Time (); // declare a constructor without Parameters
Time (int aX = 10, int aY = 10, int aZ = 10 );
Time box1; // ERROR
We recommend that you use the default constructor of the default parameters to improve code reuse.
A class can contain only one default constructor.
Summary
The name of the constructor is the same as the class name. Therefore, no return value type can be specified, including void.
The constructor is usually public.
Constructor is a member function that can be implemented in the class body or in vitro.
The constructor can be overloaded to avoid "ambiguity" during overload ".
It is best to use the parameter initialization list to implement the constructor.
The initialization list is displayed when the constructor is out of class!
Constructors with no parameters and all parameters contain default values are Default constructors.
A class can contain only one default constructor.
Traditionally, a default constructor with default parameters must be defined to enhance the versatility of the class.