Difference between revisions of "Advanced-C++/C2/Static-Members/English-timed"
From Script | Spoken-Tutorial
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
{| border = 1 | {| border = 1 | ||
| '''Time''' | | '''Time''' | ||
Line 7: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to the spoken tutorial on '''static members''' in C++. | + | | Welcome to the '''spoken tutorial''' on '''static members''' in '''C++'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
| 00:09 | | 00:09 | ||
− | |'''Static | + | |'''Static''' keyword. '''Static variable''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:12 | | 00:12 | ||
− | |'''Static function''' | + | |'''Static function'''. |
|- | |- | ||
| 00:14 | | 00:14 | ||
− | |We will do this through an | + | |We will do this through an example. |
|- | |- | ||
− | | | + | |00:17 |
− | | | + | |To record this tutorial, I am using: |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | '''Ubuntu OS '''version 11.10 | + | |'''Ubuntu OS '''version '''11.10''' |
|- | |- | ||
| 00:24 | | 00:24 | ||
− | | '''g++ compiler ''' | + | |'''g++ compiler '''version '''4.6.1''' |
|- | |- | ||
Line 51: | Line 45: | ||
|- | |- | ||
| 00:39 | | 00:39 | ||
− | |Only one copy of the static variable | + | |Only one copy of the '''static variable''' exists for the whole program. |
|- | |- | ||
Line 63: | Line 57: | ||
|- | |- | ||
|00:52 | |00:52 | ||
− | | '''Static functions''' | + | | '''Static functions'''- |
|- | |- | ||
| 00:54 | | 00:54 | ||
− | | A static function may be called by itself without depending on any object. | + | | A '''static function''' may be called by itself without depending on any '''object'''. |
|- | |- | ||
− | | 01 | + | | 01:00 |
− | | To access a static function we use | + | | To access a static function, we use- |
|- | |- | ||
− | | 01 | + | | 01:03 |
− | | classname :: (scope resolution operator) and the staticfunction(); | + | | '''classname :: (scope resolution operator)''' and the '''staticfunction();''' |
|- | |- | ||
| 01:09 | | 01:09 | ||
− | | Let us see an example on static | + | | Let us see an example on '''static memeber'''s. |
|- | |- | ||
| 01:13 | | 01:13 | ||
− | | I have already typed the code on the editor. | + | | I have already typed the code on the '''editor'''. |
|- | |- | ||
| 01:17 | | 01:17 | ||
− | | Note that our file name is '''static dot cpp''' | + | | Note that our file name is '''static dot cpp'''. |
|- | |- | ||
Line 95: | Line 89: | ||
|- | |- | ||
| 01:24 | | 01:24 | ||
− | |This is our | + | |This is our '''header file''' as '''iostream'''. |
|- | |- | ||
|01:27 | |01:27 | ||
− | | Here we are using the '''std namespace''' | + | | Here we are using the '''std namespace'''. |
|- | |- | ||
Line 107: | Line 101: | ||
|- | |- | ||
| 01:34 | | 01:34 | ||
− | | In this we have a non-static variable as x declared as '''private.''' | + | | In this, we have a '''non-static variable''' as 'x' declared as '''private.''' |
|- | |- | ||
| 01:40 | | 01:40 | ||
− | | Then we have a static variable '''sum''' declared as '''public.''' | + | | Then we have a '''static variable''' '''sum''' declared as '''public.''' |
|- | |- | ||
Line 119: | Line 113: | ||
|- | |- | ||
| 01:48 | | 01:48 | ||
− | | In this we have incremented''' sum.''' | + | | In this, we have incremented''' sum.''' |
|- | |- | ||
| 01:52 | | 01:52 | ||
− | | Then the value of''' sum '''is stored in'' | + | | Then the value of''' sum '''is stored in 'x'. |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | | Here we have a '''static function''' '''stat | + | | Here we have a '''static function''' '''stat'''. |
|- | |- | ||
| 01:58 | | 01:58 | ||
− | | In this we print the''' sum.''' | + | | In this, we print the''' sum.''' |
|- | |- | ||
| 02:01 | | 02:01 | ||
− | |Then we have''' | + | |Then we have function '''number().''' |
|- | |- | ||
| 02:04 | | 02:04 | ||
− | | Here we print the number | + | | Here we print the number 'x'. |
|- | |- | ||
| 02:07 | | 02:07 | ||
− | | The class is closed here. | + | | The '''class''' is closed here. |
|- | |- | ||
| 02:10 | | 02:10 | ||
− | | To declare the '''static variable''' globally we use scope resolution operator. | + | | To declare the '''static variable''' globally, we use '''scope resolution operator.''' |
|- | |- | ||
Line 155: | Line 149: | ||
|- | |- | ||
| 02:19 | | 02:19 | ||
− | | '''datatype classname scope resolution operator and static variable name.''' | + | | '''datatype classname scope resolution operator''' and the '''static variable name.''' |
|- | |- | ||
Line 167: | Line 161: | ||
|- | |- | ||
| 02:33 | | 02:33 | ||
− | | This is our '''main | + | | This is our '''main()''' function. |
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | Here we create objects of class '''statex | + | | Here, we create objects of '''class''' '''statex''' |
|- | |- | ||
| 02:39 | | 02:39 | ||
− | | | + | | as '''o1''', '''o2''' and '''o3.''' |
|- | |- | ||
| 02:42 | | 02:42 | ||
− | | Then we call the function number using the objects ''' o1, o2 and o3.''' | + | | Then we call the function '''number()''' using the objects '''o1, o2''' and '''o3.''' |
|- | |- | ||
| 02:49 | | 02:49 | ||
− | | '''Static''' function '''stat''' is accessed here | + | | '''Static''' function '''stat()''' is accessed here |
|- | |- | ||
| 02:52 | | 02:52 | ||
− | | | + | | using the '''class name '''and the ''' scope resolution operator.''' |
|- | |- | ||
| 02:56 | | 02:56 | ||
− | | Here we print the | + | | Here, we print the '''static variable sum.''' |
|- | |- | ||
| 03:00 | | 03:00 | ||
− | | And this is | + | | And this is our '''return''' statement. |
|- | |- | ||
| 03:03 | | 03:03 | ||
− | | Let us execute the program. | + | | Let us '''execute''' the program. |
|- | |- | ||
| 03:05 | | 03:05 | ||
− | | Open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard. | + | | Open the '''terminal window''' by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard. |
|- | |- | ||
| 03:13 | | 03:13 | ||
− | | To compile | + | | To compile, type: |
|- | |- | ||
| 03:15 | | 03:15 | ||
− | | '''g++ space static dot cpp space hyphen o space stat'''. Press Enter | + | | '''g++ space static dot cpp space hyphen o space stat'''. Press '''Enter'''. |
|- | |- | ||
| 03:24 | | 03:24 | ||
− | | Type '''./stat''' (dot slash stat). Press Enter | + | | Type: '''./stat''' (dot slash stat). Press '''Enter'''. |
|- | |- | ||
| 03:28 | | 03:28 | ||
− | | The output is displayed as | + | | The '''output''' is displayed as: |
|- | |- | ||
Line 231: | Line 225: | ||
|- | |- | ||
| 03:35 | | 03:35 | ||
− | | '''Now static var sum is 3''' | + | | '''Now static var sum is 3'''. |
|- | |- | ||
|03:38 | |03:38 | ||
− | | Now I will explain the output | + | | Now, I will explain the output. Let me resize the '''window'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 03:42 | | 03:42 | ||
− | | Here, | + | | Here, first the value of''' number '''is''' 0''' i.e.' x' is '''0.''' |
|- | |- | ||
| 03:49 | | 03:49 | ||
− | | The first object gives the value as''' 0''' | + | | The first '''object''' gives the '''value''' as''' 0'''. |
|- | |- | ||
| 03:53 | | 03:53 | ||
− | | Then we have the value as '''1''' | + | | Then we have the value as '''1''' i.e.''' x =1'''. |
|- | |- | ||
| 03:58 | | 03:58 | ||
− | | The second object gives the value as''' 1''' | + | | The second object gives the value as''' 1'''. |
|- | |- | ||
| 04:01 | | 04:01 | ||
− | | And the third object gives the value as''' 2''' | + | | And the third object gives the value as''' 2'''. |
|- | |- | ||
| 04:05 | | 04:05 | ||
− | | Then we call the '''stat''' function which gives the value of '''sum'''. | + | | Then we call the '''stat()''' function which gives the value of '''sum'''. |
|- | |- | ||
| 04:10 | | 04:10 | ||
− | | '''Result is sum.''' | + | | '''Result is: sum.''' |
|- | |- | ||
| 04:13 | | 04:13 | ||
− | | Here sum is incremented and stored in''' x.''' | + | | Here, '''sum''' is incremented and stored in '''x.''' |
|- | |- | ||
| 04:18 | | 04:18 | ||
− | | Hence this will | + | | Hence, this will give the value as''' 3.''' |
|- | |- | ||
Line 287: | Line 277: | ||
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | Now let us create another object here as '''o4.''' | + | | Now, let us create another '''object''' here as '''o4.''' |
|- | |- | ||
| 04:34 | | 04:34 | ||
− | | I will call the function '''number''' using the''' object o4.''' | + | | I will call the function '''number()''' using the''' object o4.''' |
|- | |- | ||
| 04:43 | | 04:43 | ||
− | | ''' | + | | Click on '''Save'''. |
|- | |- | ||
| 04:45 | | 04:45 | ||
− | | Let us execute. | + | | Let us '''execute'''. |
|- | |- | ||
| 04:48 | | 04:48 | ||
− | | Press the arrow key twice. | + | | Press the '''up arrow''' key twice. |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | Again | + | | Again press the '''up arrow''' key twice. |
|- | |- | ||
Line 315: | Line 305: | ||
|- | |- | ||
| 04:57 | | 04:57 | ||
− | | '''Now static var sum is 4''' | + | | '''Now static var sum is 4'''. |
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | As the 4th object is created. | + | | As the 4th '''object''' is created. |
|- | |- | ||
Line 327: | Line 317: | ||
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | Come back to | + | | Come back to our '''slides'''. |
|- | |- | ||
|05:08 | |05:08 | ||
− | | Let us summarize | + | | Let us summarize. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | In this tutorial, we learned | + | | In this tutorial, we learned: |
|- | |- | ||
| 05:12 | | 05:12 | ||
− | | '''static keyword | + | |'''static keyword''' '''static variable''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 351: | Line 337: | ||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | ''' | + | |'''static function.''' '''eg. static void stat()''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:22 | | 05:22 | ||
− | | As an assignment | + | | As an assignment- Create a '''class''' that declares a '''static variable''', |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:26 | | 05:26 | ||
− | | | + | | decrement the variable, |
|- | |- | ||
Line 375: | Line 353: | ||
|- | |- | ||
| 05:31 | | 05:31 | ||
− | | Watch the video available at the link shown | + | | Watch the video available at the link shown below. |
|- | |- | ||
| 05:34 | | 05:34 | ||
− | | It summarizes the Spoken Tutorial project | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 05:37 | | 05:37 | ||
− | | If you do not have good bandwidth, you can download and watch it | + | | If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 05:41 | | 05:41 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: |
|- | |- | ||
| 05:44 | | 05:44 | ||
− | | Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
| 05:47 | | 05:47 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
Line 403: | Line 381: | ||
|- | |- | ||
| 05:58 | | 05:58 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
|- | |- | ||
| 06:02 | | 06:02 | ||
− | | It is supported by the National Mission on Education through ICT, MHRD, Government of India | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 06:08 | | 06:08 | ||
− | | More information on this | + | | More information on this mission is available at the link shown below. |
|- | |- | ||
|06:13 | |06:13 | ||
− | | This is Ashwini Patil from IIT Bombay signing off | + | | This is Ashwini Patil from IIT Bombay, signing off.Thank You for joining. |
− | + | ||
− | Thank You for joining. | + | |
|} | |} |
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. |