Difference between revisions of "Advanced-C++/C2/Static-Members/English-timed"
From Script | Spoken-Tutorial
Line 329: | Line 329: | ||
|- | |- | ||
| 05:12 | | 05:12 | ||
− | |'''static keyword''''''static variable''' | + | |'''static keyword''' '''static variable''' |
|- | |- |
Latest revision as of 12:58, 23 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on static members in C++. |
00:06 | In this tutorial, we will learn: |
00:09 | Static keyword. Static variable |
00:12 | Static function. |
00:14 | We will do this through an example. |
00:17 | To record this tutorial, I am using: |
00:20 | Ubuntu OS version 11.10 |
00:24 | g++ compiler version 4.6.1 |
00:29 | Let us start with an introduction to static. |
00:33 | Static variables are initialized to zero before the first object is created. |
00:39 | Only one copy of the static variable exists for the whole program. |
00:44 | All the objects will share that variable. |
00:47 | It will remain in the memory till the end of the program. |
00:52 | Static functions- |
00:54 | A static function may be called by itself without depending on any object. |
01:00 | To access a static function, we use- |
01:03 | classname :: (scope resolution operator) and the staticfunction(); |
01:09 | Let us see an example on static memebers. |
01:13 | I have already typed the code on the editor. |
01:17 | Note that our file name is static dot cpp. |
01:21 | Let me explain the code now. |
01:24 | This is our header file as iostream. |
01:27 | Here we are using the std namespace. |
01:31 | Then we have class statex. |
01:34 | In this, we have a non-static variable as 'x' declared as private. |
01:40 | Then we have a static variable sum declared as public. |
01:45 | This is our constructor statex. |
01:48 | In this, we have incremented sum. |
01:52 | Then the value of sum is stored in 'x'. |
01:55 | Here we have a static function stat. |
01:58 | In this, we print the sum. |
02:01 | Then we have function number(). |
02:04 | Here we print the number 'x'. |
02:07 | The class is closed here. |
02:10 | To declare the static variable globally, we use scope resolution operator. |
02:15 | To access a static variable we write as: |
02:19 | datatype classname scope resolution operator and the static variable name. |
02:26 | Now the storage is allocated to variable sum. |
02:31 | It is initialized to 0. |
02:33 | This is our main() function. |
02:35 | Here, we create objects of class statex |
02:39 | as o1, o2 and o3. |
02:42 | Then we call the function number() using the objects o1, o2 and o3. |
02:49 | Static function stat() is accessed here |
02:52 | using the class name and the scope resolution operator. |
02:56 | Here, we print the static variable sum. |
03:00 | And this is our return statement. |
03:03 | Let us execute the program. |
03:05 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
03:13 | To compile, type: |
03:15 | g++ space static dot cpp space hyphen o space stat. Press Enter. |
03:24 | Type: ./stat (dot slash stat). Press Enter. |
03:28 | The output is displayed as: |
03:30 | Number is: 0, 1, 2 |
03:33 | Result is: 3 |
03:35 | Now static var sum is 3. |
03:38 | Now, I will explain the output. Let me resize the window. |
03:42 | Here, first the value of number is 0 i.e.' x' is 0. |
03:49 | The first object gives the value as 0. |
03:53 | Then we have the value as 1 i.e. x =1. |
03:58 | The second object gives the value as 1. |
04:01 | And the third object gives the value as 2. |
04:05 | Then we call the stat() function which gives the value of sum. |
04:10 | Result is: sum. |
04:13 | Here, sum is incremented and stored in x. |
04:18 | Hence, this will give the value as 3. |
04:22 | Thus the final output we have is. |
04:25 | Static var sum is 3. |
04:28 | Now, let us create another object here as o4. |
04:34 | I will call the function number() using the object o4. |
04:43 | Click on Save. |
04:45 | Let us execute. |
04:48 | Press the up arrow key twice. |
04:51 | Again press the up arrow key twice. |
04:54 | You can see that Result is 4. |
04:57 | Now static var sum is 4. |
05:00 | As the 4th object is created. |
05:03 | This brings us to the end of this tutorial. |
05:06 | Come back to our slides. |
05:08 | Let us summarize. |
05:10 | In this tutorial, we learned: |
05:12 | static keyword static variable |
05:15 | eg. static int sum; |
05:18 | static function. eg. static void stat() |
05:22 | As an assignment- Create a class that declares a static variable, |
05:26 | decrement the variable, |
05:29 | And print the value. |
05:31 | Watch the video available at the link shown below. |
05:34 | It summarizes the Spoken Tutorial project. |
05:37 | If you do not have good bandwidth, you can download and watch it. |
05:41 | The Spoken Tutorial Project Team: |
05:44 | Conducts workshops using spoken tutorials. |
05:47 | Gives certificates to those who pass an online test. |
05:51 | For more details, please write to, contact@spoken-tutorial.org |
05:58 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
06:02 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
06:08 | More information on this mission is available at the link shown below. |
06:13 | This is Ashwini Patil from IIT Bombay, signing off.Thank You for joining. |