160 likes | 182 Views
C++ Language Tutorial. Lesson 1 –Writing your first program. Installing an integrated development environment (IDE).
E N D
C++ Language Tutorial Lesson 1 –Writing your first program
Installing an integrated development environment (IDE) Before we can start writing a program we need an integrated development environment which is a software application that provides essential tools for computer programs assist in software development. Essentially it is the software you will use to write and compile your code. For these tutorials we will be using a program called CodeBlocks for such reasons as: • Its free • Cross platform support for windows, linux and mac • Uses a simple design ideal for beginners
Installing an integrated development environment (IDE) To install CodeBlocks follow the steps below: • Download CodeBlocks from www.codeblocks.org/downloads • Open the executional file downloaded to start the installation • Follow the onscreen instructions to install CodeBlocks • Open code blocks
Installing an integrated development environment (IDE) If you have installed CodeBlocks correctly, when open you should see the same as the screenshot below:
Writing Your First Program Before we start any coding we need to setup a new project as follows: • Select File -> New -> Project. (from the top left corner) • Now select “Empty Project” and click “go” on the right • Select a title such as “Hello world Program” and a destination and click next, then click finish on the second screen. • Select File -> New -> Empty File. (from the top left corner) • Choose a name such as “program.cpp” ensuring the extension .cpp is at the end of the file name.
Writing Your First Program - #include Now that we have our project set up we can start. The first piece of code you need to write is “#include <iostream>”. This allows us to link our file with the input/output stream. Meaning we don’t need to rewrite everything our self. #include allows you to include header files so you can add functionality to your program that has already been written by others. <iostream> is the file your are including. So far your program should look like this:
Writing Your First Program – ending a statement Semi Colons ; are used in c++ to tell the compiler when a statement has ended. These are essential as missing one will cause your program to not build until you correct the error. All statements in c++ end with a semi colon.
Writing Your First Program – int main() So now we have allowed our self to be able to input and output date however we first need a main function in which to write our code (we will cover functions later) for now all you need to know is that int main() will house the main functionality of your program. Written as Int main() { } All code needs to be placed within the squiggly brackets. Think of them as BEGIN and END the brackets define a block of code. I’m sure much of this may spark a lot of questions these will all be answered in later lessons.
Writing Your First Program – outputting text. So far you should have:
Writing Your First Program – outputting text. So now we want to output some text so we write this as “std::cout<<“Hello World”<<std::endl; • std::cout is saying we want to output something. Std is a namespace which tells the compiler that cout is contained within the standard library and cout is what allows us to output. • “Hello World” is the text we wish to output. Text always needs to be encased within quotation marks. To distinguish it from a variable (covered next lesson). • Std::endl is telling the compiler to end the line and start a new one. • The arrows << is an insertion operator that inserts the data that follows it to the corresponding stream which in this case is the output stream • ; ends the statement
Writing Your First Program So far you should have:
Writing Your First Program – Returning 0 Any function that is an integer needs to return a number. Int main () is no different so at the end of our code we need to include “return 0”. Returning a value to the operating system allows to check if our program succeeded or not by default 0 will returned however we can change this value with a return statement.
Building The Program Now that we have finished writing our program we need to build and run it. • Click the yellow Cog to build (top of the page) • Now press the green play button beside the cog, to run the program You should be presented with:
Video Tutorial Below is a video of me writing the program.
One last note • One last but very important note is commenting your code. Commenting your code means to explain what is happening. You can add coments in c++ with // or for large comments more than a line using • /* comments go here */ • This will become more clear as time progresses you will see them in the downloadable files.
Well Done! • Well done you have now created your first c++ program. If you are struggling with anything download you can download the source code with comments, along with the executional file.