C-and-C++/C2/First-C++-Program/English
Title of script: First C++ program
Author: Ashwini
Keywords: int main(),
|
|
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: |
Open the Terminal
|
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
“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 library in C++. | |
Type using namespace std; | Now press Enter
Type “using” space “namespace” space “std” and a semicolon “;” |
Highlight using
|
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 detail 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
And << is an output operator. |
Type
cout << “Talk to a teacher\n” |
Now after the brackets, type
within the double quotes “Talk to a teacher backslash \n”. |
Highlight “” | Anything within the double quotes in the cout << statement will be printed. |
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
Type “return” space “0” and a semicolon “;”. |
Highlight return 0;
|
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 |
Please open the 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 on compilation g++ creates an executable -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 out program. | |
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 That is what I did now Yes it is working. | |
Error 2 | I will show you another common error
Let us switch back to the editor. |
Now, suppose here we missed std. | |
Save it. | |
Highlight error
Highlight Line no.3 and 6 Highlight talk.cpp Highlight ; Highlight cout |
Compile and run it as before
We see that there are errors at line no 3 and 6 in our talk.cpp file 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 program type std. | |
Save it. | |
Let us execute it
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, contact spoken-tutorial.org |
Slide Number 10
|
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: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank you for joining |