| A VERY BASIC C/C++ TUTORIAL |
| If
you want to become an accomplished C/C++ programmer then this is not
the place you should be. This tutorial is an accompanying tutorial to
my QT4 tutorial and it just covers a smattering of C/C++, just enough
to get by on when combined with the QT API. Remember it is supposed to be fun We are not entering the programmer of the year competition here, we just want to write a few applications. To this end we are not interested in what is good programming practice, we are mainly interested in writing applications that are easy to read and debug/extend. To this end feel free to declare all your variables you are going to use in a function at the start of the function and never run into scope problems (see below). It may be good programming practice to create variables just as they are needed and letting them be destroyed as soon as they are out of scope but if it makes the code harder for YOU to understand then don't do it. I moved to C++ from Object Pascal and in Object Pascal you declare all a functions variables at the start of the function and they only go out of scope when the function exits. This may be one of the reasons that Pascal is considered to be one of the easiest languages to learn. Pascal is very readable. You can always change the way you write C++ as you become more proficient but do not allow the complexity of C++ put you off from using it. Remarks A badly written well remarked application is easier to maintain than a well written badly remarked application. C++ has two types of remarks, single line remarks and multi line remarks. Single line remarks start with // and end at the end of the line. Multi line remarks start with /* and end with */ myNote.append("Block B disabled."); //Write some text to the display windowThe
remark after the above code would be considered a bad remark, it
doesn't tell you anything more than what a glance at the code tells
you. A good remark would generally not be telling you what is being
done but rather telling you why it is being done. Like this....myNote.append("Block B disabled."); //While they have the options openN disable block B and let the user knowThis is a rather trivial example but I am sure you get the point. I promise you that 3 months after writing an application that has little or no remarks that you will find it almost impossible to maintain/improve/extend the application. Dots and pointers What is the difference between myDisplay.append("hello world");and myDisplay->append("hello world");This can be the cause of much confusion especially when you see this chkBuffer->buffer().fill('\0');Here as you can see we have a -> in the first part and a . in the second part. Again the rules are pretty simple, if the item you are referring to is a pointer then you use -> and if it is not a pointer then use the dot . In the above chkBuffer is a pointer to a buffer but the fill() method acts on the buffers internal QByteArray and that is not a pointer. So if I create a QString like this QString * s = "hello";and I want to use the QString method left() to get the n leftmost characters of the string I would write s->left(4);However if I create a QString like this QString s = "hello";I would then write s.left(4);Also note that QString* s; and QString * s; and QString *s; are all just different ways of writing the same thing.Loops There are 'do/while' loops and 'for' loops and just plain 'while' loops. In all the examples of loops below we just iterate from zero to nine. int x = 0;If there is only a single line of code in the loop then
the curly braces are not required, but you may still want to include
them for clarity, its up to you.Also a number of programmers don't like to waste a complete line just for the curly braces and tend to write code like this... int x = 0;Personally I find this form harder to debug but it is all down to personal preference, do whatever you feel happy with.Assignment/Evaluation Another major reason for programs to not behave the way you expect is by confusing assignment and evaluation. If you want to compare x with a value it is written like this if(x == 22)if you make the mistake of doing this if(x = 22)This will always evaluate as true because what you have actually done is assigned 22 to x. So remember to assign a value with = and evaluate with == You also have all the other evaluation symbols available != means 'not equal to' >= means 'greater than or equal to' <= means 'smaller than or equal to' Assignment also has a lot more available than just the equals sign, if you have a variable called x that has the current value of 10 and you want to assign 11 to it then you can just say x++ or to reduce it to 9 x--. Also if you have a string s = "Hello World!" and another string t = "I Say " then you can assign t += s. This will make t equal "I Say Hello World!". Using plus and equals like this is the equivalent of saying t = t + s. If you want to compare two things at one time then the parenthesis is important, for example in C/C++ to test if A==10 AND B==12 you would write it like this if((A == 10) && (B == 12)). && equates to AND and || equates to OR. Note, the | character is a shifted backslash on a UK keyboard. Scope Scope is important to understand and to be honest I still get caught out by scope now and again. Take a look at the following piece of code. for(int x = 0; x < 100; x++)This will not compile ! Why? Well the integer q was thrown
away as soon as the 'if' statement finished, so when we tried to create
an integer called 'w' and assign it with q multiplied by 20 the integer
q doesn't exist so it's an impossible piece of code. Also note that as
soon as the 'for' loop finishes that w also is thrown away and doesn't
exist any longer.Finally To be honest you are not likely to ever use much more than a few 'for' and 'while' loops and a bunch of 'if' statements when writing applications using QT. Take a look at the following function I wrote to encode and decode a string using a supplied key. QString dencrypt(QString dataIn, QString key)This is a reasonably complex piece of code and even then it just has a 'for' loop, three ints and two chars, most of what goes on is covered by QT leaving me with very little in the way of C++ code to write. The bottom line is this.... Do NOT keep looking for complex ways to achieve a task, look in the QT documentation and find your answers there, nine times out of ten QT can achieve what you want with a simple function call. |
| Copyright 2007 Clive Cooper |