c++ class destructor deleting some but not all member data : <p>I've read through different resources but feel like I'm missing something. I'm explicitly calling the destructor when a variable (numItems) reaches zero. I have a loop that prints all the class objects (students), but while any object that had the destructor called on it has a blank first and last name, the ID and numItems variables still exist. Am I misunderstanding how the destructor works? Why would it delete some but not all of the member attributes?</p>

<p>Also, the "items" are stored in a dynamic array. I can set and access them as long as the array is public. But even using setters, the program crashes if I attempt to populate a private array. </p>

<p>Header:</p>

<pre><code>#ifndef STUDENT_H
#define STUDENT_H
#include &lt;string&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#define ARRAY_MAX 15
using namespace std;

class Student
{
private:
    string firstName, lastName;
    unsigned int ID, numItems = 0;
    typedef string* StringPtr;
    //StringPtr items;


public:
    int capacity = 15;
    string *items = new string[capacity];
    Student();
    Student(const unsigned int id, const string fName, const string lName);
    string getfName() const;
    string getlName() const;
    unsigned int getnumItems() const;
    string getItem(int num);
    unsigned int getID() const;
    void setfName(string fname);
    void setlName(string lname);
    void setID(unsigned int id);
    void setItem(string str, int num);
    int CheckoutCount();
    bool CheckOut(const string&amp; item);
    bool CheckIn(const string&amp; item);
    bool HasCheckedOut(const string&amp; item);
    void Clear();
    ~Student();
    //const Student operator+(string rhs);
    //void operator+=(string rhs);
    //bool operator==(Student rhs);
    friend istream&amp; operator&gt;&gt;(istream&amp; input, Student&amp; stu);
    friend ostream&amp; operator&lt;&lt;(ostream&amp; output, const Student&amp; stu);

};
#endif // STUDENT_H
</code></pre>

<p>Definitions:</p>

<pre><code>#include "student.h"

using namespace std;

Student::Student()
{
}

Student::Student(const unsigned int id, const string fName, const string lName)
{
    firstName = fName;
    lastName = lName;
    ID = id;
}

string Student::getfName() const
{
    return firstName;
}

string Student::getlName() const
{
    return lastName;
}

unsigned int Student::getnumItems() const
{
    return numItems;
}

string Student::getItem(int num)
{

    return items[num];
}

unsigned int Student::getID() const
{
    return ID;
}

void Student::setfName(string fname)
{
    firstName = fname;
}

void Student::setlName(string lname)
{
    lastName = lname;
}

void Student::setID(unsigned int id)
{
    if ((id &gt;= 1000) &amp;&amp; (id &lt;= 100000))
    {
        ID = id;
    }
    else
    {
        cout &lt;&lt; "Attempted ID for " &lt;&lt; firstName &lt;&lt; " " &lt;&lt; lastName &lt;&lt; " is invalid. Must be between 1000 and 100,000." &lt;&lt; endl;
    }
}

void Student::setItem(string str, int num)
{
    items[num] = str;
}

int Student::CheckoutCount()
{
    return numItems;
}

bool Student::CheckOut(const string &amp; item)
{
    if (this-&gt;HasCheckedOut(item) == true)
    {
        return false; // already found item in list, CheckOut failed...
    }
    else
    {
        items[numItems] = item;
        numItems++;
        return true; // CheckOut successful
    }

}

bool Student::CheckIn(const string &amp; item)
{
    for (int i = 0; i &lt; numItems; i++)
    {
        if (items[i] == item)
        {
            for (; i &lt; numItems - 1; i++)
            {
                // Assign the next element to current location.             
                items[i] = items[i + 1];
            }

            // Remove the last element as it has been moved to previous index.
            items[numItems - 1] = "";
            numItems = numItems - 1;
            if (numItems == 0)
            {
                this-&gt;~Student();
            }
            return true;
        }
    }
    return false;

}

bool Student::HasCheckedOut(const string &amp; item)
{
    string *end = items + numItems;
    string *result = find(items, end, item);
    if (result != end)
    {
        return true; // found value at "result" pointer location...
    }
    else
        return false;
}

void Student::Clear()
{
    ID = 0;
    firstName = "";
    lastName = "";
    delete[] items;
}

Student::~Student()
{
}

istream &amp; operator&gt;&gt;(istream &amp; input, Student &amp; stu)
{
    string temp;
    input &gt;&gt; stu.ID &gt;&gt; stu.firstName &gt;&gt; stu.lastName &gt;&gt; stu.numItems;
    int loopnum = stu.numItems;
    if (loopnum &gt; 0)
    {
        for (int i = 0; i &lt; loopnum; i++)
        {
            input &gt;&gt; temp;
            stu.setItem(temp, i);
        }
    }
    return input;
}

ostream &amp; operator&lt;&lt;(ostream &amp; output, const Student &amp; stu)
{
    string s = stu.firstName + " " + stu.lastName;
    output &lt;&lt; setw(8) &lt;&lt; stu.ID &lt;&lt; setw(16) &lt;&lt; s &lt;&lt; setw(8) &lt;&lt; stu.numItems;
    int loopnum = stu.numItems;
    if (loopnum &gt; 0)
    {
        for (int i = 0; i &lt; loopnum; i++)
        {
            output &lt;&lt; stu.items[i] &lt;&lt; "  ";
        }
    }
    output &lt;&lt; endl &lt;&lt; endl;
    return output;
}
</code></pre>

<p>Main:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include "student.h"
using namespace std;

void fcheck(ifstream &amp;mystream);

int main()
{
    ifstream sin("students.txt");           // File input/output variables
    ifstream fin("checkins.txt");
    ifstream chin("checkouts.txt");
    ofstream sout("UpdatedStudentsC.txt");
    Student stu1, stu2;

    fcheck(sin);
    fcheck(fin);
    fcheck(chin);

    typedef Student* StuPtr;
    StuPtr studentList;
    int stud_capacity = 50;
    studentList = new Student[stud_capacity];
    int num_studs = 0;


    sout &lt;&lt; std::setiosflags(std::ios::left); // justify output to format properly.
    while (sin.good())              // While there's data in the file, do stuff.
    {
        sin &gt;&gt; stu1;

        stu1.CheckIn("Towel");
        stu1.CheckIn("Locker");
        if (stu1.getnumItems() == 0)
        {
            stu1.~Student();
        }
        studentList[num_studs] = stu1;
        num_studs++;

        sout &lt;&lt; stu1; 
    }

    for (int i = 0; i &lt; 16; i++)
    {
        cout &lt;&lt; studentList[i].getID() &lt;&lt; " " &lt;&lt; studentList[i].getfName() &lt;&lt; "  " &lt;&lt; studentList[i].getnumItems() &lt;&lt;  endl;
    }

    system("pause");

    // Close files
    fin.close();
    chin.close();
    sout.close();
    sin.close();

    // Quit without error
    return 0;
}

void fcheck(ifstream &amp;mystream)
{
    if (!mystream)                          // If we can't find the input file, quit with error message.
    {
        cout &lt;&lt; "file not opened!" &lt;&lt; endl;
        system("pause");
        exit(1);
    }
}
</code></pre>
