lightatdawn
#include <stdio.h> //for printf #include <string.h> //for strcpy //------------------------------------------ // CLASSES: Its a beautiful OOP thing //------------------------------------------ /* This is the definition of our CDisplay class. Normally this would be in a header file (probably CDispaly.h) and we would include that file in any source file that we wanted to use CDisplay in. We're not doing that in this particular instance as it would make this page more confusing. The first thing you notice about a class is that it looks a lot like a struct. Yup. Very very simular. In fact you could replace the word "class" below with "struct" and this application would compile and execute idendically. There is only one technical difference between a class and a struct. A classes members are private (accessable only to itself) be _default_ while a structs members are public (accessable to all) by _default_. But since its always a good idea to explicitly declare members as 'public' and 'private' (as we've done below), this makes no difference whatsoever. The other difference between a class and a struct is how it is percieved. It's a common rule of thumb to use structs for data storage (i.e. the linked list example), and classes for a specific function. In the end though, its just preference. */ class CDisplay { public: /* This section of CDisplay will be public. The functions here, and any variables we wanted to be, are accesable normally, just like we used a struct. The first two functions are unusual and unique. The first is a constructor. It is called automatically when an instance of the class is created. In this case the function will be called as soon as main() starts. The second is the destructor. It is called when the class is deleted or goes out of scope (i.e. When the program ends). The destructor can be called explicitly (like any other function), but the constructor can not be called by the programmer. */ CDisplay(); ~CDisplay(); /* These are the functions we're going to use. The first will display the text passed to it. The second is an overloaded function. If Print is called without an argument, it will display the last thing it has displayed. */ void Print(char * Say); void Print(); private: /* This data is private. It cannot be accessed from outside the functions of this class. Attempting to do so will cause your compiler to generate an error. LastSaid will point to a section of memory containing the last displayed string. The ClearMemory() function will delete this memory, and the Remember() function will record the last displayed string when requested. */ char * LastSaid; void ClearMemory(); void Remember(char * Say); }; //------------------------------------------ // CDISPLAY CONSTRUCTOR //------------------------------------------ CDisplay::CDisplay() { /* When an instance of CDisplay is created, this Constructor will be called automatically. We set LastSaid to NULL for safety at this point. */ LastSaid = NULL; } //------------------------------------------ // CDISPLAY DESTRUCTOR //------------------------------------------ CDisplay::~CDisplay() { /* When the CDisplay object goes out of scope (when the program ends), this destructor will be called automatically. The destructor in turn calls ClearMemory() to delete any memory left behind. This prevents any accidental memory leaks and frees the programmer from actually having to explicitly call for its deletion. */ ClearMemory(); } //------------------------------------------ // CDISPLAY PRINT //------------------------------------------ void CDisplay::Print(char * Say) { //Print what we're told printf(Say); /* Call the private function Remember(char * Say); which will remember this string in case Print() is called without and argument and we have to display the last string. Notice that Remember(...) can be called normally from inside this CDisplay member function, but cannot be called from main. Thats the nature of private member functions. */ Remember(Say); } //------------------------------------------ // CDISPLAY PRINT (overloaded) //------------------------------------------ void CDisplay::Print() { /* This is the overloaded member function of CDisplay. If Print is called without an argument, this function will execute, and the last requested string before this will be printed. Notice that we check to make sure a string _has_ been requested before this. */ if (LastSaid != NULL) printf(LastSaid); } //------------------------------------------ // CDISPLAY CLEARMEMORY //------------------------------------------ void CDisplay::ClearMemory() { //If any memory is pointed to by LastSaid, we delete it if (LastSaid != NULL) delete LastSaid; } //------------------------------------------ // CDISPLAY REMEMBER //------------------------------------------ void CDisplay::Remember(char * Say) { /* This function first called ClearMemory(). This frees up any memory that might have been allocated before this. We dont want to leave allocated memory blocks lying around that we're not using, now do we? */ ClearMemory(); //Now we allocate enough memory for the new string LastSaid = new char[strlen(Say) + 1]; //And copy over the latest string strcpy(LastSaid, Say); } //------------------------------------------ // MAIN //------------------------------------------ int main(void) { /* This is where we create the instance of our class. Right here is where the constructor will execute, setting LastSaid to NULL. Notice that nothing is unusual about this definition. Exactly the same a a struct or any other data type. */ CDisplay ToScreen; //Now we use the Print(char * Say); function of the CDisplay instance. ToScreen.Print("Hello World\n"); //Now we call the overloaded function ToScreen.Print(); //Attempting to call either ClearScreen() or Remember() from here will result //in a compilation error. Likewise so will accessing LastSaid. Give it a try. //At this point the destructor for ToScreen will be called and the memory //allocated to LastSaid will be deallocated. Jobs done. Wasnt that easy? return 0; }