Classes are the core of C++; when you get into programming for Windows, you'll find that pretty much everything you work with will be a class. Furthermore, all of the work you'll be doing will probably be within a class. This is because classes are the embodiment of object oriented programming: the bread and butter of most modern programming ideas. The idea behind object oriented programming (henceforth known as "OOP") is that, like structs, all of the program's information is encapsulated and instantiated. Most everything you see is a class of some sort, from the program you're using down to the dialog boxes and menus that the program uses -- whenever you invoke the program or its menus, you are asking the computer to generate an instance of that specific class. This allows programs to be event driven; they only perform operations when you ask them to, rather than constantly running sequentially like we're doing here.
But I digress. A class, like a struct, simply encapsulates information. Rather than simply encompassing variables like structs, though, classes hold functions as well. These are called member functions/variables. Finally, you're allowed to set permissions for the code you write. You can restrict usage of the member functions or variables to other member functions within the class (i.e., make them private) or allow any other outside program to invoke the information directly (i.e., make them public.) Here's a simple database class to get us started.
class Database
{
public:
Database();
Database(Database & copy);
Database(int value, char name[]);
int getID();
void setID(int value);
char * getName();
void setName(char name[]);
private:
int myID;
apstring myName;
};
Now to make sense of all this. First, notice how the class is declared very similarly to a struct. Simply use the keyword class followed by the class's name. Within the declarations are two "sections." The public and private sections. Within each sections are the public and private members, respectively. In the public section, you'll see some strange looking functions which have the same name as the class and no data type. These are called constructors. When an instance of the class is declared, one of these functions is called depending on how many parameters the declaration has (you'll see what I'm talking about next lesson). You can have as few or as many as you like, though you generally want to at least have the default constructor. Within constructors, you simply initialize any variables that are within the class for later use. You can do whatever you want in them, but they exist for the purpose of preliminary initialization. In the private section, you'll have all of the variables and functions that you want to restrict access to.
As for the normal member functions and variables, you can have as many or as few as you like; you can make them all public or private (though that would be pointless; it would be completely inaccessible to the program!). Aside from the constructors, the functions and variables seen here are just like the prototypes or declarations you'd see anywhere else.
Now to actually create our member functions. Their headers follow the template "data type class name::function name(parameters)." You may remember that with structs, you have to use the class's variable name with a period in order to access the "inner variables." Such is the case here as well (even for member functions), but when you're already in a member function, every piece of data within the class is globally available to it. Here are all of the member functions.
Database::Database()
{
myID = 0;
strcpy(myName,"");
}
Database::Database(Database & copy)
{
myID = copy.getID();
strcpy(myName,copy.getName());
}
Database::Database(int value, char name[])
{
myID = value;
strcpy(myName,name);
}
int Database::getID()
{
return myID;
}
void Database::setID(int value)
{
myID = value;
}
char * Database::getName()
{
return myName;
}
void Database::setName(char name[])
{
strcpy(myName,name);
}
Simple enough, right? If you look below the three constructors, you'll see that template I mentioned above in action. Since the constructors have no data type, you simply omit that part in their headers. Anyways, the first constructor is known as the default constructor; it assigns generic values to its member variables. The second one is a copy constructor. It is called if you set a Database object equal to another one. This is a good example of how classes work. Since its own members are globally available, it can simply call them as they are. In the copy, though, we had to use the variable name prior to calling its members. The third constructor is called a custom constructor, wherein the user can set initial values him or herself. Next we have a few get and set functions. These are called accessor functions, and in real world applications they would be the functions prompting you for a password; they are essentially a line of defense from which you can determine whether or not the user is allowed to access your private members.