Author : Unknown
Page : << Previous 9 Next >>
This is why C++ is so powerful.
Remember the Array tutorial I wrote, if not, I suggest go read it again. It will be necessary for you to know how to work arrays before you start to get further into this chapter.
Well, remember how I told you that an object is nothing but a user-define data type. Well, just like int's and double's, they are used the same way. So, lets take our structure up above, called " Student " and put it to good use. So, to make life easy, lets take the entire class example.
So, first off, we declare an array, with our data type in it.
Student ClassArray[30]; // Will hold Class data
Now, we do the same thing to access the data, so for instance, if we wanted to get the 12th student's data, we would just use ClassArray[13]; ( dont forget we start at 0, not one ). Now, I bet you ask, what if we want just the id or just the gpa, and not both. Well, then we use our lovely little operator, called the Selection Operator, which is just a period. So, for instance, lets say we wanted the 12th student's GPA, and assign it to a variable. We would do this:
double TempGPAVariable = ClassArray[13].gpa; // That will "select" the gpa for the 12th student
We can do that for any variable inside of the structure. Remember, the data type ( double ) has to be the same, or it will type cast it, like I explained in the Array's Tutorial, and you will lose data.
Now, the same rule applies to your own data type as it does to the data type already built in. For you past them in the same, you can assign them ( but they HAVE to be the same data type, no type casting your own structs ). I will show an example of how to do basic things with structs, that you can do with the other basic data types.
Parameter Passing:
Lets say that you wanted to pass your struct, or just a member ofyour struct to a function, lets look on how to do that.
int GetID(Student); // Declaring function, Student is the name of the struct, and data type
Student Class[30];
... // Other Code here, int main(), etc..
int ClassID[30]; // Will hold just the ID's
for(int i = 0;i < 30;i++)
{
ClassID[i] = GetAge(Class[i]); // Pass in just one student
}
int GetID(Student LocalClass) // Passing in just one Student, not the entire class
{
return LocalClass.id; // Pointless, but it gets the point across
}
Now, just like if we were passing an int, we would have the int as the parameter data type, but since we are passing a " Student ", we use that instead, its our own data type. Now that function really serves any purpose, but it shows how to pass a structure which is the point of that example. Now, if we were just passing in one of the members of the structure, lets say the gpa. Instead of having the Student as the data type, we would put double, because that is the actual value we are passing. We dont want all of the info, just the gpa, so we would pass just the gpa. The gpa is not a Student, its a double. Lets look at a quick example.
void PrintGPA(double); // Declaring function
Student Class[30]; // Holds info
... // Other Code
PrintGPA(Class[14].gpa);
... // Other Code Here
void PrintGPA(double TempGPA)
{
cout << " The GPA is: " << TempGPA << endl;
}
Thats it. Now, all this does is print out the GPA of the 13th student. But the point is that we are not passing all of his informatin, like the id. We just want to GPA so thats all we pass. Simple as that. So its just like passing any other variable.
I believe that is all that there is to structs. You have the basic concept of it, so we are ready to move on. I know that this chapter was not that long, but trust me, its important that you understand what we are talking about. Like I said before, OOP is one of the most important parts of C++. Until next time, have fun and PROGRAM !
Welcome, to yet again, another fun filled day of programming. This time, we are going to go deeper into OOP. Remember what that stands for ? Object Oriented Programming. Now, struct as I said where really the OOP for C. Now, what we are going to learn next, is C++, not for C. This is the true power of C++. We are going to learn classes. Now, classes are similar in struct, but you get all the fun pins and buttons and every other nice thing with it. However, with all those fun extra's, we have to understand what they do and when to use them. Lets go over some quick ideas before we continue.
We know what an object is, and when we make a class, we are making an object, just as we did with the structs. You will notice some very similar properties of structs and classes, but understand, they are not the same. structs are kind of like the father of classes, even though classes are more powerful then structs. When we made a variable with our own data type, we were making an instance of that data type. Basically, when I say instance, its just saying that its there. There is an instance of it, so its there. There are some things that I have to discuss, so please bear with me as I explain these, for its very important.
- constructor Now, when we make an object, and we create a variable to represent that object, something has to happen to it, the computer does not know what to do. A special kind of function is called every time a variable is declared. Its done automatically for you, so you probably never knew. This is called a constructor. Basically, its a function that will initialize a variable. Lets say we were making a string class, when we made a constructor to tell the computer to make it an empty string, that is done through the constructor. If not, then you have a larger chance of obtaining programming errors. Now, for simple classes, if you do not write the constructor yourself, one is made for you as default.
- deconstructor This is another special kind of function that is called automatically. When creating a class, you dont HAVE to have this, but its almost required in some cases. Now, this is called when the variable is either deleted, or it " runs out of scope ". The goal of this function is to return any memory you might have borrowed. Again, if you dont write one, then a default one will be made for you, however, it wont give back the memory for you.
- public / private Now, this will be a little confusing without seeing some sample code, but bear with me. When we make an object, we can create what are called member functions. Basically, they are functions that are specifically made for your object. They are commonly referred to as methods also. Just like a member variable in a struct, the same goes for classes, they just have functions also. Now, when we make functions, we can restrict access to certain data and function calls by declaring a function/variable as public or private. When I show some sample code, this will make much more sense. For right now, lets simply say that its a way to restrict/protect certain data.
Now that we have the basic understanding of a simple class, lets go ahead and make a simple one. Look here:
class ObjectNameHere
{
public: // Note, its a : not a ;
ObjectNameHere(); // Constructor
~ObjectNameHere(); //DeConstructor
void PublicFunctionNameHere();
private: // Note, its a : not a ;
int PrivateDataHere;
char PrivateFunctionHere();
};
Now, we just made a simple class. Now, lets go over it. The ObjectNameHere would be the name of your object obviously. Now, note the public and private, both followed by a colon (:). This is what I was talking about restricting/protecting data. Basically, I am making two sections. I am saying that certain functions and data can be access/modified from outside of the object ( public ) and others can only be access/modified within the object ( private ). Now, the constructor and deconstructor have to be public. Now, you will notice that the constructor has the EXACT same name as the object name, and the deconstructor has the EXACT same name as the object, with the addition of a ~ (tilde). That is the deconstructor symbol. That is the function
Page : << Previous 9 Next >>