First off, I'm not going to elaborate too much because you should already know a basic understanding of hwo to program. If not, then I suggest googling programming in c++ because this might be a little over your heads. Anyway, I'm going to go over some of the details involved in creating a pserver in c++. Mainly some of the procedures and what they mean, and how to work with files and headers.
1. Files and Headers
When you create a new file in c++ its always a good practice to set up a corresponding header for all the variable, function and method declarations. For example if you have a file called server.cpp, you should create a header named server.h. This tells the compiler to look for the header of the same name to compile all the instructions for the cpp file. An example would be if you have a function in the cpp file and you don't declare it in the header, the compiler will give a linkage error at the end of compilation. Remember to declare all the variables and functions in the header file and then use the functions in the cpp file.
Let's look at an example. I'm going to use PasteBin for these examples so let me know if the pages get removed after a period of time.
So here is an example of a header file and its corresponding cpp file. I will be using Selector from the Connection project in the login server solution. Note that the connection project is shared across both solutions.
Selector.h
Selector.cpp
In the header file you will see that it defines the Selector class and inside the class it has all the variables and declarations needed for it. Notice how you don't see much definition to the class functions. That's because we will detail them in the cpp file.
So now you might be asking, well that's good and all but whats up with the Selector:: stuff?
Good question. In programming you have what's called a namespace and in lehman's terms its how far the function can be seen. Meaning you can't call the function outside the selector class if you don't use the Selector:: namespace first. Should you go ahead and call it, you will get compiler errors. Not a good thing. So its always best to define the namespace in front of the function you are creating. That way all you need to do is include the header file of the class and you can use the function anywhere its included.
Now I'm going to talk about the different types of functions inside the classes. For example, you will and should know what void, int, bool, etc are when defining the type of function you want. So here are some examples of each one (not all because they can be infinite if too technical):
Void - This function simply runs from beginning to end and does not return a value. No end result. All results are done in function and that's all there is to it. The program will keep running after the function is complete.
Int, Bool, Short, etc - These type will return a variable in some form. Be sure its the type of variable you want otherwise you will run into conversion problems. Let's say you have an integer variable declared. You are making a function that will populate that variable. It is common sense to make the function return an integer variable and not a boolean variable.
Object variable functions - These will probably be discussed later in the lessons but for now know that they return an entire object complete with all its own variables and methods and functions. Usually passed into other functions altogether but they function just like variable return functions.
I hope this has given a little more knowledge on how things work inside c++ and every progam solution here on out.
Feel free to post comments, concerns and feel free to contact me with the same. Serious inquiries only and make them short and to the point otherwise I may need to charge for my services which is unnecessary when teaching for free.
Look for part the next chapter when we start looking inside the socket structure of the server and see how it functions.
Thursday, October 15, 2009
Subscribe to:
Post Comments (Atom)
i'm looking forward to the complete guideline =)
ReplyDelete