The constructor of the meaning of existence
- When instantiated object, the data object needs to be initialized.
-
First, construct a class
class Tank { private: int m_iPosX; int m_iPosY; public: void init() { m_iPosX = 0; m_iPosY = 0; } };
-
Examples of two objects, and by
void init()
initializing the objectint main() { Tank t1; t1.init(); Tank t2; t2.init(); return 0; };
- Object Initialization type:
- And only once
- According to the conditions initialization
- Constructor - initialization function to avoid misuse
- Constructor is invoked automatically when an object is instantiated
- The constructor function of the same name
- The constructor has no return value (do not write void)
- Constructors can have multiple overloads
- Only a constructor object instance
- When the user does not define a constructor, the compiler automatically generates a constructor
Classification constructor
Constructor:
- General constructor:
- No-argument constructor -> Default Constructor
- There arg constructor:
- Parameters with default values -> default constructor
- No default parameter values
- Copy constructor
Whatever the constructor initialization list has a vital role
General constructor
- No-argument constructor
class Student { public: Student(){m_strName = "jim";} private: string m_strName; };
- There arg constructor
class Student { public: Student(string name) {m_strName = name;} private: string m_strName; };
- Overloaded constructor (must follow the rules overloaded function)
class Student { public: Student(){m_strName = "jim";} Student(string name) {m_strName = name;} private: string m_strName; };
- Default constructor: constructor parameters need to pass
Initialization list
class Student
{
public:
Student(): m_strName("jim"), m_iAge(10){}
private:
string m_strName;
int m_iAge;
};
- Features initialization list
- Initialization list in the first implementation of the constructor
- Constructor initialization list can only be used
- The initial list of words can initialize multiple simultaneous data members
- There is a list of initialization large column Constructor summary constructor necessity
class Circle { public: Circle(){m_dPi = 3.14;} // 编译报错,不能给const第二次赋值 private: const double m_dPi; // 圆周率pi为固定值 };
class Circle { public: Circle():m_dPi(3.14){} // 编译通过,使用初始化列表初始化const private: const double m_dPi; };
Another form
class Circle { public: Circle(double pi):m_dPi(pi){} // 编译通过,传入的形参通过初始化列表进行赋值 private: const double m_dPi; };
Copy constructor
***** Teacher Header ****
include <iostream>
include <string>
using namespace std;
class Teacher
{
public:
Teacher(string name = "jim", int age = 1);
Teacher(const Teacher &tea);
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
*** Teacher Header end ***
***** Teacher Cpp ********
include <Teacher.h>
include <iostream>
include <string>
using namespace std;
Teacher::Teacher(string name, int age):m_strName(name), m_iAge(age)
{
cout << "Teacher(string name, int age)" << endl;
}
Teacher::Teacher(const Teacher& tea)
{
cout << "Teacher(const Teacher& tea) << endl;
}
void Teacher::setName(string name)
{
m_strName = name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge = age;
}
int Teacher::getAge()
{
return m_iAge;
}
***** Teacher Cpp end *****
********** test ***********
include <Teacher.h>
include <iostream>
include <string>
using namespace std;
void test(Teacher& t)
{
cout << t.getName << " " << t.getAge << endl;
}
int main()
{
Teacher t1;
Teacher t2(t1);
Teacher t3 = t1;
test(t1);
return 0;
}
******** test end *********
Characteristics of the copy constructor
- By the same line when the object is instantiated additional objects, called automatically copy constructor
- Examples of object parameters is passed, triggering the copy process, the copy constructor automatically calls
- Copy function can not be overloaded! ! ! ! !