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

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: First C++ program

Author: Ashwini

Keywords: C++ Program, cout<<, cin>>, int main(), Video Tutorial


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

I am using gedit in this tutorial.

For relevant tutorial Please visit our website which is as shown:

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 “&” sign

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 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 tells 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 should have 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 } Then type closing curly bracket “}”

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

Now 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 “<<”
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 Here \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

And 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

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

talk.cpp is our filename

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

We see that the program is compiled.
Type

ls -lrt

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

./output

Let us execute the program,

type dot slash “./output”

And 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

Switch back to our text editor.

{ Suppose here we miss the closing curly bracket}


Now save the file.
Let us execute

Come back to our terminal

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

Highlight Line no.7

Highlight talk.cpp

Highlight {

We see an error.

we see that there is an error at line no.7 in our talk.cpp file That Expected curly bracket at the end of input.

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

Now Save the file.
Let us execute it again.

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 our 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 our terminal.Let us Compile.

We see that there is an error at line no 3 and line no 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 now 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 below

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