Declaring Objects
When one works with classes, each instance of the class which is defined is known as an object. C++ is an extremely object oriented language (perhaps not so much so as Java, however). In OOP, or Object Oriented Programming, one can easily perform multiple tasks on objects which are created. For example, when one declares an instance of the class, they can enact numerous functions upon that instance of the class.
Structures
Structures, or structs, are a simpler form of a class. They allow the creation of objects which handle multiple variables, but no functions. For such a reason, all variables within a struct can be considered public to the entire program. While structure instances are declared the same was as class instances, one can only work upon a variable in a struct (as opposed to variables and functions in a class).
struct student
{
char[10] name;
float gpa;
};
The above is a simple structure which handles students. By declaring student Jason; for example, the variable Jason is created of type student. This is similar to how an instance of a class is created. One can deal with the name and gpa of Jason by simply using Jason.name[] and Jason.gpa.
Pointers
In its simplest form, RAM (random access memory) can be throught of as a huge series of slots with addresses, each containing different data used by the computer. Pointers are directly related to a computer's use of variables stored in RAM. A pointer is an address of where data is stored in memory.
Pointers allow the memory management available in C++ to really go into full swing. You can theoretically choose the exact place in memory you wish for objects to occupy. In addition, you can overwrite objects in memory, copy memory addresses, etc.
Passing By Reference
It is possible to pass variables by reference into a function. When passing by reference, only the memory address of a variable is being passed in. Variables are passed by reference by following their data types with an ampersand (&) symbol.
Of course, when variables are being passed into a function, they act simply like local variables. Theoretically, copies of these variables are created in RAM and these copies are used inside of the function. If their values are altered in any way, it is only temporary. As soon as the function runs its course, the original values are used. For this reason, the same variable name can exist in multiple functions, each storing a different value or even data type.
All of this is different with variables which are passed by reference. When a variable is passed into a function by reference, changes made to the variable are permanently altered in the function calling the function in question.
When one uses a return statement in a function, they can only return one piece of data or one variable. However, suppose one needed to write a function to return multiple variables. That is when passing by reference comes into play. This can be accomplished by writing a void function, thus with no return statement (or just a return; statement with no value.) Pass any number of variables by reference into this void function. Then, any changes made inside the function will be directly altered from where it was called from. This is almost like a workaround for only being able to use make use of one return statement per function call.
void square(int size, int& area, int& perimeter)
{
area = size ^ 2;
perimeter = size * 4;
return;
};
In the above program, the size variable is used to represet the size of one side of a square. This variable is being passed into the function normally. The area and perimeter variables, however, are being passed by reference. This is because the purpose of this function is to compute both the area and perimeter and send them back to the function which called square(). This is possible by passing these two parameters by reference.