Difference between revisions of "C-and-C++/C2/First-C++-Program/Gujarati"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
{| border = 1
+
{| border=1
 
+
|| ''Time'''
|'''Time'''
+
|| '''Narration'''
 
+
|'''Narration'''
+
 
+
 
+
 
|-
 
|-
 
| 00.02
 
| 00.02
| |First C++ પ્રોગ્રામ પરનાં સ્પોકન ટ્યુટોરીયલમાં સ્વાગત છે.
+
| Welcome to the spoken tutorial on First C++ program.  
  
 
|-
 
|-
| 00.06
+
| 00.07
આ ટ્યુટોરીયલમાં આપણે શીખીશું,  
+
In this tutorial I am going to explain,  
  
 
|-
 
|-
| 00.08
+
| 00.10
|'''C++''' પ્રોગ્રામ કેવી રીતે લખવું
+
| How to write a C++ program
 
+
|-
+
| 00.11
+
| તેને કમ્પાઈલ કેવી રીતે કરવું
+
  
 
|-
 
|-
 
| 00.13
 
| 00.13
| તેને એક્ઝીક્યુટ કેવી રીતે કરવું
+
| How to compile it
  
|-
+
|-  
 
| 00.14
 
| 00.14
|એ સાથે જ અમે સમજાવીશું કેટલાક સામાન્ય એરરો અને તેમનાં ઉકેલો.
+
| How to execute it
  
 
|-
 
|-
|00.19
+
| 00.17
| | આ ટ્યુટોરીયલને રેકોર્ડ કરવા માટે હું  વાપરી રહ્યી છું.  
+
| We will also explain some common errors and their solution.  
  
 
|-
 
|-
| 00.21
+
| 00.22
|ઉબુન્ટુ ઓપરેટીંગ સીસ્ટમ આવૃત્તિ ૧૧.૧૦ અને ઉબુન્ટુ પર '''G++''' કમ્પાઈલર આવૃત્તિ ૪..
+
| To record this tutorial, I am using Ubuntu operating system version 11.10 and G++ Compiler version 4.5.2 on Ubuntu.
  
 
|-
 
|-
| 00.31
+
| 00.35
| આ ટ્યુટોરીયલનાં અભ્યાસ માટે,  
+
| To practice this tutorial,  
  
 
|-
 
|-
| 00.33
+
| 00.38
|તમને ઉબુન્ટુ ઓપરેટીંગ સીસ્ટમ અને એડીટરની જાણ હોવી જરૂરી છે. 
+
| You should be familiar with Ubuntu Operating System and an Editor
  
 
|-
 
|-
| 00.39
+
| 00.44
|''vim''' અને '''gedit''' એ અમુક એડીટરો છે
+
|Some editors are vim and gedit
  
 
|-
 
|-
| 00.42
+
| 00.48
|I will use gedit in this tutorial
+
| I am using gedit in this tutorial
  
 
|-
 
|-
| 00.46
+
| 00.51
|For relevant tutorials please visit our website: [http://spoken-tutorial.org/ http://spoken-tutorial.org]  
+
| For relevant tutorial Please visit our website: [http://spoken-tutorial.org/ http://spoken-tutorial.org]  
  
 
|-
 
|-
|00.51
+
| 00.56
| Let me tell you how to write a C program through an example  
+
| Let me tell you how to write a C++ program through an example  
  
 
|-
 
|-
| 00.56
+
| 01.01
|Open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard.
+
| Open the  terminal Window using '''Ctrl, Alt and T keys''' simultaneously on your keyboard.
  
 
|-
 
|-
| 01.07
+
| 01.09
| Now let's open the text editor. So, at the prompt, type  
+
| To open the text editor, type under terminal.
  
 
|-
 
|-
| 01.12
+
| 01.13
|'''“gedit”''' space '''“talk”''' dot '''“c”''' space “&”'''
+
| '''“gedit”''' space '''“talk”''' dot '''“.cpp”''' space ampersand '''“&”.'''
  
 
|-
 
|-
| 01.20
+
| 01.21
|'''We use ampersand (&) to free up the prompt '''
+
| We use the '''&”''' to free up the prompt.
  
 
|-
 
|-
 
| 01.25
 
| 01.25
| Please note that all the '''C''' files will have extension dot '''“c”'''
+
| Please note that all the C++ files will have the extension '''“.cpp”'''
  
 
|-
 
|-
|01.31
+
| 01.31
| Now Press '''Enter'''
+
| Now   Press '''Enter'''
  
 
|-
 
|-
 
| 01.33
 
| 01.33
| the text editor has opened
+
|the text editor has opened.
  
 
|-
 
|-
| 01.37
+
| 01.36
|  Let us start to write a program  
+
|  Let us start to write a program.
  
 
|-
 
|-
| 01.39
+
| 01.38
 
| Type double slash '''“//”''' space  
 
| Type double slash '''“//”''' space  
  
 
|-
 
|-
| 01.42
+
| 01.41
|'''“My first C program”.'''
+
| '''“My first C++ program”.'''
  
 
|-
 
|-
|01.48
+
| 01.44
| Here, double slash is used to comment the line
+
| Here, double slash is used to comment the line
  
 
|-
 
|-
| 01.52
+
| 01.49
|Comments are used to understand the flow of program
+
| Comments are used to understand the flow of program
  
 
|-
 
|-
| 01.56
+
| 01.52
|It is useful for documentation
+
| It is useful for documentation
  
 
|-
 
|-
| 01.58
+
| 01.55
|It gives us information about the program
+
| It gives us information about the program
  
 
|-
 
|-
| 02.01
+
| 01.59
|The double slash is called as single line comment.
+
| The double slash is called as single line comment.Now press Enter.
  
 
|-
 
|-
| 02.07
+
| 02.05
| Now press '''Enter'''
+
| Type hash '''“#include”''' space opening angle bracket ''' ''closing angle bracket ''' .
 +
 
  
 
|-
 
|-
|02.09
+
| 02.13
|Type hash '''“#include”''' space opening bracket , closing bracket'''
+
|It is  a good practice to complete the brackets first, and then start writing inside it
  
 
|-
 
|-
| 02.17
+
| 02.20
| It is always a good practice to complete the brackets first, and then start writing inside it
+
| Now Inside the bracket, type '''“iostream”''' .
  
 
|-
 
|-
| 02.24
+
| 02.23
| Now  Inside the bracket, type'''“stdio”''' dot”.” “'''h” '''
+
| Here '''iostream''' is a '''header file'''  
  
 
|-
 
|-
| 02.30
+
| 02.26
'''stdio.h''' is a '''header file'''
+
| This file includes the declaration of standard input output functions  in C++.Now press Enter
  
 
|-
 
|-
| 02.33
+
| 02.35
|A '''program''' must contain this header file when it uses standard '''input/output functions'''.Now press Enter
+
| Type '''“using”''' space '''“namespace”''' space '''“std”''' and a semicolon '''“;”''' .
  
 
|-
 
|-
| 02.43
+
|02.45
|   type '''“int” '''space''' “main” '''opening bracket,
+
| The '''using''' statement informs the compiler that you want to use the '''std namespace'''
closing bracket '''“()”. '''
+
  
|-
 
| 02.50
 
| | ' '''main''' is a special '''function'''
 
 
|-
 
|-
 
| 02.52
 
| 02.52
|It denotes that the execution of the program begins from this line
+
| The purpose of '''namespace''' is to avoid name collisions
  
 
|-
 
|-
| 02.58
+
| 02.56
|The opening bracket and  closing bracket is called as parenthesis.
+
| It is done by localizing the names of identifiers
  
 
|-
 
|-
| 03.04
+
|03.01
| Parenthesis followed by '''main''' is to tell the user that '''main''' is a '''function'''
+
| It creates a declarative region and defines a scope
  
 
|-
 
|-
| 03.11
+
| 03.05
| Here the '''int''' '''main function''' takes no '''arguments'''  
+
| Anything defined within a '''namespace''' is in the SCOPE of that namespace
  
 
|-
 
|-
| 03.15
+
| 03.11
|It returns a value of type '''integer'''.
+
|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.19
 
| We will learn about '''data types''' in another tutorial.
 
  
 
|-
 
|-
| 03.23
+
| 03.27
| Now  Let us switch to the slides to know more about '''main function'''. Let us go to the next slide.
+
|  '''main''' is a special '''function'''
  
 
|-
 
|-
 
| 03.30
 
| 03.30
| Every '''program'''should have  one main function
+
| It denotes that the execution of the program begins from this line.
  
 
|-
 
|-
| 03.33
+
| 03.35
|There should NOT be more than one main function
+
|The opening and the closing bracket is called as'' Parenthesis''.
 
+
 
|-
 
|-
| 03.37
+
| 03.39
|Otherwise the compiler cannot locate the beginning of the program
+
| Parenthesis followed by '''main'''  tells the user that '''main''' is a '''function.'''
  
 
|-
 
|-
| 03.41
+
| 03.45
|The empty pair of parentheses indicates that main has no '''arguments'''
+
| Here the '''int''' '''main''' '''function''' takes no '''arguments''' and returns a value of type integer.
 
+
|-
+
| 03.46
+
|The concept of arguments will be discussed in detail in the upcoming tutorials.
+
  
 
|-
 
|-
 
| 03.52
 
| 03.52
| Now let us come back to our program. Press Enter.
+
| We will learn about data types in another tutorial.
  
 
|-
 
|-
| 03.58
+
| 03.56
| Type opening curly brace '''“{”'''
+
|Now  Let us switch to the slides to know more about  main function.
  
 
|-
 
|-
| 04.00
+
| 04.02
|The opening curly bracket marks the beginning of the '''function''' '''main'''.
+
| Every '''program'''should have  one main function
  
 
|-
 
|-
 
| 04.05
 
| 04.05
|Then  Type  closing curly  bracket '''“}”'''
+
| There should NOT be more than one “main” function
+
 
 
|-
 
|-
| 04.08
+
| 04.09
|The closing curly bracket indicates the end of the '''function''' '''main'''.
+
| Otherwise the compiler cannot locate the beginning of the program
  
 
|-
 
|-
 
| 04.13
 
| 04.13
| Now Inside the bracket 
+
| The empty pair of parentheses indicates that main has no '''arguments'''
  
 
|-
 
|-
| 04.14
+
| 04.19
|press '''Enter''' twice, move the cursor one line up
+
| The concept of '''arguments''' will be discussed in the upcoming tutorials. Now come back to our program. press enter.
  
 
|-
 
|-
| 04.20
+
| 04.29
| Indentation makes the code easier to read
+
| Type opening curly bracket '''“{”'''
  
 
|-
 
|-
| 04.23
+
| 04.32
|It also helps to locate errors faster
+
| The opening  curly  bracket marks the beginning of the '''function main. '''
  
 
|-
 
|-
| 04.26
+
| 04.37
|So let us give three space here
+
|Then Type closing curly bracket '''“}”'''
  
 
|-
 
|-
| 04.29
+
| 04.40
| And  Type''' “printf” ''' opening bracket closing bracket '''“()” '''
+
| The closing bracket indicates the end of the '''function main '''
  
 
|-
 
|-
| 04.34
+
| 04.45
|'''printf '''is a standard C function to print the output on the terminal
+
|Now  Inside the bracket press enter twice
  
 
|-
 
|-
| 04.39
+
| 04.49
| Here inside the brackets, within double quotes,
+
| Move the cursor one line up.
  
 
|-
 
|-
| 04.44
+
|04.51
|Anything within the double quotes in the printf statement will be printed on the terminal.
+
| Indentation makes the code easier to read
  
 
|-
 
|-
| 04.50
+
| 04.55
|Type'''“Talk To a Teacher '''backslash '''n”'''
+
| It also helps to locate errors faster
  
 
|-
 
|-
| 05.00
+
| 04.58
|Backslash n '''“\n” '''signifies newline
+
|So let us  give a  space here.
  
 
|-
 
|-
| 05.03
+
| 05.01
|As a result, after execution of the '''printf '''function, the cursor moves to the new line
+
|And type '''“cout”''' space two opening angle bracket ''''
  
 
|-
 
|-
| 05.11
+
| 05.08
| Every '''C''' statement must end with a '''semicolon “;”'''
+
| Here '''cout '''is a standard '''C++ function''' to print the output on the terminal.
 +
 
  
|-
 
| 05.15
 
|Hence, type it at the end of this line.
 
  
 
|-
 
|-
| 05.19
+
| 05.14
|'''Semicolon''' acts as a statement terminator.
+
|Now  after the brackets, type within  double quotes '''
  
 
|-
 
|-
|05.24
+
| 05.18
|Now press '''Enter''' give three space here
+
|Anything within the double quotes in the '''cout ''' functions will be printed. Now inside a quote type“Talk to a teacher backslash \n”'''
  
 
|-
 
|-
|05.28
+
| 05.31
| And type '''“return”''' space''' “0”''' and a '''semicolon''' '''“;”'''
+
| Here '''\n''' signifies newline
  
 
|-
 
|-
| 05.34
+
| 05.35
| This statement returns the integer zero
+
| As a result, after execution of the '''cout function''', the cursor moves to the new line.
  
 
|-
 
|-
| 05.38
+
| 05.41
|An integer has to be returned for this function because the function type is '''int'''
+
| Every C++ statement must end with a '''semicolon'''  
 +
 
  
 
|-
 
|-
 
| 05.45
 
| 05.45
|The '''return''' statement marks the end of executable statements
+
| Hence type it at the end of this line.
  
 
|-
 
|-
| 05.51
+
| 05.48
|We will learn more about the returned values in another tutorial.
+
| Semicolon acts as a statement terminator. Now press Enter.
  
 
|-
 
|-
| 05.56
+
| 05.53
| Now click on"'''Save'''" button to save the file
+
| Give a space here and  Type '''“return”''' space '''“0”''' and a semicolon '''“;”.'''
  
 
|-
 
|-
 
| 06.00
 
| 06.00
|It is a good habit to save files frequently
+
| This statement returns the integer zero
 +
 
  
 
|-
 
|-
 
| 06.03
 
| 06.03
|This will protect you from sudden power failures
+
| An integer has to be returned for this '''function'''
 +
 
  
 
|-
 
|-
 
| 06.06
 
| 06.06
|It will also be useful in case the applications were to crash.
+
| Because the '''function''' type is '''int'''
 +
 
  
 
|-
 
|-
 
| 06.10
 
| 06.10
| Let  us now compile the program come back to a  terminal.
+
| The '''return''' statement marks the end of executable statements
 +
 
  
 
|-
 
|-
 
| 06.15
 
| 06.15
|Type '''“gcc”''' space '''“talk.c”''' space hyphen “-'''o”''' space '''“myoutput”'''
+
| We will learn more about the returned values in another tutorial.
  
 
|-
 
|-
| 06.24
+
| 06.20
| '''gcc''' is the compiler
+
| Now click on '''“Save”''' button to save the file
 +
 
  
 
|-
 
|-
| 06.27
+
| 06.23
|'''talk.c''' is our filename .
+
| It is a good habit to save files frequently
 +
 
 +
 
 +
|-
 +
| 06.26
 +
| This will protect you from sudden power failures
 +
 
 +
 
 
|-
 
|-
 
| 06.30
 
| 06.30
|'''-o''' '''myoutput''' says that the executable should go to the file '''myoutput'''
+
| It will also be useful in case the applications were to crash.
 +
 
 +
|-
 +
| 06.34
 +
| Let us now compile the program.
  
 
|-
 
|-
 
| 06.37
 
| 06.37
Now Press''' Enter. '''
+
| Come back to a terminal
 +
 
  
 
|-
 
|-
 
| 06.39
 
| 06.39
|We see that the program is compiled
+
| Type '''“g++”''' space '''“talk.cpp”''' space hyphen '''“-o”''' space '''“output”.'''
  
 
|-
 
|-
| 06.42
+
| 06.49
|By typing '''ls -lrt''', we can see that '''myoutput''' is the last file to be created
+
|Here '''g++''' is the compiler used to compile '''C++ '''programs
 +
 
  
 
|-
 
|-
| 06.54
+
| 06.55
|To execute the program, type dot slash '''“./myoutput” ''' press '''Enter.'''
+
| '''talk.cpp''' is our  filename
  
 
|-
 
|-
|07.01
+
| 06.59
| Here the output is displayed as '''“Talk To a Teacher”.'''
+
| '''-o output''' says that the executable should go to the file output. Now press enter
  
 
|-
 
|-
| 07.06
+
| 07.07
As I said before, return is the last statement to be executed
+
We see that the program is compiled.
  
 
|-
 
|-
 
| 07.10
 
| 07.10
|Thus after the return statement nothing will be executed. Let us try it out.
+
| By typing '''ls -lrt''', we can see that '''output''' is the last file to be created.
  
 
|-
 
|-
|07.15
+
| 07.19
| come back to our program.
+
| Let us  execute a  program,  type dot slash '''“./output” '''
 +
 
  
 
|-
 
|-
| 07.17
+
| 07.24
| After the '''return''' statement, let us include one more '''printf''' statement give space here type printf("Welcome \n") at the end type a semicolon.
+
|And  Press '''Enter.'''
  
 
|-
 
|-
| 07.35
+
| 07.27
| Now click on  save.
+
| Here the output is displayed as '''“Talk to a teacher”.'''
  
 
|-
 
|-
| 07.37
+
| 07.31
|Let us compile and execute come back to our terminal.
+
Now let us see the common errors which we can come across
 +
 
  
 
|-
 
|-
|07.41
+
| 07.35 
you can recall the previously entered commands by using '''up arrow'''key.
+
| switch back to  a editor.
  
 
|-
 
|-
| 07.46
+
| 07.38
|That is what I did now
+
| Suppose here we miss the '''{'''.
 +
|-
 +
| 07.42
 +
|Now  save the file.
  
 
|-
 
|-
| 07.51
+
| 07.44
|We see that the second statements  '''welcome''' is  not executed
+
| Let us execute.Come back to a terminal
  
 
|-
 
|-
| 07.58
+
| 07.48
| Now come back to our program
+
| Now compile and run the program using the command we used before.We see an error
  
 
|-
 
|-
| 08.00
+
| 07.55
|Let us write the 'Welcome' statement above the return statement
+
| we see that there is an error at line no.7 in our '''talk.cpp''' file
 +
 
 +
 
 +
|-
 +
| 08.02
 +
|  That Expected curly bracket at the end of input.
  
 
|-
 
|-
 
| 08.07
 
| 08.07
Click on Save.
+
|Now Come back to our text editor .
  
 
|-
 
|-
 
| 08.09
 
| 08.09
|Let us compile and execute
+
| As i said before the closing curly bracket marks the end of the function main
 +
 
  
 
|-
 
|-
| 08.15
+
| 08.14
|We see that the second '''printf''' statement ''welcome'' has also has been executed
+
| Hence re-insert the bracket here. now Save the file.
 +
 
  
 
|-
 
|-
| 08.23
+
| 08.19
| Now let us see the common errors which we can come across. Come back to our program.
+
Let us execute it again
  
  
 
|-
 
|-
| 08.29
+
| 08.21
| Suppose here I  will the miss the dot in '''“stdio.h”''' Click on save.
+
| You can recall the previously entered commands by using up arrow key
  
  
 
|-
 
|-
| 08.36
+
| 08.26
| Let us  compile and execute .
+
| That is what I did now. Yes it is working.
  
 
|-
 
|-
| 08.41
+
|08.32
|We see that
+
| I will show you another common error
 +
 
  
 
|-
 
|-
| 08.42
+
| 08.35
|There is a fatal error  at line no.2 in our '''talk.c''' file.
+
| Let us switch back to our text editor.
  
 
|-
 
|-
| 08.48
+
| 08.38
|The compiler cannot find a '''header file''' with the name '''“stdioh”'''hence it is giving an error no such file or directory.
+
| Now, suppose here we missed '''std.'''Let us save the file  
  
 
|-
 
|-
| 08.59
+
| 08.44
|And the compilation is terminated.
+
Come back to our  terminal . Let us compile .
|-
+
 
| 09.03
+
|Let us now fix the error come back to a  program Reinsert the dot '''“.”'''click on save
+
  
  
 
|-
 
|-
| 09.11
+
| 08.48
| Let us compile and  execute.Yes  It is working.
+
| We see that there is an  errors at line no 3 and  line no 6 in our '''talk.cpp''' file
 +
 
  
 
|-
 
|-
| 09.19
+
| 08.56
| I will show you another common error
+
| That expected identifier before '''semicolon''' and '''cout''' was not declared in this scope.
  
 
|-
 
|-
|09.22
+
|09.05
|Let us switch back to the program.
+
| As '''cout''' is the standard '''C++ library function'''
  
  
 
|-
 
|-
| 09.26
+
| 09.09
| Now, suppose here i will miss the '''semicolon''' at the end of the line
+
| and the entire '''C++ library function''' is defined under '''std namespace '''
 +
 
  
 
|-
 
|-
| 09.31
+
| 09.15
| Click on  '''Save'''.Let us compile and execute
+
| Hence it is giving an error.
  
 
|-
 
|-
| 09.41
+
| 09.18
| We see that there is an error at line no.6 in our talk.c file.  That expected ''semicolon'' before  printf.
+
| Let us now fix the error
|-
+
| 09.51
+
|Come back to our program.  As  i said before semicolon acts as a statement terminator
+
  
  
 
|-
 
|-
| 09.59
+
| 09.19
|So it will search for it at the end of the line.5 and at the beginning of the line.6.
+
| Come back to our Text editor type '''std''' here
 
+
 
|-
 
|-
| 10.07
+
| 09.23
| This is line 6
+
| Let us Save it.
  
 
|-
 
|-
| 10.09
+
| 09.25
This is the last place where you can put the '''semicolon'''
+
Let us compile it again.Yes it is working. 
 
+
 
|-
 
|-
| 10.13
+
| 09.32
|Recall that the compiler also give the error message on line 6.
+
| As an assignment,
  
|-
 
| 10.18
 
| Let us try what happens if we put the semicolon here.
 
  
 
|-
 
|-
| 10.24
+
| 09.33
| Click on '''Save''' .
+
| Write a program to print your name and city
  
|-
 
| 10.26
 
|Let us  Compile and execute.Yes it is working
 
  
 
|-
 
|-
| 10.33
+
| 09.37
|Now come back to our program.  Let us type the ''semicolon'' here  at the end of this line.
+
| We used '''single line comment''' in this tutorial
  
  
 +
|-
 +
|09.40
 +
| Now just try to give a '''multiline comment'''
  
  
|-
+
   
| 10.41
+
| As it is a conventional practice to type the ''semicolon'' at the end of the line. Now click on save.
+
  
 
|-
 
|-
| 10.49
+
| 09.44
|   Let us  Compile and execute. .Yes it is working.
+
| Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
  
  
 
|-
 
|-
| 10.49
+
| 09.47
|Now let us go back to our slides.
+
| It summarises the Spoken Tutorial project
  
|-
 
|10.57
 
| As an Assignment
 
  
 
|-
 
|-
| 10.59
+
| 09.49
|Write a program to print "Welcome to the World of C"
+
| If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
| 11.03
+
|09.53
|See what happens if  '''“\n”''' is not included in the '''printf''' statement.
+
|The Spoken Tutorial Project Team
  
|-
 
| 11.09
 
| This brings us to the end of this tutorial
 
  
 
|-
 
|-
| 11.12
+
| 09.55
| Watch the video available at the link shown below, http://spokentutorial.org/What\_is\_a\_Spoken\_Tutorial
+
| Conducts workshops using spoken tutorials
  
|-
 
| 11.15
 
|It summarises the Spoken Tutorial project
 
  
 
|-
 
|-
| 11.18
+
|09.58
|If you do not have good bandwidth, you can download and watch it.
+
| Gives certificates to those who pass an online test
  
|-
 
| 11.22
 
| The Spoken Tutorial Project Team
 
  
 
|-
 
|-
| 11.24
+
| 10.01
|Conducts workshops using spoken tutorials
+
| For more details, please  write to  contact @spoken-tutorial.org
  
 
|-
 
|-
| 11.28
+
|10.10
|Gives certificates to those who pass an online test
+
| Spoken Tutorial Project is a part of the Talk to a Teacher project
  
 
|-
 
|-
| 11.32
+
| 10.14
|For more details, please write to contact [at] spoken hyphen tutorial dot org
+
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
 +
 
  
 
|-
 
|-
|11.38
+
| 10.20
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
| More information on this Mission is available at: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro
  
 
|-
 
|-
| 11.42
+
| 10.25
|It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
| This is Ashwini Patil from IIT Bombay signing off
  
|-
 
| 11.48
 
|More information on this Mission is available at  the link shown below: http://spoken-tutorial.org\NMEICT-Intro
 
  
 
|-
 
|-
| 11.51
+
| 10.28
| This is Ashwini Patil from IIT Bombay. Thank you for joining.
+
| Thank you for watching
  
 
|}
 
|}

Revision as of 14:54, 10 October 2013

Time' Narration
00.02 Welcome to the spoken tutorial on First C++ program.
00.07 In this tutorial I am going to explain,
00.10 How to write a C++ program
00.13 How to compile it
00.14 How to execute it
00.17 We will also explain some common errors and their solution.
00.22 To record this tutorial, I am using Ubuntu operating system version 11.10 and G++ Compiler version 4.5.2 on Ubuntu.
00.35 To practice this tutorial,
00.38 You should be familiar with Ubuntu Operating System and an Editor
00.44 Some editors are vim and gedit
00.48 I am using gedit in this tutorial
00.51 For relevant tutorial Please visit our website: http://spoken-tutorial.org
00.56 Let me tell you how to write a C++ program through an example
01.01 Open the terminal Window using Ctrl, Alt and T keys simultaneously on your keyboard.
01.09 To open the text editor, type under terminal.
01.13 “gedit” space “talk” dot “.cpp” space ampersand “&”.
01.21 We use the “&” to free up the prompt.
01.25 Please note that all the C++ files will have the extension “.cpp”
01.31 Now Press Enter
01.33 the text editor has opened.
01.36 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.52 It is useful for documentation
01.55 It gives us information about the program
01.59 The double slash is called as single line comment.Now press Enter.
02.05 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.23 Here iostream is a header file
02.26 This file includes the declaration of standard input output functions in C++.Now press Enter
02.35 Type “using” space “namespace” space “std” and a semicolon “;” .
02.45 The using statement informs the compiler that you want to use the std namespace
02.52 The purpose of namespace is to avoid name collisions
02.56 It is done by localizing the names of identifiers
03.01 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.27 main is a special function
03.30 It denotes that the execution of the program begins from this line.
03.35 The opening and the closing bracket is called as Parenthesis.
03.39 Parenthesis followed by main tells the user that main is a function.
03.45 Here the int main function takes no arguments and returns a value of type integer.
03.52 We will learn about data types in another tutorial.
03.56 Now Let us switch to the slides to know more about main function.
04.02 Every programshould have one main function
04.05 There should NOT be more than one “main” function
04.09 Otherwise the compiler cannot locate the beginning of the program
04.13 The empty pair of parentheses indicates that main has no arguments
04.19 The concept of arguments will be discussed in the upcoming tutorials. Now come back to our program. press enter.
04.29 Type opening curly bracket “{”
04.32 The opening curly bracket marks the beginning of the function main.
04.37 Then Type closing curly bracket “}”
04.40 The closing bracket indicates the end of the function main
04.45 Now Inside the bracket press enter twice
04.49 Move the cursor one line up.
04.51 Indentation makes the code easier to read
04.55 It also helps to locate errors faster
04.58 So let us give a space here.
05.01 And type “cout” space two opening angle bracket '
05.08 Here cout is a standard C++ function to print the output on the terminal.


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


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


06.03 An integer has to be returned for this function


06.06 Because the function type is int


06.10 The return statement marks the end of executable statements


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


06.23 It is a good habit to save files frequently


06.26 This will protect you from sudden power failures


06.30 It will also be useful in case the applications were to crash.
06.34 Let us now compile the program.
06.37 Come back to a terminal


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


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


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


07.35 switch back to a editor.
07.38 Suppose here we miss the {.
07.42 Now save the file.
07.44 Let us execute.Come back to a terminal
07.48 Now compile and run the program using the command we used before.We see an error
07.55 we see that there is an error at line no.7 in our talk.cpp file


08.02 That Expected curly bracket at the end of input.
08.07 Now Come back to our text editor .
08.09 As i said before the closing curly bracket marks the end of the function main


08.14 Hence re-insert the bracket here. now Save the file.


08.19 Let us execute it again


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


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


08.35 Let us switch back to our text editor.
08.38 Now, suppose here we missed std.Let us save the file
08.44 Come back to our terminal . Let us compile .


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


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


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


09.15 Hence it is giving an error.
09.18 Let us now fix the error


09.19 Come back to our Text editor type std here
09.23 Let us Save it.
09.25 Let us compile it again.Yes it is working.
09.32 As an assignment,


09.33 Write a program to print your name and city


09.37 We used single line comment in this tutorial


09.40 Now just try to give a multiline comment



09.44 Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial


09.47 It summarises the Spoken Tutorial project


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


09.55 Conducts workshops using spoken tutorials


09.58 Gives certificates to those who pass an online test


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


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


10.28 Thank you for watching

Contributors and Content Editors

Gaurav, Jyotisolanki, Krupali, PoojaMoolya, Pratik kamble