Difference between revisions of "Advanced-C++/C2/More-On-Inheritance/English-timed"
From Script | Spoken-Tutorial
Line 13: | Line 13: | ||
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | | + | |'''Multiple Inheritance''' |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | | | + | |'''Hierarchical Inheritance'''. |
|- | |- |
Latest revision as of 14:38, 23 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on Multiple and Hierarchical Inheritance in C++. |
00:07 | In this tutorial, we will learn: |
00:09 | Multiple Inheritance |
00:11 | Hierarchical Inheritance. |
00:13 | We will do this with the help of examples. |
00:17 | To record this tutorial, I am using: |
00:20 | Ubuntu OS version 11.10 and |
00:24 | g++ compiler version 4.6.1 |
00:29 | In multiple inheritance, derived class inherits from more than one base class. |
00:36 | Now, we will see an example on multiple inheritance. |
00:40 | I have already typed the code on the editor. |
00:42 | I will open it. |
00:45 | Note that our file name is multiple.cpp. |
00:49 | In this program, we will display the name, roll no, marks and average of the student. |
00:56 | Let me explain the code. |
00:59 | This is our header file as iostream. |
01:01 | Here we are using the std namespace. |
01:05 | Then we have class student. |
01:07 | This is the base class. |
01:09 | In this, we have roll_no as integer variable and name as character variable. |
01:16 | These are declared as protected. |
01:19 | Then we have another class exam_inherit. |
01:24 | This is also a base class. |
01:26 | Hence we have two base classes here- student and exam_inherit. |
01:32 | In this, we have 3 variables- sub1, sub2 and sub3 as protected. |
01:38 | This is because protected variables can be accessed by derived class. |
01:44 | Now, here we have class "grade" which is the derived class. |
01:50 | This inherits the base classes – class "student"' and class "exam_inherit". |
01:56 | In this, we have avg as integer variable declared as private. |
02:02 | Then we have functions: |
02:04 | 'input()'display() |
02:06 | 'average()'input_exam() |
02:08 | and display_exam() as public functions. |
02:11 | In this, we have total as integer variable declared as public. |
02:17 | Then, we use input() function to accept the roll_no and name of the student. |
02:24 | In display() function, we display the roll_no and name of the student. |
02:28 | Here, we have function input_exam(). |
02:31 | In this, we accept the marks of three subjects as sub1, sub2 and sub3. |
02:37 | Then in display_exam() function, we calculate the total of three subjects and print the total. |
02:44 | And in function average(), we calculate the average. |
02:48 | This is our main() function. |
02:51 | In this, we create an object of class grade which is the derived class as gd. |
02:57 | Then we call all the above functions. |
03:01 | This is our return statement. |
03:03 | Now, let us execute the program. |
03:05 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
03:14 | To compile, type: g++ space multiple dot cpp space hyphen o space mult. Press Enter. |
03:24 | Type: dot slash mult. Press Enter. |
03:29 | Here we see, Enter Roll no.: |
03:32 | I will enter as 3. |
03:34 | Enter Name: |
03:36 | I will enter as Pratham. |
03:39 | Enter marks of subject1: |
03:41 | I will give as 67. |
03:43 | subject2 as 78 and. |
03:46 | subject3 as 84. |
03:48 | The output is displayed as: |
03:51 | 'Roll no is: 3'Name is: Pratham |
03:53 | Total is: 229 |
03:55 | Average is: 76. |
03:58 | This was multiple inheritance. |
04:00 | Now, we will see hierarchical inheritance. |
04:03 | Come back to our program. |
04:05 | In hierarchical inheritance, multiple derived classes inherit from one base class. |
04:12 | Note that our 'file name' is hierarchical dot cpp. |
04:16 | I will explain the code now. |
04:19 | This is our header file as iostream. |
04:22 | Here we have used the std namespace. |
04:25 | Then, we have class student which is the base class. |
04:29 | In this, we have roll_no as integer variable. |
04:34 | Sub1, sub2, sub3 and total as integer variables. |
04:40 | Then name as character variable. |
04:43 | These are declared protected. |
04:46 | Here, we have another class 'show'. |
04:49 | This is the derived class. |
04:51 | It inherits the properties of class student. |
04:54 | In this, we have two functions: input() and display(). |
04:59 | These are declared as public functions. |
05:02 | In function input(), we accept the roll_no and name of the student. |
05:07 | In function display(), we display the roll_no and name of the student. |
05:11 | Then we have another derived class as class exam. |
05:15 | This also inherits class student. |
05:19 | You can see that there are two derived classes- class exam and class show. |
05:26 | Both the classes inherit the class "student". |
05:30 | In class exam, we have two functions as input_exam() and total_marks() declared as public. |
05:38 | Here, we access the function input_exam(). |
05:41 | It accepts the marks of three subjects- sub1, sub2 and sub3. |
05:46 | Then we have total_marks() function. |
05:49 | It calculates the total of three subjects and prints the total. |
05:53 | This is our main() function. |
05:56 | In this, we create objects of three classes as st, sw and em. |
06:03 | Then we call all the above functions using these objects.
sw.input(); em.input_exam(); sw.display(); em.total_marks(); |
06:15 | And this is our return statement. |
06:17 | Now, let us execute the program. |
06:19 | Come back to the terminal. |
06:21 | Let me clear the prompt. |
06:24 | Let us compile, type: g++ space hierarchical dot cpp space hyphen o space hier |
06:36 | Press Enter.Type: dot slash hier |
06:41 | Press Enter. |
06:43 | Enter Roll no.:I will give as 4. |
06:46 | Enter Name:I will give as Ashwini. |
06:50 | Enter marks of subject1 |
06:52 | I will give as 87. |
06:54 | subject2 as 67 and subject3 as 97. |
07:00 | The output is displayed as: |
07:02 | Roll no is: 4 |
07:04 | Name is: Ashwini and |
07:06 | Total is : 251This brings us to the end of this tutorial. |
07:10 | Come back to our slides. |
07:13 | Let us summarize.In this tutorial, we learnt- |
07:16 | Multiple Inheritance and |
07:18 | Hierarchical Inheritance. |
07:20 | As an assignment: Create a class area and perimeter. |
07:25 | Find the area and perimeter of rectangle. |
07:29 | Watch the video available at the link shown below. |
07:32 | It summarizes the Spoken Tutorial project. |
07:35 | If you do not have good bandwidth, you can download and watch it. |
07:40 | The Spoken Tutorial Project Team: |
07:42 | Conducts workshops using spoken tutorials. |
07:45 | Gives certificates to those who pass an online test. |
07:49 | For more details, please write to: |
07:51 | contact@spoken-tutorial.org |
07:56 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
08:01 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
08:07 | More information on this mission is available at the link shown below. |
08:11 | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |