Advanced-C++/C2/Inheritance/English-timed
From Script | Spoken-Tutorial
Revision as of 22:50, 27 June 2015 by Sandhya.np14 (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken tutorial on Inheritance in C++. |
00:06 | In this tutorial, we will learn: |
00:09 | * Inheritance |
00:10 | * Types of inheritance. |
00:12 | We will do this with the help of examples. |
00:16 | To record this tutorial, I am using: |
00:19 | * Ubuntu OS version 11.10 |
00:24 | * g++ compiler version 4.6.1 |
00:28 | Let us start with an introduction to Inheritance. |
00:32 | When one object acquires the property of another, it is called as inheritance. |
00:38 | It is the process of reusing the existing class without modifying them. |
00:44 | Types of Inheritance- |
00:45 | * Single level inheritance |
00:48 | * Multiple level inheritance |
00:50 | * Hierarchical Inheritance |
00:52 | * Multilevel inheritance and |
00:55 | * Hybrid Inheritance. |
00:57 | First, let us know about the base class and the derived class. |
01:02 | The base class has its own properties and functionality. |
01:06 | It is also called as parent class. |
01:09 | It has the common qualities that all the objects can inherit. |
01:14 | The derived class is the child class. |
01:18 | Derived class inherits the properties and functionality of the base class. |
01:23 | Let us see what is single level inheritance. |
01:27 | In single level inheritance only one base class and one derived class is needed. |
01:34 | Multiple inheritance- |
01:37 | In multiple inheritance, derived class inherits from more than one base class. |
01:44 | Hierarchical Inheritance- |
01:47 | In Hierarchical Inheritance, multiple derived classes inherit from one base class. |
01:55 | Multilevel inheritance- |
01:57 | In Multilevel inheritance, the subclass acts as the base class for other classes. |
02:05 | And Hybrid inheritance- |
02:08 | In hybrid inheritance, more than one form of inheritance is combined. |
02:14 | Now let us see an example on single level inheritance. |
02:18 | I have already typed the code on the editor. |
02:21 | I will explain it. |
02:23 | Note that our file name is exam_inherit.cpp. |
02:28 | The example involves to display the name, roll no and marks of the student. |
02:35 | This is our header file as iostream. |
02:38 | Here, we are using the std namespace. |
02:42 | Here, we have class student. |
02:44 | Then we have an integer variable roll_no and character array name, as private members of class student. |
02:53 | Function input() and function display() are the public functions of class student. |
02:59 | Here we are using the input() function to accept the roll no. and name of the student. |
03:06 | Then we have the display() function to display the roll_no and name. |
03:11 | Here we have another class as exam_inherit. |
03:16 | This is the derived class. |
03:18 | It inherits the function and data of class student. |
03:23 | Then we have declared sub1, sub2, sub3 and total |
03:28 | as private variables of class exam_inherit. |
03:33 | Here we have input_exam() and display_exam() as public functions. |
03:41 | Here we close the class exam_inherit. |
03:44 | Now, we are using input_exam() function to accept the marks of three subjects. |
03:52 | Here, we are using display_exam() function to calculate the total of three subjects. |
03:59 | Then we print a total. |
04:01 | This is our main() function. |
04:03 | Here we create an object of class exam_inherit as ex. |
04:10 | Then we call all the functions using the object ex. |
04:15 | And this is our return statement. |
04:18 | Let us execute the program. |
04:20 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously, on your keyboard. |
04:30 | To compile, type: |
04:31 | g++ (space) exam_inherit.cpp (space)-o (space) exam. |
04:41 | Press Enter. |
04:42 | Type: |
04:43 | ./ (dot slash) exam. Press Enter. |
04:46 | Here it is displayed as: |
04:47 | Enter Roll no.: |
04:49 | I will give as 1. |
04:51 | Enter Name: |
04:52 | I will enter as Arya. |
04:55 | Enter marks of subject1 |
04:57 | I will give as 60, |
05:00 | subject 2 as 70 |
05:02 | and subjec 3 as 80. |
05:06 | The output is displayed as: |
05:08 | Roll no is: 1 |
05:09 | Name is: Arya and |
05:11 | Total is: 210. |
05:13 | Now, we will see multilevel inheritance in the same example. |
05:18 | I have already typed the code. |
05:20 | Let's switch back to our editor. |
05:22 | Note that our file name is multilevel.cpp. |
05:28 | Here, we have declared the variable total. |
05:32 | as public variable of class exam_inherit. |
05:38 | This is because the private members are not accessed by the derived class. |
05:44 | Here we have another class as grade. |
05:49 | This inherits the class exam_inherit. |
05:53 | Class grade is the derived class |
05:56 | and class exam_inherit is the base class for class grade. |
06:02 | All the functions and data of class exam_inherit will be inherited to class grade. |
06:11 | Here, we have declared avg as private member of class grade |
06:17 | and function average() as public function. |
06:21 | Then we close the class. |
06:23 | Here we use the average() function to calculate the average. |
06:27 | Then we print the average. |
06:30 | Inside the main function, we create an object of class grade as gd. |
06:36 | Then we call all the functions using the object gd. |
06:40 | gd.input() |
06:42 | input_exam() |
06:44 | display() |
06:45 | display_exam() |
06:46 | and the average() function. |
06:49 | And this is our return statement. |
06:52 | Now you can see that class grade is the derived class. |
06:56 | and exam_inherit is the base class for class grade. |
07:01 | And here class exam_inherit is the derived class. |
07:06 | And class student is the base class for class exam_inherit. |
07:12 | Let us execute the program. |
07:14 | Come back to our terminal. |
07:17 | To compile, type: |
07:18 | g++ (space) multilevel.cpp (space) -o (space) mul |
07:26 | Press Enter. |
07:27 | Type ./mul |
07:30 | Press Enter. |
07:32 | Here we see, Enter Roll no.: |
07:34 | I will enter as 2. |
07:36 | Enter Name: |
07:38 | I will enter as Pratham. |
07:41 | Enter marks of subject1 |
07:43 | I will give as 65, |
07:46 | subject2 as 67 and |
07:48 | subject3 as 82. |
07:52 | The output is displayed as: |
07:54 | Roll no is: 2 |
07:56 | Name is: Pratham |
07:57 | Total is: 214 and |
07:59 | Average is: 71. |
08:01 | This brings us to the end of this tutorial. |
08:05 | Let us move back to our slides. |
08:07 | Let us summarize. |
08:08 | In this tutorial, we learnt: |
08:10 | * Inheritance and |
08:11 | * Types of inheritance. |
08:14 | As an assignment- |
08:15 | write a program to create a class Shape. |
08:18 | Then create two functions of the class as Area and Perimeter. |
08:23 | Then find the area and perimeter of various shapes like square, rectangle and circle. |
08:31 | Watch the video available at the link shown below. |
08:34 | It summarizes the Spoken Tutorial project. |
08:37 | If you do not have good bandwidth, you can download and watch it. |
08:42 | The Spoken Tutorial Project Team: |
08:44 | Conducts workshops using spoken tutorials. |
08:47 | Gives certificates to those who pass an online test. |
08:51 | For more details, please write to: |
08:53 | contact@spoken-tutorial.org |
08:58 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
09:02 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
09:09 | More information on this mission is available at the link shown below. |
09:13 | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |