C-and-C++/C2/First-C++-Program/English-timed

From Script | Spoken-Tutorial
Revision as of 11:44, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time' Narration
00.01 Welcome to the spoken tutorial on First C++ program.
00.06 In this tutorial I am going to explain,
00.10 How to write a C++ program
00.12 How to compile it
00.14 How to execute it
00.16 We will also explain some common errors and their solution.
00.21 To record this tutorial, I am using Ubuntu operating system version 11.10 and G++ Compiler version 4.5.2 on Ubuntu.
00.34 To practice this tutorial,
00.36 You should be familiar with Ubuntu Operating System and an Editor
00.42 Some editors are vim and gedit
00.46 I am using gedit in this tutorial
00.49 For relevant tutorial Please visit our website: http://spoken-tutorial.org
00.54 Let me tell you how to write a C++ program through an example
00.59 Open the terminal Window using Ctrl, Alt and T keys simultaneously on your keyboard.
01.08 To open the text editor, type under terminal.
01.12 “gedit” space “talk” dot “.cpp” space ampersand “&”.
01.20 We use the “&” to free up the prompt.
01.24 Please note that all the C++ files will have the extension “.cpp”
01.30 Now Press Enter
01.32 the text editor has opened.
01.35 Let us start to write a program.
01.38 Type double slash “//” space
01.41 “My first C++ program”.
01.44 Here, double slash is used to comment the line
01.49 Comments are used to understand the flow of program
01.53 It is useful for documentation
01.55 It gives us information about the program
01.58 The double slash is called as single line comment.Now press Enter.
02.04 Type hash “#include” space opening angle bracket closing angle bracket .


02.13 It is a good practice to complete the brackets first, and then start writing inside it
02.20 Now Inside the bracket, type “iostream” .
02.24 Here iostream is a header file
02.28 This file includes the declaration of standard input output functions in C++.Now press Enter
02.36 Type “using” space “namespace” space “std” and a semicolon “;” .
02.48 The using statement informs the compiler that you want to use the std namespace
02.54 The purpose of namespace is to avoid name collisions
02.58 It is done by localizing the names of identifiers
03.02 It creates a declarative region and defines a scope
03.05 Anything defined within a namespace is in the SCOPE of that namespace
03.11 Here std is the namespace in which entire standard C++ library is declared. Now press Enter.


03.20 Type “int” space “main” opening bracket “(” closing bracket “)” .


03.28 main is a special function
03.31 It denotes that the execution of the program begins from this line.
03.36 The opening and the closing bracket is called as Parenthesis.
03.41 Parenthesis followed by main tells the user that main is a function.
03.48 Here the int main function takes no arguments and returns a value of type integer.
03.55 We will learn about data types in another tutorial.
04.00 Now Let us switch to the slides to know more about main function.
04.06 Every programshould have one main function
04.09 There should NOT be more than one “main” function
04.13 Otherwise the compiler cannot locate the beginning of the program
04.17 The empty pair of parentheses indicates that main has no arguments
04.23 The concept of arguments will be discussed in the upcoming tutorials. Now come back to our program. press enter.
04.34 Type opening curly bracket “{”
04.37 The opening curly bracket marks the beginning of the function main.
04.42 Then Type closing curly bracket “}”
04.45 The closing bracket indicates the end of the function main
04.50 Now Inside the bracket press enter twice
04.54 Move the cursor one line up.
04.57 Indentation makes the code easier to read
05.00 It also helps to locate errors faster
05.03 So let us give a space here.
05.06 And type “cout” space two opening angle bracket '
05.13 Here cout is a standard C++ function to print the output on the terminal.


05.20 Now after the brackets, type within double quotes
05.26 Anything within the double quotes in the cout functions will be printed. Now inside a quote type“Talk to a teacher backslash \n”.
05.39 Here \n signifies newline
05.43 As a result, after execution of the cout function, the cursor moves to the new line.
05.50 Every C++ statement must end with a semicolon


05.54 Hence type it at the end of this line.
05.57 Semicolon acts as a statement terminator. Now press Enter.
06.03 Give a space here and Type “return” space “0” and a semicolon “;”.
06.10 This statement returns the integer zero


06.14 An integer has to be returned for this function


06.17 Because the function type is int


06.21 The return statement marks the end of executable statements


06.26 We will learn more about the returned values in another tutorial.
06.31 Now click on “Save” button to save the file


06.36 It is a good habit to save files frequently


06.39 This will protect you from sudden power failures


06.42 It will also be useful in case the applications were to crash.
06.47 Let us now compile the program.
06.50 Come back to a terminal


06.53 Type “g++” space “talk.cpp” space hyphen “-o” space “output”.
07.05 Here g++ is the compiler used to compile C++ programs


07.12 talk.cpp is our filename
07.16 -o output says that the executable should go to the file output. Now press enter
07.25 We see that the program is compiled.
07.27 By typing ls -lrt, we can see that output is the last file to be created.
07.37 Let us execute a program, type dot slash “./output”


07.43 And Press Enter.
07.46 Here the output is displayed as “Talk to a teacher”.
07.50 Now let us see the common errors which we can come across


07.54 switch back to a editor.
07.57 Suppose here we miss the {.
08.01 Now save the file.
08.04 Let us execute.Come back to a terminal
08.08 Now compile and run the program using the command we used before.
08.15 we see that there is an error at line no.7 in our talk.cpp file


08.22 That Expected curly bracket at the end of input.
08.28 Now Come back to a text editor .
08.31 As i said before the closing curly bracket marks the end of the function main


08.36 Hence reinsert the bracket here now Save the file.


08.41 Let us execute it


08.44 You can recall the previously entered commands by using up arrow key


08.49 That is what I did now. Yes it is working.
08.56 I will show you another common error


08.59 Let us switch back to our text editor.
09.02 Now, suppose here we missed std.Let us save the file
09.08 Come back to our terminal . Let us compile .


09.13 We see that there is an errors at line no 3 and line no 6 in our talk.cpp file


09.21 That expected identifier before semicolon and cout was not declared in this scope.
09.30 As cout is the standard C++ library function


09.33 and the entire C++ library function is defined under std namespace


09.39 Hence it is giving an error.
09.42 Let us now fix the error


09.44 Come back to our Text editor type std at line no-3.
09.50 Let us Save it.
09.52 Let us compile it again.Yes it is working. Now let us go back to our slide.


10.02 As an assignment,


10.04 Write a program to print your name and city


10.07 We used single line comment in this tutorial


10.10 Now just try to give a multiline comment


10.14 This brings us to the end of these tutorial .
10.17 Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial


10.20 It summarises the Spoken Tutorial project


10.22 If you do not have good bandwidth, you can download and watch it.
10.26 The Spoken Tutorial Project Team


10.28 Conducts workshops using spoken tutorials


10.31 Gives certificates to those who pass an online test


10.35 For more details, please write to contact @spoken-tutorial.org
10.43 Spoken Tutorial Project is a part of the Talk to a Teacher project
10.47 It is supported by the National Mission on Education through ICT, MHRD, Government of India


10.53 More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
10.58 This is Ashwini Patil from IIT Bombay signing off


11.01 Thank you for watching

Contributors and Content Editors

Ashwini, Devraj, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha