c++

Constructor

  • Declaration of Constructor

    1
    2
    3
    4
    class Human
    {
    Human(); //declaration of a constructor
    };
  • Implementation of Constructor

    The first case: Implementation in class

    1
    2
    3
    4
    5
    6
    7
    class Human
    {
    Human()
    {
    //constructor code here
    }
    };

    The second case: Implementation out class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class Human
    {
    public :
    Human(); //constructor declaration
    };
    //constructor implementation (definition)
    Human::Human() //'::': Domain Resolution Operator
    {
    //constructor code here
    }
  • When and How to use Constructor?

    When creating object, which made constructor a ideal place to initialize class menmber variables to a specific value.

    • When you invoke a function without providing parameters which is called Default Constructor.
    • And the Default Constructor is optional,if you don’t provide one, compiler will creat one for you, it just create member attribute, but not initialize POD type(such int) to a non-zero value.
  • Overload Constructor

    Like function, Constructor can also Overload.(overload func, the same func, which you can provide different parameter such as you can do not provide or provide a string or int)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class Human 
    {
    Human()
    {
    //default constructor code here
    }
    Human(string humansName)
    {
    //overload constructor code here
    }
    }

    The following program will tell you

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #include <iostream>
    #include <string>
    using namespace std;

    class Human
    {
    private:
    string name;
    int age;
    public:
    Human() //You can regard this as Setter
    {
    age = 0;
    cout << "Default constructor: name and age not Set" << endl;
    }

    Human(string humansName, int humansAge) //You can regard this as Getter and Setter!!!
    {
    name = humansName;
    age = humansAge;
    cout << "Overload constructor creates" << endl;
    cout << name << " of" << age << " years" << endl;
    }
    };

    int main()
    {
    Human firstMan; //use default constructor
    Human firstWoman("Eve",20); //use overload constructor
    }
    • You can choose do not implement Default constructor, so you must provide some parameters when instantiating object.
  • You do not need default constructor and just keep Overload constructor.

    And it can have default value.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    Human(string humansName,int humansAge)
    {
    //...
    }
    Human(string humansName, int humansName = 20)
    {
    name = humansName;
    age = humansAge;
    }

    //By the way, overload constructor can cantain initialzation list.
    Human(string humansName, int humansAge)
    :name(humansName), age(humansAge)
    {
    //cout name,age...
    }
Share