1)
Constructor - The function of the constructor is to create your object. Just like a house needs to be constructed before you live in it, so does your object need to be constructed before you can use it. So in the constructor we usually will define variables to default values or build other variables so they are ready for use.
Destructor - The function of a destructor is to destroy parts of an object that otherwise couldnt be destroyed by the default destructor (the one that the compiler automatically creates for you). Usually you only build a destructor if you have dynamically allocated memory from the heap (used the new operator, which asks the operating system for memory during runtime of your program). If you don't destroy the the dynamically allocated type (such as int, or string or even your class Arrays), you have something called a memory leak. memory leaks could cause your system to slow down or eventually crash if it is bad enough. So when I wrote arrayList = new int [num], I knew that when my class went out of scope I would need to free that memory up. That is why I built a destructor in the class.
2) When you are programming a class, such as maybe a character in a game, you dont want a programmer (or even yourself) to be able to screw up the characteristics of your character. So, you make certain information private so you dont have direct access to it (not being able to use the private member data outside the member functions). It provides a layer of protection and security for your variables. Good practice is to put all your variables in the private section, and even some functions that you dont want other people to be able to directly call.
3) Well cin >> this->arrayList[i] is just putting whatever the user typed in into the array at element i. So if I was 1 then whatever the user typed in would go to the element 1 in the array. In all reality you dont need 'this->'. It is called the 'this' pointer and is created for you to use everytime a object calls its member function. I just use it to help denote what is the objects member variables so I dont get confused. The arrow operator (->) is a quick way to dereference the pointer (dereference something is saying give me whatever is at the address the pointer is pointing to. In this case it was the calling object.). You can just use arrayList [i] though if you are not comfortable with the 'this' pointer.
4) If you ask a user to input something, that person has the option of inputting whatever is on his/her keyboard. When you ask the user to input something the variable that will catch their input is called input_two, which you delcared to be of type int. So you are expecting an integer only. Well if the user types, 'hello' that cannot be caught by an integer type variable. So cin says whoa thats not going to work and sets its fail bit to true (which means some input failed). You can check to see if the fail bit is set to true by calling cin's member function fail(). It works just like any of your member functions except this returns a boolean value (either true meaning fail bit is set or false meaning fail bit is not set).
5) printTest() was called when I ran it.
By the way I noticed a glaring error that I made in the program:
You are going to need to change the entire getvalues() function to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void Arrays :: getValues()
{
int input_two = 0;
bool key = true;
while (key)
{
cout << "Which value would you like to view? ";
cin >> input_two;
if (!cin.fail() && input_two <= sizeOfArray)
{
input_two = input_two - 1;
cout << arrayList[input_two] << endl;
}
else
{
cin.clear();
cin.ignore (256, '\n');
key = false;
}
}
}
|
if you want it to work properly. Haha, no ones perfect I guess. By the way clear() and ignore are also member functions of cin. clear() resets the fail bit and ignore extracts characters out of cin's buffer so they dont mess up your program.