|
Lesson 12: Introduction to Classes C++ is a bunch of small additions to C, and one major addition. This one addition is the object-oriented approach. As its name suggests, this deals with objects. Of course, these are not real-life objects. Instead, these objects are the essential definitions of real world objects. Structures are one step away from these objects, they do not possess one element of them: functions. The definitions of these objects are called classes. The easiest way to think about a class is to imagine a structure that has functions. What is this mysterious structure (not the programming type)? Well, it is not only a collection of variables under one heading, but it is a collection of functions under that same heading. If the structure is a house, then the functions will be the doors and the variables will be the items inside the house. They usually will be the only way to modify the variables in this structure, and they are usually the only to access the variables in this structure. From now on, we shall call these structures with functions classes (I guess Marx would not like C++). The syntax for these classes is simple. First, you put the keyword 'class' then the name of the class. Our example will use the name computer. Then you put an open bracket. Before putting down the different variables, it is necessary to put the degree of restriction on the variable. There are three levels of restriction. The first is public, the second protected, and the third private. For now, all you need to know is that the public restriction allows any part of the program, including that which is not part of the class, access the variables specified as public. The protected restriction prevents functions outside the class to access the variable. The syntax for that is merely the restriction keyword (public, private, protected) and then a colon. Finally, you put the different variables and functions (You usually will only put the function prototype[s]) you want to be part of the class. Then you put a closing bracket and semicolon. Keep in mind that you still must end the function prototype(s) with a semi-colon. Classes should always contain two functions: the constructor and destructor. The syntax for them is simple, the class name denotes a constructor, a ~ before the class name is a destructor. The basic idea is to have the constructor initialize variables, and to have the destructor clean up after the class, which includes freeing any memory allocated. The only time the constructor is called is when the programmer declares an instance of the class, which will automatically call the constructor. The only time the destructor is called is when the instance of the class is no longer needed. When the program ends, or when its memory is deallocated (if you do not understand the deallocation part, do not worry). Keeps in mind this: NEITHER constructors NOR destructors RETURN AN ARGUMENT! This means you do not want to try to return a value in them. The syntax for defining a function that is a member of a class outside of the actual class definition is to put the return type, then put the class name, two colons, and then the function name. This tells the compiler that the function is a member of that class. For example: void Aclass::aFunction() { cout<<"Whatever code"; } #include <iostream.h> class Computer //Standard way of defining the class { public: //This means that all of the functions below this //(and variables, if there were any) //are accessible to the rest of the program. //NOTE: That is a colon, NOT a semicolon... Computer(); //Constructor ~Computer(); //Destructor void setspeed(int p); int readspeed(); //These functions will be defined outside of the class protected: //This means that all the variables under this, //until a new type of restriction is placed, //will only be accessible to other functions in the //class. NOTE: That is a colon, NOT a semicolon... int processorspeed; }; //Do Not forget the trailing semi-colon Computer::Computer() { //Constructors can accept arguments, but this one does not processorspeed = 0; //Initializes it to zero } Computer::~Computer() { //Destructors do not accept arguments } //The destructor does not need to do anything. void Computer::setspeed(int p) { //To define a function outside put the name of the function //after the return type and then two colons, and then the name //of the function. processorspeed = p; } int Computer::readspeed() { //The two colons simply tell the compiler that the function is part //of the class return processorspeed; } int main() { Computer compute; //To create an 'instance' of the class, simply treat it like you would //a structure. (An instance is simply when you create an actual object //from the class, as opposed to having the definition of the class) compute.setspeed(100); //To call functions in the class, you put the name of the instance, //a period, and then the function name. cout<<compute.readspeed(); //See above note. return 0; } As you can see, this is a rather simple concept. However, it is very powerful. It makes it easy to prevent variables that are contained (or owned) by the class being overwritten accidentally. It also allows a totally different way of thinking about programming. I want to end this tutorial as an introduction, however. |