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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 74: Line 74:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''gedit talk.cpp &'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''gedit talk.cpp &'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To open the text editor, type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To open the text editor, type on the terminal
  
 
'''“gedit”''' space '''“talk”''' dot '''“.cpp”''' space ampersand '''“&”.'''
 
'''“gedit”''' space '''“talk”''' dot '''“.cpp”''' space ampersand '''“&”.'''
Line 132: Line 132:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here iostream is a header file  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here iostream is a header file  
  
This file includes the declaration of basic standard input output library in''' C++.'''
+
This file includes the declaration of basic standard input output functions in''' C++.'''
  
 
|-
 
|-
Line 200: Line 200:
 
The empty pair of parentheses indicates that '''main''' has no '''arguments'''
 
The empty pair of parentheses indicates that '''main''' has no '''arguments'''
  
The concept of '''arguments''' will be discussed in detail in the upcoming tutorials.
+
The concept of '''arguments''' will be discussed in the upcoming tutorials.
  
 
Now come back to our program.
 
Now come back to our program.
Line 236: Line 236:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>cout<<</nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>cout<<</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here '''cout '''is a standard '''C++ function''' to print the output
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here '''cout '''is a standard '''C++ function''' to print the output on the terminal
  
And '''<nowiki><< </nowiki>'''is an output operator.
 
  
 
|-
 
|-
Line 246: Line 245:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now after the brackets, type
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now after the brackets, type
  
within the double quotes '''“Talk to a teacher backslash \n”'''.
+
within the double quotes  
 
+
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''“”'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''“”'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Anything within the double quotes in the '''<nowiki>cout <<</nowiki>''' statement will be printed.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Anything within the double quotes in the '''<nowiki>cout <<</nowiki>''' function  will be printed.
 +
 
 +
Now inside the quotes type '''“Talk to a teacher backslash \n”'''.
  
 
|-
 
|-
Line 274: Line 274:
  
 
'''return 0; '''
 
'''return 0; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now press '''Enter'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now press '''Enter'''. Give a space here
  
 
Type '''“return”''' space '''“0”''' and a semicolon '''“;”.'''
 
Type '''“return”''' space '''“0”''' and a semicolon '''“;”.'''
Line 311: Line 311:
  
 
'''g++ talk.cpp -o output'''  
 
'''g++ talk.cpp -o output'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our terminal
  
 
Type '''“g++”''' space '''“talk.cpp”''' space hyphen '''“-o”''' space '''“output”.'''
 
Type '''“g++”''' space '''“talk.cpp”''' space hyphen '''“-o”''' space '''“output”.'''
Line 324: Line 324:
  
 
'''talk.cpp''' is the filename
 
'''talk.cpp''' is the filename
 
on compilation g++ creates an executable
 
  
 
'''-o output''' says that the executable should go to the file output.
 
'''-o output''' says that the executable should go to the file output.
Line 396: Line 394:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to out program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our text editor.
  
 
|-
 
|-
Line 412: Line 410:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute it
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute it
  
You can recall the previously entered commands by using up arrow
+
You can recall the previously entered commands by using up arrow key
  
 
That is what I did now
 
That is what I did now
Line 422: Line 420:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will show you another common error
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will show you another common error
  
Let us switch back to the editor.
+
Let us switch back to text editor.
  
 
|-
 
|-
Line 430: Line 428:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save it.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|let us  Save the file.
  
 
|-
 
|-
Line 442: Line 440:
  
 
Highlight '''cout'''
 
Highlight '''cout'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile and run it as before
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to terminal.Let us Compile
  
We see that there are errors at line no 3 and 6 in our '''talk.cpp''' file
+
We see that there is an error at line no 3 and 6 in our '''talk.cpp''' file that
  
 
expected identifier before '''semicolon'''  
 
expected identifier before '''semicolon'''  
Line 462: Line 460:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error
  
Come back to our program
+
Come back to our text editor.
  
type '''std.'''  
+
type '''std.''' here
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save it.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Let us Save it.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute it
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile it again
  
 
Yes it is working.
 
Yes it is working.
Line 520: Line 518:
 
Gives certificates to those who pass an online test  
 
Gives certificates to those who pass an online test  
  
For more details, contact spoken-tutorial.org
+
For more details, please write to contact @spoken-tutorial.org
  
 
|-
 
|-
Line 531: Line 529:
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
More information on this Mission is available at: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro
+
More information on this Mission is available at the link shown below : [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro
  
 
|-
 
|-

Revision as of 15:32, 29 January 2014

Title of script: First C++ program

Author: Ashwini

Keywords: int main(),


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on First C++ program.
Slide 2

Learning Objectives

In this tutorial I am going to explain,

How to write a C++ program.

How to compile it.

How to execute it.

We will also explain some common errors and their solutions.

Slide 3

System Requirements


To record this tutorial, I am using

Ubuntu operating system version 11.10

and G++ Compiler version 4.5.2 on Ubuntu.

Slide 4

Prerequisites


To practice this tutorial,

You should be familiar with Ubuntu Operating System

and an Editor

Some editors are vim, gedit.

I am using gedit in this tutorial.

For relevant tutorial Please visit our website:

http://spoken-tutorial.org

Open the Terminal


Open the terminal using Ctrl + Alt + T

Let me tell you how to write a C++ program through an example

Open terminal Window using Ctrl, Alt and T keys simultaneously on your keyboard.

gedit talk.cpp & To open the text editor, type on the terminal

“gedit” space “talk” dot “.cpp” space ampersand “&”.

Highlight & We use the “&” to free up the prompt.
Highlight .cpp Please note that all the C++ files will have the extension “.cpp”
Now Press Enter

The text editor has opened.

Let us start to write a program.
//My first C++ program Type double slash “//” space

“My first C++ program”.

Highlight // Here, double slash is used to comment the line

Comments are used to understand the flow of program

It is useful for documentation

It gives us information about the program

The double slash is called as single line comment.

Type #include <> Now press Enter

Type hash “#include” space opening angle bracket “<” closing angle bracket “>”.

It is a good practice to complete the brackets first, and then start writing inside it

Type #include <iostream> Now, inside the bracket, type

“iostream”.

Here iostream is a header file

This file includes the declaration of basic standard input output functions in C++.

Type using namespace std; Now press Enter

Type “using” space “namespace” space “std” and a semicolon “;”

Highlight using


Highlight namespace


Highlight std

The using statement informs the compiler that you want to use the std namespace

The purpose of namespace is to avoid name collisionsis

It is done by localizing the names of identifiers

It creates a declarative region and defines a scope

Anything defined within a namespace is in the SCOPE of that namespace

Here, std is the namespace in which entire standard C++ library is declared.

Type int main() Now Press Enter.

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

Highlight main() Main is a special function

It denotes that the execution of the program begins from this line.

The opening and the closing bracket is called as parenthesis.

Highlight () Parenthesis followed by main is to tell the user that main is a function.
Highlight int main() Here the int main function takes no arguments and returns a value of type integer.
We will learn about data types in another tutorial.
Now let us switch to the slides to know more about the main function.
Every program will have only one main function

There should NOT be more than one “main” function

Otherwise the compiler cannot locate the beginning of the program

The empty pair of parentheses indicates that main has no arguments

The concept of arguments will be discussed in the upcoming tutorials.

Now come back to our program.

Type { press Enter.

Type opening curly bracket “{”

The opening curly bracket marks the beginning of the function main.

Type } Type closing curly bracket “}”

The closing curly bracket indicates the end of the function main.

Inside the bracket press enter twice

Move the cursor one line up.

Indentation makes the code easier to read

It also helps to locate errors faster.

So let us give a space here.

Type cout << And type “cout” space two opening angle bracket “<<” space .
Highlight cout<< Here cout is a standard C++ function to print the output on the terminal


Type

cout << “Talk to a teacher\n”

Now after the brackets, type

within the double quotes

Highlight “” Anything within the double quotes in the cout << function will be printed.

Now inside the quotes type “Talk to a teacher backslash \n”.

Highlight \n \n signifies newline

As a result, after execution of the cout function, the cursor moves to the new line.

Type

“;”

Every C++ statement must end with a semicolon

Hence type it at the end of this line.

Highlight ; Semicolon acts as a statement terminator.
Type

return 0;

Now press Enter. Give a space here

Type “return” space “0” and a semicolon “;”.

Highlight return 0;


Highlight int

This statement returns the integer zero.

An integer has to be returned for this function.

Because the function type is int.

The return statement marks the end of executable statements.

We will learn more about the returned values in another tutorial.

Save Now click on the “Save” button to save the file.

It is a good habit to save files frequently.

This will protect you from sudden power failures.

It will also be useful in case the applications were to crash.

Compile Let us now compile the program.
Type

g++ talk.cpp -o output

Come back to our terminal

Type “g++” space “talk.cpp” space hyphen “-o” space “output”.

Highlight g++

Highlight talk.cpp

Highlight -o output

g++ is the compiler used to compile C++ programs

talk.cpp is the filename

-o output says that the executable should go to the file output.

We see that the program is compiled.
Type

ls -lrt

By typing ls -lrt, we can see that output is the last file to be created.
Type

./output

To execute the program,

type dot slash “./output”

Press Enter.

Highlight Talk to a teacher Here the output is displayed as “Talk to a teacher”.
Errors


Now let us see the common errors which we can come across

Please switch to the editor.

{ Suppose we miss the {

I will retain rest of the code as it is.

save the program.
Let us execute it again

Let us go to the terminal

Now compile and run the program using the command we used before.
Highlight error

Highlight Line no.7

Highlight talk.cpp

Highlight {

we see that there is an error at line no.7 in our talk.cpp file

Expected curly bracket at the end of input.

Come back to our text editor.
As i said before the closing curly bracket marks the end of the function main

Hence reinsert the bracket .

Save it.
Let us execute it

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

That is what I did now

Yes it is working.

Error 2 I will show you another common error

Let us switch back to text editor.

Now, suppose here we missed std.
let us Save the file.
Highlight error

Highlight Line no.3 and 6

Highlight talk.cpp

Highlight ;

Highlight cout

Come back to terminal.Let us Compile

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

expected identifier before semicolon

and cout was not declared in this scope.

As cout is the standard C++ library function

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

Hence it is giving an error.

Let us fix the error

Come back to our text editor.

type std. here

Let us Save it.
Let us compile it again

Yes it is working.

As an assignment,

Write a program to print your name and city

We used single line comment in this tutorial

Now just try to give multiline comment

Hint:

/*..................

.....................*/

Slide 8

About the Spoken Tutorial Project


Watch the video available at the link shown

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it.

Slide Number 9

Spoken Tutorial Workshops


The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to contact @spoken-tutorial.org

Slide Number 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at the link shown below : http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank you for joining

Contributors and Content Editors

Ashwini, PoojaMoolya