Constructor summary constructor

The constructor of the meaning of existence

  1. 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 object

      int main()
      {
          Tank t1;
          t1.init();
          Tank t2;
          t2.init();
    
          return 0;
      };
    
  1. Object Initialization type:
    1. And only once
    2. According to the conditions initialization
  2. Constructor - initialization function to avoid misuse
    1. Constructor is invoked automatically when an object is instantiated
    2. The constructor function of the same name
    3. The constructor has no return value (do not write void)
    4. Constructors can have multiple overloads
    5. Only a constructor object instance
    6. When the user does not define a constructor, the compiler automatically generates a constructor

Classification constructor

Constructor:

  1. General constructor:
    1. No-argument constructor -> Default Constructor
    2. There arg constructor:
      1. Parameters with default values ​​-> default constructor
      2. No default parameter values
  2. 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;
};
  1. Features initialization list
    1. Initialization list in the first implementation of the constructor
    2. Constructor initialization list can only be used
    3. The initial list of words can initialize multiple simultaneous data members
  2. 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

  1. By the same line when the object is instantiated additional objects, called automatically copy constructor
  2. Examples of object parameters is passed, triggering the copy process, the copy constructor automatically calls
  3. Copy function can not be overloaded! ! ! ! !


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11691100.html