简体   繁体   中英

Why there is difference in constructor and destructor calls?

Header file: Date.h file

#include<iostream>
#include<cstdlib>   
#include<cstring>  

using namespace std;

class Date{

private:
    int day;
    int month;
    int year;

public:
    Date(int d = 1, int m = 1, int y = 1900): day(d), month(m), year(y) 
    {
        cout << "date constructor is called"<< endl;
    }

    void print() const {
        cout << day << ":" << month << ":" << year <<endl;
    }

    ~Date(){
        cout << "date destructor is called"<< endl;
    }

  };

Header file: Employee.h

#include"Date.h"

class Employee{

private:
    char *fname;
    char *lname;
    Date dob;       // object, has-a relationship
    Date hiredate;  // object, has-a relationship

public:
    Employee(char *f, char *l, Date bd, Date hd){ 
        cout << "employee constructor is called"<< endl;
        int lengthf;
        lengthf = strlen(f);
        fname = new char[lengthf+1];  
        strcpy(fname, f);

        int lengthl;
        lengthl = strlen(l);
        lname = new char[lengthl +1];
        strcpy(lname, l);

    }

    ~Employee(){
        delete [] fname;  
        delete [] lname;
        cout << "employee destructor is called"<< endl;
    }


   };

main() function :

#include"Employee.h"

int main(){

Date db(07, 11, 1991);
Date dh; 

dh.print();

Employee e("Dan", "Lee", db , dh);  


db.print();



system("pause");
return 0;
}

Once executed, we get this!

So, the question is as we can see there are 4 date constructors are being executed and then there is class Employee constructor is being called. Next, two date destructors are being executed. Now, when we get "press an key" option and once we press, we get a call of Employee destructor before 4 more date destructor calls. Thus, there are total 4 date constructor calls while 6 date destructor calls. However, class Employee calls one constructor and destructor.

**Why there is 4 date constructor calls and more destructor calls, 6 destructor calls? Further, can anybody elaborate the sequence and specify the points at where these constructor and destructor are being called, one by one.

Further, point to be noted, member objects are being passed to Employee constructor by value. But if we pass by reference, then there are 4 date constructor calls and 4 date destructor calls while one and one constructor and destructor calls of class Employee. Check the picture.

constructor and destructor calls when reference of objects is being passed to class Employee constructor

I'm a newbie, so it would be a great help. Thanks **

The two additional destructor calls are generated by the Date objects generated by the implicit copy constructors when passed by value to the Employee class constructor. These two new Date objects are destroyed as soon as the Employee constructor finishes.

When you pass by value, you are in fact creating another object for the function's lifetime.

Notice how you Pass by value two Data objects in Employee's Constructor? That means that two new Date objects are created up until Employee's constructor returns.

These values are called automatic, and once constructor exits they are automatically deleted,as a result their destructor's are called. This is where your two extra destructor calls come from.

Edit: You create 2 Date objects, so in fact 2 Date constructor calls

You then create an employee object, so you call employee's constructor and also another 2 Date object's constructors(since each employee object has 2 Date members )

Since employee's constructor is called, you have the 2 constructor calls i mentioned above, so total 6 Date construction calls, now once your employee's constructor ends, the Date object's that were created by "call by value" are deleted and thus their destructors are called, so total 2 destructors so far,

Once Employee object gets deleted,its 2 date objects get as well,so that's another 2 destructor calls.

Then another 2 Date objects are deleted( declared in main ) and thus another 2 destructor calls.

Good luck learning C++!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM