Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.

Similar presentations


Presentation on theme: "C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM."— Presentation transcript:

1 C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM

2 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

3 To install CodeBlocks follow the steps below: 1.Download CodeBlocks from www.codeblocks.org/downloadswww.codeblocks.org/downloads 2.Open the executional file downloaded to start the installation 3.Follow the onscreen instructions to install CodeBlocks 4.Open code blocks

4 If you have installed CodeBlocks correctly, when open you should see the same as the screenshot below:

5 Before we start any coding we need to setup a new project as follows: 1.Select File -> New -> Project. (from the top left corner) 2.Now select “Empty Project” and click “go” on the right 3.Select a title such as “Hello world Program” and a destination and click next, then click finish on the second screen. 4.Select File -> New -> Empty File. (from the top left corner) 5.Choose a name such as “program.cpp” ensuring the extension.cpp is at the end of the file name.

6 Now that we have our project set up we can start. The first piece of code you need to write is “#include ”. 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. is the file your are including. So far your program should look like this:

7 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.

8 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.

9 So far you should have:

10 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

11 So far you should have:

12 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.

13 Now that we have finished writing our program we need to build and run it. 1.Click the yellow Cog to build (top of the page) 2.Now press the green play button beside the cog, to run the program You should be presented with:

14 Below is a video of me writing the program.

15 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.

16 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.


Download ppt "C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM."

Similar presentations


Ads by Google