Topic : C++ Tutorial
Author : Unknown
Page : << Previous 10  
Go to page :


( ~ObectNameHere ) that will be called when the variable is done with. And when we create a variable, the function ( ObjectNameHere() ) will be called. It will be done automatically for you. Also, note that for the constructor and deconstructor there are no return types. There is none, for only those two types of functions, all other functions, whether public or private, HAVE to have a return type. Now, as before, we have declared functions and implemented them. Well, we just declared a function in the class, now we have to implement it. Thats what we will go over how to do next.
This is the basic syntax:
<return type> <objectname>::<function name>
{
   // Whatever in here
}


And thats it. Its the same, whether private or public, they are both implemented the same exact way. So, if we were to implement our public function above, it would look like this:


void ObjectNameHere::PublicFunctionNameHere()
{
   // Do whatever in here
}



And thats that. Now that we know how to implement it, we now have to know how to access it, because we cant just call its name. Remember how we accessed the data members of a struct ? We use the same operator ( the selection operator ). So lets say that we wanted to access that function, we would do the variable name, the selection operator, and then the function name.

ObjectNameHere VariableName;
VariableName.PublicFunctionNameHere(); // Calling PublicFunctionNameHere()


And if we wanted to do the same thing to a PUBLIC data member, we do the exact same thing, we just replace the function name with the variable name.

Now, I am willing to bet that you are still confused on why you would want to use private and public and whatnot. Well, lets quickly explain why we would even want to use it.

As I said before, when we are creating an object, we are making what we call instances of variables in the computer memory. Now, the computer just knows the variable exsists and to give the information when needed. Now, what private does it kinda like a security feature. It will not let you have access to certain variables. For instance, lets say that we were making an ATM object, to imitate an ATM machine. Well, when the user inputs how much money they want from the machine, it will be first compared to how much they have. Now, lets say that we have that variable stored in memory, we dont want them to be able to change it, or they could make themselves richer than Bill Gates. So thats why we restrict access to it. Now, as you go on, you will find ways to allow certain ' rights ' to the variables.

Now that we know how to call functions, why dont we go over how to implement them. Remember how we have to declare a function, then actually write it. We have to do the same for a class.

Its easy, all you have to do is just follow the syntax, and you cant lose. It looks like this:

FunctionDataType ClassName::FunctionName(OptionalParameters) OptionalConst
{
     // Function Code Here !
}


So if we wanted to implement our function, PublicFunctionNameHere() from ObjectNameHere class, it would look like this.

// Assume that the object, ObjectNameHere, is already implemented
void ObjectNameHere::PublicFunctionNameHere()
{
     cout <<a " Hi ";
}


And thats it. We treat it like a everyday function, and with the ObjectNameHere:: we are basically just telling the computer that the function belongs to our object.

There is just two more things that I want to touch base upon before we leave this chapter. When I was telling you about the syntax to implement functions, I used the OptionalParameters and OptionalConst, well, I have to explain those. See, when we are calling a function, no matter what, even if you dont have any parameters in your function, there is ALWAYS a parameter passed into your function. Now, I have not gone over this, but what is passed is a Pointer. I will explain pointers in the next chapter, but its a pointer, named *this. For right now, I am not going to go any further than to basically explain that we are " pointing " to a space in the computer memory, where the information for our object is kept. I know it sounds confusing, but it will make more sence in the next chapter. Until next time, just dont worry about it, but I did want you to know that it does exsist.

Now, the const is just another security feature. If you put const on the end of a function, it will prevent you from changing ANY data within that function. So, no matter what, no data can be altered within a constant function.

Well, this ends another load of C++. Now, these chapters might not seem that long or important, but remember, this is the second, and there will be another one classes/objects soon. Im just breaking them up. Until next time, Program your ass off !



Page : << Previous 10