Constructors: each time the object is created called, there is no return value (including void) can be used to set the initial values for certain member variable.
The following are several different constructors:
Common constructor:
1 .h: 2 3 explicit MainWindow(QWidget *parent = 0); 4 5 .c: 6 7 MainWindow::MainWindow(QWidget *parent) : 8 QMainWindow(parent), 9 ui(new Ui::MainWindow) 10 { 11 ui->setupUi(this); 12 } 13 14 .h: 15 16 explicit Widget(QWidget *parent = 0); 17 18 .c: 19 20 Widget::Widget(QWidget *parent) : 21 QWidget(parent), 22 ui(new Ui::Widget) 23 { 24 ui->setupUi(this); 25 }
Constructor with parameters:
The default constructor has no parameters, but if necessary, the constructor can have parameters. This will assign an initial value to the object when the object is created.
example:
. 1 #include <the iostream> 2 . 3 the using namespace STD; . 4 . 5 class Line . 6 { . 7 public : . 8 void the setLength ( Double len); . 9 Double getLength ( void ); 10 Line ( Double len); // this is the constructor 11 12 is Private : 13 is Double length; 14 }; 15 16 // member function definitions, including the constructor . 17 Line :: Line (double len) 18 { 19 cout << "Object is being created, length = " << len << endl; 20 length = len; 21 } 22 23 void Line::setLength( double len ) 24 { 25 length = len; 26 } 27 28 double Line::getLength( void ) 29 { 30 return length; 31 } 32 // 程序的主函数 33 int main () 34 is { 35 Line Line ( 10.0 ); 36 37 [ // get the length of the default settings 38 is COUT << " the Length of Line: " << line.getLength () << endl; 39 // set again the length 40 line.setLength ( 6.0 ); 41 is COUT << " the Length of Line: " << line.getLength () << endl; 42 is 43 is return 0 ; 44 is }
In C ++, Explicit keyword is used to modify the class constructor, modified class constructor can not occur in the corresponding implicit type conversion, so as to display only the type conversion.
Use initialization list to initialize the field:
Equivalent wording following two:
Line::Line( double len): length(len) { cout << "Object is being created, length = " << len << endl; } Line::Line( double len) { length = len; cout << "Object is being created, length = " << len << endl; }
Common destructor: it will be executed each time the object is created deleted. The class name as the destructor is identical, only preceded by a tilde (~) as a prefix, it does not return any value, it can not have any parameters. Destructor helps exits the routine (such as closing the file, the release of memory, etc.) prior to the release of resources.
E.g:
. 1 #include <the iostream> 2 . 3 the using namespace STD; . 4 . 5 class Line . 6 { . 7 public : . 8 void the setLength ( Double len); . 9 Double getLength ( void ); 10 Line (); // this is the constructor declaration 11 ~ Line (); // this is declaration destructor 12 is 13 is Private : 14 Double length; 15 }; 16 . 17 // 成员函数定义,包括构造函数 18 Line::Line(void) 19 { 20 cout << "Object is being created" << endl; 21 } 22 Line::~Line(void) 23 { 24 cout << "Object is being deleted" << endl; 25 } 26 27 void Line::setLength( double len ) 28 { 29 length = len; 30 } 31 32 doubleGetLength :: Line ( void ) 33 is { 34 is return length; 35 } 36 // main function program 37 [ int main () 38 is { 39 Line Line; 40 41 is // Set the length 42 is line.setLength ( 6.0 ); 43 is COUT < < " the Length of Line: " << line.getLength () << endl; 44 is 45 return 0 ; 46 is }