Difference between revisions of "C-and-C++/C2/Scope-Of-Variables/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Constructors and Destructors '''Author: '''Ashwini R. Patil '''Keywords: Constructors, Destructors, Video tutorial''' {| style="border-spacing:0;" | s…')
 
Line 1: Line 1:
'''Title of script''': Constructors and Destructors
+
'''Title of script''': Scope of variables
  
 
'''Author: '''Ashwini R. Patil
 
'''Author: '''Ashwini R. Patil
  
'''Keywords: Constructors, Destructors, Video tutorial'''
+
'''Keywords: Scope of variables, global variable, local variable, Video tutorial'''
  
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
! <center>Visual Cue</center>
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
! <center>Narration</center>
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Constructors and Destructors in C++.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Scope of variables in C and C++.
  
 
|-
 
|-
Line 22: Line 22:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
  
Constructors.
+
Scope of variable.
  
Destructors.
+
Global variable.
 +
 
 +
Local variable.
  
 
We will do this with the help of example.
 
We will do this with the help of example.
Line 39: Line 41:
 
'''Ubuntu Operating System''' version 11.04,
 
'''Ubuntu Operating System''' version 11.04,
  
'''g++''' '''Compiler''' version 4.6.1
+
'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on Ubuntu
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to Constructors and Destructors.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the scope of variables.
  
A constructor is a member function.
+
It is the region of code within which the variable can be accessed.
  
It has the same name as the class name.
+
Depending on its type and place of declaration it is divided into two catagories:
  
Constructors cannot return values.
+
Global Variable.
  
Constructor is automatically called when an object is created.
+
Local Variable.  
  
Types of Constructors are:
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type:
  
Parameterized Constructors.
 
  
Copy Constructors.
+
'''<nowiki>#include<stdio.h></nowiki>'''
  
Default Constructors.
 
  
We will discuss about them in another tutorial.
 
  
|-
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see an example.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on Constructors,
+
  
 
I have already typed the code on the editor,
 
I have already typed the code on the editor,
  
So i will open it
+
Let me open it
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point the cursor
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point the cursor
  
'''constructor.c'''
+
'''scope.c'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have saved the file with the name '''constructor.c'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''scope.c.'''
  
 
Let me explain the code now.
 
Let me explain the code now.
Line 82: Line 80:
 
| 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
  
'''<nowiki>#include <iostream></nowiki>'''
+
'''<nowiki>#include <stdio.h></nowiki>'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file.'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file.'''
  
Line 88: Line 86:
 
| 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
  
'''using namespace std;'''
+
'''int a=5;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the std namespace.
+
  
|-
+
'''int b=2;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared two global variables '''a and b.'''
  
'''class Addition'''
+
And we have '''initialized''' them by assigning value as '''5 and 2.'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have defined a class Addition'''.'''
+
  
 +
A global variable is available to all functions in your program.
  
 +
These are declared outside any functions above '''main()''' funtion.
  
 +
'''These have a Global scope.'''
  
 
|-
 
|-
 
| 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
  
'''int a, b ;'''
+
'''void add()'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' and '''b''' are the integer variables.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a '''function''' '''add '''without''' arguments.'''
  
 
|-
 
|-
 
| 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
  
'''public'''
+
'''int sum;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It is the access specifier.
+
  
It allows the data to be accessible by the other parts of our program.
+
'''sum = a+b;'''
 +
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here '''sum''' is a local variable'''.'''
  
|-
+
It is declared inside the function''' add.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''Rectangle(int, int)'''
+
A local variable is available only to the function inside which it is declared.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Addition is the construtor here.
+
 
+
A constructor has the same name as the class name.
+
 
+
We passed two arguements here.
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
 
+
'''int area( )'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have defined a function add.
+
  
 +
These variables are declared inside a block.
  
 +
'''These have local scope.'''
  
 +
The sum of''' a '''and''' b '''will be stored in the variable''' sum.'''
  
 
|-
 
|-
 
| 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
  
'''return(a + b);'''
+
'''printf("Sum is %d\n",c);'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns sum of a and b.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we print the '''sum.'''
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
 
+
'''Rectangle :: Rectangle (int a, int b)'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is a scope operator.
+
 
+
We must use this operator.
+
 
+
It specifies that function Addition is not a global function.
+
 
+
It is a member function of the class Addition.
+
 
+
Here we have passed two arguments.
+
 
+
int x and int y.
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
 
+
'''a = x;'''
+
 
+
'''b = y;'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now the value of x will be stored in variable a.
+
 
+
And the value of y will be stored in variable b.
+
  
 
|-
 
|-
Line 174: Line 138:
 
| 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
  
'''Addition obj (3, 4);'''
+
'''add();'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| obj is the object of class Addition.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''add''' function is called and then executed.
 
+
Here we pass two values as 3 and 4.
+
 
+
3 will be stored in a and 4 will be stored in b.
+
 
+
Which means 3 will be the width and 4 will be the height.
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
 
+
'''<nowiki>cout << “Area of the rectangle is ” <<rect.area() <<“\n”</nowiki>'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result.
+
 
+
Here we call the function area.
+
  
 
|-
 
|-
Line 195: Line 145:
  
 
'''return 0;'''
 
'''return 0;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''return''' statement.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is our return statement.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Click on save.
  
 
|-
 
|-
Line 214: Line 164:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
  
'''g++ constructor.c -o cons'''
+
'''gcc scope.c -o sco'''
  
 
To execute
 
To execute
Line 220: Line 170:
 
Type
 
Type
  
'''./cons'''
+
'''./sco'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type,
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type,
  
'''g++ constructor.c -o cons'''
+
'''gcc scope.c -o sco'''
 +
 
 +
'''Press Enter'''
  
 
To execute
 
To execute
Line 229: Line 181:
 
Type
 
Type
  
'''./cons'''
+
'''./sco'''
 +
 
 +
'''Press Enter'''
  
 
|-
 
|-
Line 235: Line 189:
  
 
'''Output'''
 
'''Output'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as
  
'''Area of rectangle is 12.'''
+
'''Sum of a and b is 7'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| NOW LET US SEE HOW THE DESTRUCTOR WORKS.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
  
 
Come back to our program
 
Come back to our program
Line 253: Line 207:
  
 
'''.cpp'''
 
'''.cpp'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''File''' menu.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First press '''shfit+ctrl '''and''' s '''keys simultaneously on your keyboard.
  
click on '''Save as option.'''
+
Now Save the file''' '''with an''' '''extension. '''.cpp '''and click on''' SAVE'''
 
+
Save the file''' '''with''' '''the name''' destructor.cpp'''.
+
  
 
|-
 
|-
Line 271: Line 223:
  
 
'''<nowiki><iostream></nowiki>'''
 
'''<nowiki><iostream></nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the header file as
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change the header file as
  
 
'''iostream.'''
 
'''iostream.'''
Line 279: Line 231:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will include the '''using '''statement here.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now include the '''using '''statement.
 +
 
 +
Click on '''Save'''.
  
 
|-
 
|-
 
| 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="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The function declaration in C++ is same as in C,
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The declaration of global variable and local variable is same in C++.
  
So no need to change anything here.
+
So no need to change anything.
  
 
|-
 
|-
Line 293: Line 247:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement
  
As we use '''<nowiki>cout<< function</nowiki>''' to print a line in C++.
+
 
 +
 
  
 
|-
 
|-
Line 299: Line 254:
  
 
'''%d\n'''
 
'''%d\n'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We don't need the '''format specifier''' and '\n' here,
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete the '''format specifier''' and ''''\n''''
  
So delete it.
+
Now delete the comma.
  
 
|-
 
|-
Line 307: Line 262:
  
 
'''<nowiki><<</nowiki>'''
 
'''<nowiki><<</nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type two opening angle brackets here
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type two opening angle brackets.Delete the closing bracket.
  
Then type '''sum '''two opening angle brackets again,
+
Again type two opening angle brackets and within the double quotes type backslash n.
  
and within the double quotes backslash n.
+
'''Now click on Save.'''
  
Delete the closing bracket.''' '''
+
'''Let us execute the program.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the terminal.
 
+
Come back to the terminal.
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
  
'''g++ function.cpp -o fun1'''
+
'''g++ scope.cpp -o sco1'''
  
Type
 
  
'''./fun1'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type,
 
  
'''g++ function.cpp -o fun1, '''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile Type,  
  
and press''' Enter.'''
+
'''g++ scope.cpp -o sco1'''
  
To execute,
+
Here we have''' sco1 '''because we don't want to''' '''overwrite the output parameter '''sco''' for the file''' scope.c'''
 +
 
 +
press''' Enter.'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
 +
 
 +
'''./sco1'''
 +
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute,
  
 
Type,
 
Type,
  
'''./fun1'''
+
'''./sco1'''
 +
 
 +
press''' Enter.'''
  
 
|-
 
|-
Line 345: Line 305:
  
 
'''output'''
 
'''output'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as,  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as,  
  
 
'''Sum of a and b is 9.'''
 
'''Sum of a and b is 9.'''
 +
 +
We can see that it is similar to our''' C '''code.
  
 
|-
 
|-
Line 367: Line 329:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on save.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on save.
 +
 +
We have declared the variable '''a '''above the '''main '''function.
 +
 +
And after the '''add '''function.
 +
 +
Let us see what happens.
  
 
Come back to the terminal.
 
Come back to the terminal.
Line 375: Line 343:
  
 
'''Error'''
 
'''Error'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile,
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile as before.
  
We see an error that declaration of int a shadows a parameter.
+
We see an errors
 +
 
 +
Redifinition of '''int a'''
 +
 
 +
'''int a''' previously defined here.
 +
 
 +
Come back to our program.
  
 
'''a''' is a global variable.
 
'''a''' is a global variable.
Line 385: Line 359:
 
We cannot declare the variable twice as it is already declared globally
 
We cannot declare the variable twice as it is already declared globally
  
We can only declare '''variable a''' as a local variable now.
+
We can only declare '''variable a''' as a local variable.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the program.
+
| style="background-color:transparent;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.
 
+
Let us fix the error.
+
  
 
|-
 
|-
Line 397: Line 369:
  
 
'''int a = 2;'''
 
'''int a = 2;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Remove this declaration.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete this.
  
 
|-
 
|-
Line 405: Line 377:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute.  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute again.  
  
 
Come back to our terminal.
 
Come back to our terminal.
Line 411: Line 383:
 
|-
 
|-
 
| 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="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Nocompile as before
 +
 
 +
Execute as before.
  
 
|-
 
|-
Line 417: Line 391:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Yes it is working.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Yes it is working.
  
Now come back to our slides.
+
This brings us to the end of this tutorial.
 +
 
 +
 
 +
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
 +
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize,
 +
 
 +
In this tutorial we learnt,
 +
 
 +
Scope of variables
 +
 
 +
Global variable
 +
 
 +
eg. int a =5;
 +
 
 +
Local variable
 +
 
 +
eg. int sum;
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
  
Line 426: Line 419:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
Line 436: Line 429:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
Line 453: Line 446:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide Number 10
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide Number 9
  
  

Revision as of 13:08, 26 December 2013

Title of script: Scope of variables

Author: Ashwini R. Patil

Keywords: Scope of variables, global variable, local variable, Video tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Scope of variables in C and C++.
Slide 2


In this tutorial we will learn,

Scope of variable.

Global variable.

Local variable.

We will do this with the help of example.

We will also see some common errors and their solutions.

Slide 3


To record this tutorial, I am using

Ubuntu Operating System version 11.04,

gcc and g++ Compiler version 4.6.1 on Ubuntu

Slide 4 Let us start with the introduction to the scope of variables.

It is the region of code within which the variable can be accessed.

Depending on its type and place of declaration it is divided into two catagories:

Global Variable.

Local Variable.

Type:


#include<stdio.h>


Now we will see an example.

I have already typed the code on the editor,

Let me open it

Point the cursor

scope.c

Note that our filename is scope.c.

Let me explain the code now.

Highlight

#include <stdio.h>

This is our header file.
Highlight

int a=5;

int b=2;

Here we have declared two global variables a and b.

And we have initialized them by assigning value as 5 and 2.

A global variable is available to all functions in your program.

These are declared outside any functions above main() funtion.

These have a Global scope.

Highlight

void add()

Here we have declared a function add without arguments.
Highlight

int sum;

sum = a+b;

Here sum is a local variable.

It is declared inside the function add.

A local variable is available only to the function inside which it is declared.

These variables are declared inside a block.

These have local scope.

The sum of a and b will be stored in the variable sum.

Highlight

printf("Sum is %d\n",c);

Here we print the sum.
Highlight

int main()

This is our main function.
Highlight

add();

The add function is called and then executed.
Highlight

return 0;

And this is our return statement.
Click on save Now Click on save.
Let us execute the program.
Open the terminal

Ctrl, Alt and T keys simultaneously

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

gcc scope.c -o sco

To execute

Type

./sco

To compile the program type,

gcc scope.c -o sco

Press Enter

To execute

Type

./sco

Press Enter

Highlight

Output

The output is displayed as

Sum of a and b is 7

On the editor NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++

Come back to our program

Go to File menu

Click on Save as option

Type

.cpp

First press shfit+ctrl and s keys simultaneously on your keyboard.

Now Save the file with an extension. .cpp and click on SAVE

Now let me change a few things here
Delete

<stdio.h>

Type

<iostream>

Let us change the header file as

iostream.

Type

using namespace std;

Now include the using statement.

Click on Save.

The declaration of global variable and local variable is same in C++.

So no need to change anything.

Type

cout <<

Now replace the printf statement with cout statement



Delete

%d\n

Delete the format specifier and '\n'

Now delete the comma.

Type

<<

Type two opening angle brackets.Delete the closing bracket.

Again type two opening angle brackets and within the double quotes type backslash n.

Now click on Save.

Let us execute the program.

On the terminal Come back to the terminal.
Type

g++ scope.cpp -o sco1


To compile Type,

g++ scope.cpp -o sco1

Here we have sco1 because we don't want to overwrite the output parameter sco for the file scope.c

press Enter.

Type

./sco1

To execute,

Type,

./sco1

press Enter.

Highlight

output

The output is displayed as,

Sum of a and b is 9.

We can see that it is similar to our C code.

Errors


Type

int a = 2;

Now we will see some common errors which we can come across.

Suppose here I will declare variable a again,

Type

int a = 2;

On the terminal Click on save.

We have declared the variable a above the main function.

And after the add function.

Let us see what happens.

Come back to the terminal.

Highlight


Error

Now compile as before.

We see an errors

Redifinition of int a

int a previously defined here.

Come back to our program.

a is a global variable.

It has a global scope.

We cannot declare the variable twice as it is already declared globally

We can only declare variable a as a local variable.

On the editor Let us fix the error.
Delete

int a = 2;

Delete this.
Click on save Click on save.
On the terminal Let us execute again.

Come back to our terminal.

Nocompile as before

Execute as before.

Yes it is working.

This brings us to the end of this tutorial.



Slide 5 Let us summarize,

In this tutorial we learnt,

Scope of variables

Global variable

eg. int a =5;

Local variable

eg. int sum;

Slide 6 As an assignment,

Write a program to print the difference of two numbers.

Slide 7

About the Spoken Tutorial Project

Watch the video available at the link shown .

It summarises the Spoken Tutorial project.

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

Slide 8

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 9


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: http://spoken-tutorial.org\NMEICT-Intro.

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini