Our tutorials have been submitted by our own members and staff over the last four years. They cover a range of design packages and programming languages.
Tutorials are key part of learning new skills, so we not only recommend that you try some, we suggest you experiment with the framework that they give you, don't be scared to wander from the instructions.
Click the categories below to get started!
Alternatively you can pass on your useful tips and advice to others, by creating your own.
Submit a Tutorial
If you like a tutorial, please use the "digg" button below it.
Definition
The scope of a variale is the rande of which it is defined.
Example
For example, a variable declared within a function is only accessible within it. A variable declared outside of a function is accessible anywhere though. Consider the following code snippet:
// Exists throughout the program double
globalVariable;
void myFunction(void){
// Exists only in this function double variableA;
}
int main(void){
myFunction();
}
The snippet starts off by execueint the function "main". Then, it runs myFunction. myFunction declates the variable variableA. When myFunction exits, the variable previously declared, variableA, is no longer accessible and in fact, no longer even exists (it goes out of scope). The part of memory that it is at is probably now being used by something else. A variable out of scope can't even be returned by a function.
Using the Heap
A block of memory under complete control of the programmer is often known as the "heap". The scope problem can be solved by useing the "new" keyword to allocate memory off the heap. The following code snippet should help demonstrate this:
// This does not work
int* aFunction(void){
int localVariable;
return &localvariable;
}
void anotherFunction(void){
int* pointerLocalVariable;
pointerLocalVariable = aFunction();
*pointerLocalVariable = 34;
}
// This does work
int* aFunction(void){
// Allocate a memory off the heap
int* pointerLocalVariable = new int;
return pointerLocalVariable;
}
anotherFunction(void){
// aFunction returns the address of
// the block of heap memory allocated
int* differentVariable = aFunction();
// Store 34 in the address of allocated
// memory
*differentVariable = 34;
// Memory returned to heap by using "delete"
delete pointerLocalVariable;
As you can see in the above code, the "new" keyword is used to allocate some memory off the heap. To clarify things, the following shows how to allocate some memory off the heap: type* variable name = new type You may also notice the "delete" command. This returns the memory back to the heap. You use "delete" with the pointer variable used when allocating the memory.
All in all...
I could go into all the details and could expand the length of this tutorial, but it's already long enough. Also, excuses my long variable names - they are used to clarify if they are local or not, etc. Well, you all now have (or at least I hope so) a fairly good understanding of allocating memory off the heap, and what scope means and what the heap is. Happy coding =)
About Qt
Qt is a toolkit made by Trolltech that mainly is focused on graphical user interface (GUI) programming. It is not just a GUI toolkit, though, containing classes for network programming, database programming, and primitive graphics.
It is also unique in the way that it kind of adds its own "extensions" to C++, which are then processed by the meta-object compiler. These extensions generally make programming Qt easy and fast. Unfortunately, this also makes Qt not work with a lot of integrated development environments (IDEs). QDevelop, specifically designed to be used with Qt 4, works with Qt well, so that's what we'll be using.
Qt comes in two main editions: a commercial edition, which costs money, which allows it to be used in the development of proprietary, or non-open source and free, software, and an open source edition, which is free, and it is what you use if you are developing open source applications - this is the one we're interested in.
Installing Qt and Qt Basics
You can download Qt for the Windows, Mac, and X11 platforms here. If you're using Linux, or another Unix-based operating system besides Mac OS, then the X11 version is for you. In addition, if you're using Linux, you can probably more easily install Qt through repositories - most likely, the ones enabled by default. The packages you'll need will probably be under the names "qt4" and "qt4-devel," and any dependencies will automatically be handled if you're using a decent operating system (just kidding, Slackware and other "stable" operating system users ;)). You'll probably only need to install qt4-devel, and qt4 will automatically be installed, being a dependency.
If you're using Windows, Qt 4 will probably be installed in C:\Qt\4.*.* (replace each "*" with a version number, for example: C:\Qt\4.2.3). If you're using Linux and have installed Qt from a repository, it will probably be installed in /usr/lib/qt4 or /usr/lib64/qt4. If you're on Mac OS, then...well, I'm not sure...I don't know much about Macs...but whatever the installation directory is, it should probably be intuitive. ;) Anyway, navigate to wherever Qt 4 was installed. In the "bin" folder, you'll notice several programs. The ones that you should probably be most concerned about at this point are "qtdemo," "assistant," "designer," and "qmake." "qtdemo" lets you try some interesting and helpful demonstration programs, and it also comes with links to their source code and documentation. "assistant" is a very useful program, providing extensive information about Qt, and essentially being an easy-to-use right-there-when-you-need-it form of the Qt documentation - I highly recommend checking it out sometime. "designer" is a visual What-You-See-Is-What-You-Get (WYSIWYG) editor for Qt - you may not be using this directly much because QDevelop kind of intergrates with it...you'll see. :) And lastly, there is "qmake." Although you probably won't be using this directly much, I thought you should know a bit about it. It's a tool that generates a Makefile for you based off of information from a Qt project file (which has a file extension of ".pro"), simplifing matters grealtly - especially development across multiple platforms.
About QDevelop
QDevelop is a free cross-platform IDE for Qt programming with C++. It integrates well with the components of Qt and is easy to use. Once you get more advanced at programming in C++ with Qt, you might want to switch to using a plain text editor and a command prompt. This is because QDevelop likes things its way, and if you decide to make a very complicated Qt project file with a bunch of odd dependencies or something like that, QDevelop will likely either mess up you're Qt project file or crash, unfortunately (although it is radidly improving its stability). For now, and quite some time, though, QDevelop should be fine.
Installing QDevelop
Go to http://www.qdevelop.org/ and click on "Download." Downloading the correct version should be easy enough...except for a few exceptions. If you're using Linux, first check if QDevelop is in your repositories, and if it is, you should probably download it from there. If not, download the appropriate package from the QDevelop website, and if there are no packages available for your distribution, then just download the "Linux dynamic version with plugins."
Installation should be easy enough. If you're building from the source, make sure you have the necessary dependencies (such as Qt 4, which you should already have).
Using QDevelop
When you first open QDevelop, you'll be prompted for some the paths to the external tools it uses. "gdb," a debugger, and "ctags," which can further assist you while programming, are both handy, but not necessary. And if you're Qt installation was successful, you should be able to fill in most, if not all, of the other fields (except "make"). If you don't have "make" already I recommend getting it from here.
From there...feel free to experiment. :) A few things to note:
- If you click on a .ui file, or a file that is under the "Dialogs" menu, it will most likely be opened with Qt Designer. If you want to view the actual contents of the file with QDevelop, you can simply open it with File->Open.
- If you need to use any of Qt's non-GUI features, you can enable them through Project->Properties of [project name].pro.
- Most of the time, you'll probably want to create a Qt application that uses a main window and not a dialog. The default option in QDevelop is to create a Qt project with a dialog, though. When creating a new project, in the "New project creation" window, simply select the "Application with Main Window" option.
Where to Go From Here
I recommend reading through the Qt documentation, either online (http://doc.trolltech.com/4.3/ contains documentation for the current stable release of Qt) or through the Qt Assistant. There's a good Qt tutorial included within the documentation here, which I particularly suggest you go to.
"Hello World Tutorial
ok guys this is my first tutorial on C and hope it helps. Im not too good with C but this might help someone who wants to start learning C. What is C? C is a computer programming language, it is a successor of the language B (in case some people may be wondering, no there was no A :P).
Writing a C code in a text editor and then compiling that snippet of code should ouptut the commands you have written in C. This simple tutorial will teach you how to output "Hello World" on your screen. Compilers. To compile a C code you will need a compiler, here's a good on you can download for free: http://www.bloodshed.net/devcpp.html There are more compilers if you do not wish to use that one, you could use google in that case for C compilers.
Getting ready.
After you have downloaded and installed DevC++ open that compiler. in the new window go to : file > new > project OR click on the new project image on one of the toolbars. A small new window should pop up with: Basic|Introduction|Multimedia| Windows Application :: Console Application and the rest. well at the bottom you will see: C project C++ project. we will be making a code in C so you choose for C project. And application type, you choose Windows Application. In a small input box at the bottom of the window you insert the name of your project. We will be naming it: "Hello" but this is optional, you can name it whatever you want to. After clicking "OK" you will have to save the project somewhere.
My advise would be to make a folder with all your projects in it. After saving it you will get a window with a large code in it. Remove the code, all the code in that textarea can be removed. so now we have a new white page where we will be writing the code.
The Code.
first thing we do in C is include a header file. A header file contain all the info for using a build in functions you will need to make a functional code. The header file we will need to write or string is stdio.h This is the line on how to include this header file in your code
#include <stdio.h>
Now that we have our header file included we will move on to the printing of "Hello World". in all C codes you must define the function main(), this function is the first function that will be excecuted in the code. to declare function main we use this
main(){
Now we have our function main. the opening bracket "{" is used to open a function, in between the two brackets "{" and "}", you put the code you will run. Now for the exact command to print "Hello World" into the screen, we will use the function printf().
printf("Hello World");
This is really simple if yo look at it, first we call the printf function, and whatever we want to print onto the screen with printf we put inside double quotations. So if you wanted to print something else you just put something else in the double quotations:
printf("Hello Dude");
A small note, after every command in a C code, you must add a semi-colon. so now that we have added the printf function we will close the main function using the clsoing bracket "}" so now your code look like this:
#include <stdio.h> main(){ printf("Hello World"); }
To run your program click the compile & run button (its a button in the top left of your window with 4 colors). or you can just click F9. you may see a black screen for 0.1 seconds or something and then it closes automatically. this is because after printing "Hello World" the program closes itself. to stop it from doing this you add
system("PAUSE");
in the main function. So now your code looks like:
#include <stdio.h> main(){ printf("Hello World"); system("PAUSE"); }
"Hello World" may be on the same line as "Press Any Key To Continue". if you want this on a seperate line you ad a break line charachter "\n" in the printf function. like this:
printf("Hello World\n");
And that's it for your first C program (and my first C tutorial :P). hope this was helpfull, if not you can PM me or email me. have fun with C!!
What are they?
Pointer variables are variables that contain another variable's address. Every variable is stored somewherein the computer's memory. One might be stored at 0x120 while another will be at 0x190.
Important Operators
Here are some important operators having to do with the address of a variable: & = The address of * = (In a declaration) pointer to or (In an expression) the thing pointed at by
The & Operator
The & operator returns the address of a variable in the computer's memory. Here is an example:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std; int main() {
int theVariable;
cout << "Address of the Variable: " << &theVariable << endl;
system("PAUSE");
return 0;
}
The output of this program should look something like:
Address of theVariable: 0x22ff6c Press any key to continue...
The exact address outputted probably won't be the same as the example, but it should be in the same general hex format.
The * Operator
Like I said before, the * operator says in a declaration, pointer to, or in an expression, the thing pointed at by. The following code snippet demonstrates the correct use of *.
int Var;
int* pointerToVar;
pointerToVar = &Var;
//pointerToVar now points to Var
*pointerToVar = 5;
//stores 5 to the addresse of Var
The snippet above demonstrates how to create a pointer variable and then assign a value to an address. When declaring a variable a pointer variable, the syntax is: the type of the variable it points at* variable name The * may appear anywhere between the type declaration and the variable's name. So, you could do:
char* aPointerVariable;
or
char *aPointerVariable;
You can also use pointer variables and the type of a function, like:
double* myFunction(){
//...do some stuff
}
Conclusion
Well, now you should have a grasp over the basics of pointer variables. I could go over offsets and more complex stuff, but then this tutorial would probably be too long, and it's called "Pointer Variable Basics" anyway. I might make a continuation of this though...
Parts: Declaring Variables Manipulating Variables Creating Arrays Summary (And Preview) Part 1 - Declaring Variables:
Note: You should add a semi-colon after every statement in C++. Spaces in C++ are usually used for clarification. In C++, you must declare a variable's type before you assign it a value (Note: cariable names my include both numbers and letters).
If you try to give a variable a value without declaring the variable's type, when you try to compile and run the program, you will recieve an error.
The types of variables are:
int,
unsigned int,
long,
unsigned long,
float,
double,
char,
string,
and bool.
Here are the definitions of each type:
int - An integer (not a fraction) that can be positive, negative, or 0.
unsigned int - A counting number that's only non-negative.
long - An integer that is very long (in some editors it is the same as int).
unsigned long - A nonnegative integer that is long.
float - A real number that takes up less memory than double but is less accurate in calculations.
double - A real number that is accurate during calculations and that may be very large; many times larger than an int variable.
char - A single character that is kind of like a string with one character.
string - A string of characters.
bool - A logical value: true of false
Here are a few examples of how you would declare variables and their values:
Example 1: int x; x = 5; or int x = 5;
Example 2: char a; a = "q"; or char a = "q";
Also, in strings, there are a few special things you may add in them:
\n - Start a new line.
\t - Tab
\0 - Null
\\ - Backslash
Part 2 - Manipulating Variables:
Here are some symbols you may or may not be familiar with
(2 means for 2 arguments, 1 means for 1 argument): + (1) Does nothing - (1) Retuens the negative of an argument ++ (1) +1 -- (1) -1 * (2) Mutiplication / (2) Division % (2) Modulo + (2) Addition - (2) Subtraction
Assignment Types:
= Means a=b
*= Means a=a*b
%= Means a=a%b
+= Means a=a+b
-= Means a=a-b
Logical Operators:
== Tests to see if the argument on the left is equal to the argument on the right
!= Tests to see if the argument on the left ISN'T equal to the argument on the right
> Greator than
< Less than
>= Greator than or equal to
>= Less than or equal to
&& Tests if the arguments on both sides are true
|| Tests if at least one of the arguments on one of its sides are true
! Tests if its argument is false
Example:
int myVariable1 = 10; int myVariable2 = 11; bool x; b = (myVariable2 != myVariable2);
The example above would make b be true. Part 3 - Creating arrays Arrays must have their type be declared too. Here is an example array: char myArray[] = {'H','I'}; To access a specific element in the array (let's use "H" for the example), you would do:
myArray[0] If you want the same element to be repeated in the whole array, you could do something like: float myArray[10] = {2.0};
That would make all 10 sections of myArray have the number 2.0. Part 4 - Summary: That should cover the BASICS of variables. I'll probably make another tutorial that has more advnaced info on variables. Preview to Stuff in the Next Tutorial:
To display strings, you would do:
cout << "string"; So to display a variable and a string, you could do: string yourName = "Bob"; cout << "Your name is" << yourName; To let the user of the program input sometthing, you do: cin >> myVariable;