Introduction to Pointers, Structures and Linked-Lists Part 8

Published September 30, 2004 by Chris Bennett aka Dwarfsoft, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement

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:

What this means: class is the type that we are specifying, this works exactly the same as a struct or a union would work. tag is an identifier so that we can create more instances of our class. base-list a list of classes that this class inherits member-list a list of member functions and data that this class uses declarators declarators are an instance of the class, and are a usable variable. Eg: The declarator in the expression "int i;" is i

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.

Access Modifier Action public In this section we allow anybody and everybody to call the functions, or allows access to the variables that are listed (not recommended for everything) private Only the class itself and friends of the class have access to this, but we will avoid talking about function friends for now. This is where it is recommended that you store all class member variables. protected Protected functions and data can be accessed by derived classes without the need for a specific friend declaration. This is where you put data for sharing information which is useful, but users will not need to know about.

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
Contact: dwarfsoft@hotmail.com
February 24th, 2002
(C) Copyright Chris Bennett, 2002

Cancel Save
0 Likes 0 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement