Advanced-C++/C2/Abstract-Class/English-timed
From Script | Spoken-Tutorial
Revision as of 07:31, 29 June 2015 by Sandhya.np14 (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken-tutorial on abstract class and pure virtual function in C++. |
00:08 | In this tutorial, we will learn: |
00:10 | * Abstract Classes |
00:11 | * Pure virtual function. |
00:13 | We will do this through an example. |
00:16 | To record this tutorial, I am using: |
00:19 | *Ubuntu OS version 11.10 |
00:23 | *g++ compiler version 4.6.1 |
00:27 | Let us start with an introduction to abstract class. |
00:31 | Abstract class is always a base class. |
00:35 | It contains at least one pure virtual function. |
00:39 | We cannot create an instance of abstract class. |
00:43 | Let us see pure virtual function. |
00:45 | A pure virtual function is a function with no body. |
00:49 | It is not defined in the base class. |
00:52 | It is declared as: |
00:54 | virtual void virtualfunname()=0; |
01:00 | A derived class must override the function. |
01:04 | Otherwise the compiler will give an error. |
01:07 | It is up to a derived class to implement the function. |
01:11 | Let us look at an example. |
01:13 | I have already typed the code on the editor. |
01:16 | I will open it. |
01:18 | Note that our file name is abstract.cpp. |
01:22 | This example involves addition and subtraction of two numbers. |
01:28 | Let us go through the code. |
01:30 | This is our header file as iostream. |
01:33 | Here we are using the std namespace. |
01:36 | This is declaration for a class named abstractinterface. |
01:41 | Then we have public specifier. |
01:44 | In this, we have declared a virtual function named numbers(). |
01:49 | It is initialized to 0. |
01:51 | Then we have a non-virtual function. |
01:55 | And two integer variables as 'a' and 'b'. |
01:59 | Here, we access the input function. |
02:01 | In this, we accept the numbers 'a' and 'b'. |
02:05 | This is a derived class named add. |
02:09 | It inherits the properties of the base class "abstractinterface". |
02:14 | Here we override the function numbers(). |
02:18 | In this, we perform addition of two numbers 'a'and 'b' |
02:21 | and store the result in integer variable "sum". |
02:25 | Then we print the result. |
02:27 | Here we have another derived class as sub. |
02:31 | This also inherits the base class "abstractinterface". |
02:35 | In this, again we override the function numbers(). |
02:39 | And here we calculate the difference of two numbers 'a' and 'b'. |
02:43 | Then we print the difference. |
02:45 | This is our main() function. |
02:48 | Here we create an object of class "add" as obj1. |
02:53 | Then we call both the functions input() and numbers() using the object obj1. |
02:59 | Then we create another object of class "sub" as obj2. |
03:04 | Again, we call the two functions using the object obj2. |
03:08 | And this is our 'return' statement. |
03:10 | Now let us execute the program. |
03:13 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
03:21 | To compile, type:
g++ space abstract dot cpp space hyphen o space abs |
03:31 | press Enter. |
03:32 | Type: dot slash abs |
03:34 | press Enter. |
03:36 | It is displayed as Enter the numbers. |
03:38 | I will enter as 9 and 4. |
03:42 | The output is displayed as Sum is 13. |
03:46 | Again we see Enter the numbers. |
03:49 | I will enter as 8 and 3. |
03:52 | The output is displayed as diff is 5. |
03:56 | This brings us to the end of this tutorial. |
03:59 | Come back to our slides. |
04:01 | Let us summarize. |
04:03 | In this tutorial, we learnt: |
04:04 | Abstract class e.g. class abstractinterface. |
04:09 | Pure virtual function e.g. virtual void numbers()=0; |
04:14 | As an assignment-
|
04:17 | * Create a pure virtual function as Info. |
04:20 | * Accept the name and roll no of the student in the function. |
04:25 | * Create two derived classes marks and sports. |
04:29 | * In marks, accept marks of three subjects. |
04:32 | * In sports, enter marks scored in sports. |
04:35 | * Calculate the total marks. |
04:38 | * Then create another derived class as result. |
04:41 | * In this, display the name, roll-no and total marks of the student. |
04:47 | Watch the video available at the link shown below. |
04:50 | It summarizes the Spoken-Tutorial project. |
04:53 | If you do not have good bandwidth, you can download and watch it. |
04:58 | The Spoken Tutorial Project team: Conducts workshops using spoken tutorials. |
05:03 | Gives certificates to those who pass an online test. |
05:07 | For more details, please write to: contact@spoken-tutorial.org |
05:14 | Spoken-Tutorial Project is a part of the "Talk to a Teacher" project. |
05:18 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
05:25 | More information on this mission is available at the link shown below: http://spoken-tutorial.org/NMEICT-Intro |
05:30 | This is Ashwini Patil from IIT Bombay, signing off.
Thank You for watching. |