C-and-C++/C4/Working-With-Structures/Gujarati
From Script | Spoken-Tutorial
Time' | Narration
|
00:01 | Welcome to the spoken-tutorial on Structures in C and C++. |
00:06 | In this tutorial we will learn
|
00:09 | what is a Structure.
|
00:11 | Declaration of a structure
|
00:13 | We will do this through an example
|
00:16 | To record this tutorial, I am using,
|
00:19 | Ubuntu Operating System version 11.04,
|
00:23 | gcc and g++ Compiler version 4.6.1 |
00:29 | Let us start with an introduction to Structure
|
00:32 | When one or more variables are grouped under one name it is known as structure.
|
00:38 | Strucutre is used to group different data into one object.
|
00:43 | It is called as compound data-type.
|
00:45 | It is used to group related information together. |
00:49 | Now we will see
|
00:50 | The syntax to declare a structure
|
00:53 | Here the keyword struct tells the compiler that a structure is declared
|
00:59 | strcut_name is the name of the structure.
|
01:03 | eg. struct employee;
|
01:05 | You can give any name. |
01:07 | Now we will see how to declare a structure variable.
|
01:12 | Syntax for this is
|
01:14 | struct struct_name and struct_var;
|
01:17 | struct_var is the variable of type struc_name
|
01:22 | eg. struct employee addr;
|
01:27 | addr is the variable of type employee. |
01:31 | Let us move on to our example |
01:33 | I have already typed the program on the editor
|
01:36 | So let me open it. |
01:38 | Note that our filename is structure.c |
01:42 | In this program we will calculate the total marks of three subjects using structure |
01:49 | Let me explain the code now. |
01:51 | This is our header file. |
01:53 | Here we have declared a structure as student.
|
01:57 | Then we have declared three integer variables as english, maths and science.
|
02:04 | Variables defined under the structure are called as members of the structure. |
02:10 | This is our main function |
02:12 | Here we have declared an integer variable total. |
02:17 | Now we have declared a structure variable stud. stud is the variable of type student,it is used to access and modify the structure members
|
02:30 | Here we have modified the members |
02:32 | By assigning them values as 75, 70 and 65. |
02:38 | Here we calculate the total of three subjects. |
02:42 | Then we print the result. |
02:44 | This is our return statement. |
02:47 | Now click on Save |
02:49 | Let us execute the program |
02:51 | Please open the terminal window by pressing |
02:54 | Ctrl, Alt and T keys simultaneously on your keyboard. |
03:00 | To compile |
03:01 | Type; gcc space structure.c space -o space struct |
03:10 | And press Enter |
03:12 | To execute |
03:13 | Type./struct Press Enter |
03:18 | The output is displayed as: Total is 210 |
03:22 | Now we will execute the same program in C++ |
03:26 | Come back to our program. |
03:28 | I will edit the same code |
03:31 | First press shift, cntrl and S keys simultaneously on the keyboard |
03:37 | Now save the file with an extension .cpp and click on save |
03:44 | Let us change the header file as iostream |
03:48 | Now include the using statement and click on save |
03:57 | Structure declaration in C++ is same as in C |
04:02 | So no need to change anything here |
04:05 | At the end we will replace the printf statement with the cout statement. |
04:08 | Delete the format specifier and \n |
04:16 | Now delete the comma |
04:17 | Type ; two opening angle brackets. |
04:21 | Here delete the closing bracket |
04:23 | and type two opening angle backets |
04:25 | And within the double quotes type \n |
04:29 | Now click on Save |
04:31 | Let us execute the program |
04:33 | Come back to our terminal |
04:35 | To compile type g++ space structure.cpp space -o space struct1 |
04:47 | Here we have struct1 because we dont want to overwrite the output parameters for the file structure.c |
04:55 | Now press Enter |
04:57 | To execute type ./struct1 |
05:02 | Press Enter |
05:04 | The output is displayed as: Total is 210 |
05:08 | You can see that the output is same as our C code |
05:12 | Now let us go back to our slides. |
05:15 | We will summarize now. In this tutorial we learned, |
05:18 | Structure. |
05:19 | Syntax of a structure. |
05:21 | eg. struct struct_name; |
05:24 | To access members of a structure. |
05:26 | Eg: stud.maths = 75; |
05:30 | To add the structure variables. |
05:33 | Eg: total = stud.eng + stud.maths + stud.science; |
05:40 | As an assignment, |
05:41 | Write a program to diplay the records of an employee. |
05:45 | Like name, address, designation, salary. |
05:49 | Watch the video available at the link shown below |
05:53 | It summarises the Spoken Tutorial project |
05:55 | If you do not have good bandwidth, you can download and watch it |
06:00 | The Spoken Tutorial Project Team |
06:02 | Conducts workshops using spoken tutorials |
06:05 | Gives certificates to those who pass an online test |
06:08 | For more details, please write to contact@spoken-tutorial.org |
06:15 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
06:19 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
06:26 | More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
06:30 | This is Ashwini Patil from IIT Bombay. Siginning off. |
06:33 | Thank You for joining |