Difference between revisions of "Advanced-C++/C2/Static-Members/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Static in C++ '''Author: '''Ashwini Patil '''Keywords: static, Video tutorial.''' {| style="border-spacing:0;" | style="background-color:#ffffff;borde…')
 
 
Line 57: Line 57:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Static data memebers'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Static functions'''
  
Members declared as '''static''' are the '''static data members'''.
+
A static function may be called by itself without depending on any object.
  
The '''keyword''' specifies that the member is shared by all '''instance''' of the '''class.'''
+
To access a static function we use,
 +
 
 +
classname :: staticfunction();
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''stat.cpp'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''stat.cpp'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see an example.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see an example on static memebers.
  
 
I have already typed the code on the editor.
 
I have already typed the code on the editor.
  
I will explain it.
+
 
 +
 
  
 
|-
 
|-
Line 75: Line 78:
  
 
'''stat.cpp'''
 
'''stat.cpp'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please note that I have saved the file with the name '''stat.cpp'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Note that I have saved the file with the name '''static.cpp'''
  
 
Let me explain the code now.
 
Let me explain the code now.
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
 +
 +
'''<nowiki>#include <iostream></nowiki>'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our headerfile as '''iostream'''
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
 +
 +
'''using namespace std;'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using the '''std namespace'''
  
 
|-
 
|-
Line 85: Line 100:
  
 
'''{ '''
 
'''{ '''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is a '''class''' named '''statex.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''class''' '''statex.'''
  
 
|-
 
|-
Line 97: Line 112:
  
 
'''static int sum; '''
 
'''static int sum; '''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we have declared''' x '''as''' non-static '''and''' sum '''as''' static '''variable'''.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we have a non-static variable as x declared as private.
 +
 
 +
Then we have a static variable sum declared as public.
  
 
|-
 
|-
Line 113: Line 130:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Function '''statex '''is '''public function.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our constructor statex.
  
 
In this we have incremented''' sum.'''
 
In this we have incremented''' sum.'''
  
Then value of''' sum '''is stored in''' x.'''
+
Then the value of''' sum '''is stored in''' x.'''
  
 
|-
 
|-
Line 129: Line 146:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have '''static function''' '''stat.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have a '''static function''' '''stat.'''
  
We print the''' sum.'''
+
In this we print the''' sum.'''
  
 
|-
 
|-
Line 145: Line 162:
  
 
'''};'''
 
'''};'''
| style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''number function.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have''' function number.'''
  
We print the''' x '''here.
+
Here we print the number''' x'''
  
Then class is closed.
+
The class is closed here.
  
 
|-
 
|-
Line 177: Line 194:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
  
'''statex o1;'''
+
'''statex o1, o2, o3;'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here '''o1''' are the object of class '''statex.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create objects of class statex.
 +
 
 +
As '''o1''', '''o2''' and '''o3.'''
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''statex::stat();'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Static''' function stat is accessed here.
+
 
 +
'''o1.number();'''
 +
 
 +
'''o2.number();'''
 +
 
 +
'''o3.number();'''
 +
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we call the function number using''' o1, o2 and o3.'''
 +
 
 +
 
  
Using the '''class''' name and scope resolution operator.
 
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''statex o2,o3; '''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We have '''o2''', '''o3''' and '''o4''' objects of '''class statex.'''
+
 
 +
'''statex::stat();'''
 +
 
 +
 
 +
 
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Static''' function '''stat''' is accessed here.
 +
 
 +
Using the '''class name '''and''' scope resolution operator.'''
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''o2.number(); '''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
  
'''o3.number();'''
+
'''<nowiki>cout<< “Now static var sum is ” <<o1.sum<< “\n”;</nowiki>'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we call the '''number''' function using object '''o1, o2, o3''' and '''o4.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we print the static variable''' sum.'''
  
 
|-
 
|-
Line 201: Line 234:
 
'''return 0;'''
 
'''return 0;'''
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the '''return''' statement.
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the '''return''' statement.
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Click on save
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now Click on '''Save.'''
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program.
  
 
|-
 
|-
Line 219: Line 248:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type  
  
'''g++ stat.cpp -o st'''
+
'''g++ static.cpp -o stat'''
  
 
To execute
 
To execute
Line 225: Line 254:
 
Type
 
Type
  
'''./st'''
+
'''./stat'''
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type  
  
'''g++ stat.cpp -o st'''
+
'''g++ static.cpp -o stat'''
  
 
To execute
 
To execute
Line 234: Line 263:
 
Type
 
Type
  
'''./st'''
+
'''./stat'''
  
 
|-
 
|-
Line 242: Line 271:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we see,
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we see,
  
'''Result is: 1 '''
+
'''Number is: 0'''
  
'''Number is: 2 '''
+
'''Number is: 1 '''
  
'''Number is: 3 '''
+
'''Number is: 2'''
 +
 
 +
'''Number is: 3'''
 +
 
 +
'''Now static var sum is 3'''
  
 
|-
 
|-
Line 252: Line 285:
  
 
'''Output'''
 
'''Output'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Let me explain the output now:'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now I will explain the output:
  
First the value of''' sum '''is''' 1. '''
+
Let me resize the window.
  
Then '''sum''' is incemented now the value is''' 2.'''
+
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the editor and terminal
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| First the value of''' number '''is''' 0 ie x is 0.'''
  
Like this it is incremented till '''3.'''
+
The first object gives the value as''' 0'''
 +
 
 +
Then we have the value as '''1''' ie''' x =1'''
 +
 
 +
The second object gives the value as''' 1'''
 +
 
 +
And the third object gives the value as''' 2'''
 +
 
 +
Then we call the stat function which gives the value of sum.
 +
 
 +
Result is sum.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the editor
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here sum is incemented and stored in''' x.'''
  
 +
Hence this will gived the value as''' 3.'''
  
 +
Thus the final output we have is.
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us go back to our program.
+
'''Static var sum is 3.'''
  
Now i will create one more object of '''class''' '''statex''' as '''o5.'''
+
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Edit
 +
 
 +
'''statex 01, 02, 03, 04;'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us create another object here as '''o4.'''
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the editor
 +
 
 +
 
  
'''statex o5;'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I will call the function '''number''' usign the''' object o4.'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type''' statex o5;'''
+
  
Now click on save and let us execute and see.
+
'''Click on Save'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the terminal
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the terminal
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Come back to the terminal.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute.
  
Compile and execute as before.
+
Press the arrow key twice.
 +
 
 +
Again Press the arrow key twice.
  
 
|-
 
|-
Line 287: Line 342:
  
 
Output
 
Output
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Result is: 2'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''You can see that result is 4.'''
  
'''Number is: 3 '''
+
'''Result is 4'''
  
'''Number is: 4 '''
+
'''Now static var sum is 4'''
 +
 
 +
As the 4th object is created.
  
 
|-
 
|-
Line 297: Line 354:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
  
Let us come back to the slides.
+
Come back to the slides.
  
 
|-
 
|-
Line 303: Line 360:
  
 
Summary
 
Summary
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial, we have seen,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize:
 +
 
 +
In this tutorial, we have seen,
  
 
'''static keyword.'''
 
'''static keyword.'''
  
'''Static data member.'''
+
'''Static variable'''
 +
 
 +
'''eg. static int sum;'''
  
 
'''Static function.'''
 
'''Static function.'''
 +
 +
'''eg. static void stat()'''
  
 
|-
 
|-

Latest revision as of 17:02, 9 May 2013

Title of script: Static in C++

Author: Ashwini Patil

Keywords: static, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on static members in C++.
Slide 2


In this tutorial we will learn,

Static keyowrd.

Static data memeber.

Static member functions.

We will do this with the help of examples.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.04

gcc and g++ compiler v. 4.6.1

Slide 4 Let us start with the introduction to static.

Static means fixed, which cannot change.

The word justifies its meaning.

If a variable is defined as static, it means that it is initialized.

It will remain in the memory till the end of the program.

Static variables are initialized to zero before the first object is created.

Slide 5 Static functions

A static function may be called by itself without depending on any object.

To access a static function we use,

classname :: staticfunction();

Open stat.cpp Let us see an example on static memebers.

I have already typed the code on the editor.



Point the cursor

stat.cpp

Note that I have saved the file with the name static.cpp

Let me explain the code now.

Highlight

#include <iostream>

This is our headerfile as iostream
Highlight

using namespace std;

Here we are using the std namespace
Highlight

class statex

{

Then we have class statex.
Highlight

private:

int x;

public:

static int sum;

In this we have a non-static variable as x declared as private.

Then we have a static variable sum declared as public.

Highlight

static int sum;

statex()

{

sum++;

x=sum;

}

This is our constructor statex.

In this we have incremented sum.

Then the value of sum is stored in x.

Highlight

static void stat()

{

cout << "Result is: " << sum<<"\n";

}

Here we have a static function stat.

In this we print the sum.

Highlight

void number()

{

cout << "Number is: " << x<<"\n";

}

};

Then we have function number.

Here we print the number x

The class is closed here.

Highlight

int statex::sum=0;


To declare the static variable globally we use scope resolution operator.

To access a static variable we write as:

datatype classname::static var name.

Now the storage is allocated to variable sum.

It is initialized to 0.

Highlight

int main()

This is our main function.
Highlight

statex o1, o2, o3;

Here we create objects of class statex.

As o1, o2 and o3.

Highlight

o1.number();

o2.number();

o3.number();

Then we call the function number using o1, o2 and o3.



Highlight

statex::stat();


Static function stat is accessed here.

Using the class name and scope resolution operator.

Highlight

cout<< “Now static var sum is ” <<o1.sum<< “\n”;

Here we print the static variable sum.
Highlight

return 0;

This is the return statement.
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

g++ static.cpp -o stat

To execute

Type

./stat

To compile the program type

g++ static.cpp -o stat

To execute

Type

./stat

Highlight

Output

Here we see,

Number is: 0

Number is: 1

Number is: 2

Number is: 3

Now static var sum is 3

Highlight

Output

Now I will explain the output:

Let me resize the window.

On the editor and terminal First the value of number is 0 ie x is 0.

The first object gives the value as 0

Then we have the value as 1 ie x =1

The second object gives the value as 1

And the third object gives the value as 2

Then we call the stat function which gives the value of sum.

Result is sum.

Here sum is incemented and stored in x.

Hence this will gived the value as 3.

Thus the final output we have is.

Static var sum is 3.

Edit

statex 01, 02, 03, 04;

Now let us create another object here as o4.
On the editor


I will call the function number usign the object o4.

Click on Save

On the terminal Let us execute.

Press the arrow key twice.

Again Press the arrow key twice.

Highlighted

Output

You can see that result is 4.

Result is 4

Now static var sum is 4

As the 4th object is created.

Switch back to the slides This brings us to the end of this tutorial.

Come back to the slides.

Slide 7

Summary

Let us summarize:

In this tutorial, we have seen,

static keyword.

Static variable

eg. static int sum;

Static function.

eg. static void stat()

Slide 6

Assignment

As an assignment

Create a class that declares a static variable.

Decrement the variable.

And print the value.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown

It summarizes the Spoken Tutorial project

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

Slide 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 Indiaabout:startpage

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