Introduction to Pointers, Structures and Linked-Lists Part 8
This is just a short article that will help with the small but easy transition from structures to classes. There is very little difference between a class and a struct, and the differences I will not cover in this article until the "Inheritance" and "Polymorphism" articles. Here I will just go through the definition of a class (as copied from the MSDN library, the Microsoft Visual C++ definition of a class): class [tag [: base-list ]] { member-list } [declarators]; [ class ] tag declarators; Well, what does this syntax tell us about how we define a class? Let us stroll through what each part of the class definition process is:
I will go into inheritance later, so we can ignore the base-list part. The member-list stuff we have already seen in our struct's as they are the data and functions that can be used to all the ends of our requirements. One thing that I never covered in the previous article on Member functions was the public, private and protected access modifiers. Quite simply, they are seperators that you place throughout your class or structure that allow data flow only where you allow it. Here is an example: If you have our linked list that was defined in previously featured articles, and left them as they are, but allowed users (or third-party programmers) direct access through the way the linked list was set up, then they would be able to point our Next and Previous pointers to whatever they wanted to. Code that allows anybody to do this is bad code, and dangerous code. The program is likely to crash because it is pointing to some obscure memory address, or it could even crash your computer by overwriting some important data in memory unknowingly. I will not go into the details of data hiding at the moment (I will save that for my proposed next article, "Class Design") but I will breifly describe what the modifiers mean to us.
Mostly we store our member variables in the private section, and our interface functions (the functions that allow the users certain minimal access to data and how they may manipulate it) will be in the public section. There are occassions where private or protected functions will be created that aid our other class members, but are either too likely to be misused by the user or are of little use to the interface of the class. Cheers, Chris (a.k.a. Dwarfsoft) Author: Chris Bennett aka Dwarfsoft Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|