Difference between revisions of "Advanced-C++/C2/Abstract-Class/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Abstract class and pure virtual function in C++ '''Author: '''Ashwini Patil '''Keywords: abstract class, pure virtual function, Video tutorial.''' {| …')
 
 
(One intermediate revision by one other user not shown)
Line 13: Line 13:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 1
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 1
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Welcome to the spoken tutorial on '''abstract class and pure virtual function''' in '''C++.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Welcome to the spoken tutorial on '''abstract class''' and '''pure virtual function''' in '''C++.'''
  
 
|-
 
|-
Line 22: Line 22:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we will learn,
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we will learn,
  
Abstract Classes
+
*Abstract Classes
 
+
*Pure virtual function
Pure virtual function
+
*We will do this through an example.
 
+
We will do this with the help of an example.
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3  
 
 
  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using
 
+
*'''Ubuntu OS '''version 11.10
'''Ubuntu OS '''version 11.04
+
*'''g++ compiler '''v. 4.6.1
 
+
'''g++ compiler '''v. 4.6.1
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Abstract class '''is used as a base class.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with an introduction to '''abstract class.'''
  
It contains one '''pure virtual function.'''
+
'''Abstract class '''is used as a '''base class'''.
 +
 
 +
It contains at least one '''pure virtual function.'''
  
 
We cannot create an instance of '''abstract class.'''
 
We cannot create an instance of '''abstract class.'''
Line 49: Line 45:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| A '''pure virtual function''' is a function with no body.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see '''pure virtual function'''.
  
It is not defined in the base class.
+
A '''pure virtual function''' is a function with no '''body'''.
 +
 
 +
It is not defined in the '''base class'''.
  
 
It is declared as follows:
 
It is declared as follows:
  
virtual void virtualfunname()=0;
+
'''virtual void virtualfunname()=0;'''
  
A derived class must override the function.
+
A '''derived class''' must override the '''function'''.
  
Otherwise will give an error.
+
Otherwise the '''compiler''' will give an error.
  
It is upto a derived class to implement the function.
+
It is upto a '''derived class''' to implement the '''function'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''abstract.cpp'''
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''abstract.cpp'''
  
 +
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us look at an example
  
 +
I have already typed the code on the editor.
  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us look at an example
+
Note that our filename is '''abstract.cpp'''
  
 
This example involves addition and subtraction of two numbers.
 
This example involves addition and subtraction of two numbers.
 
I have already typed the code on the editor.
 
  
 
|-
 
|-
Line 78: Line 76:
  
 
'''abstract.cpp'''
 
'''abstract.cpp'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have saved the file with the name '''abstract.cpp'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us go through the code.
 
+
Let me explain the code now.
+
  
 
|-
 
|-
Line 92: Line 88:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the using statement.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here are using '''std namespace.'''
  
 
|-
 
|-
Line 107: Line 103:
  
 
'''public:'''
 
'''public:'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have public specifier.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''public specifier'''.
  
 
|-
 
|-
Line 113: Line 109:
  
 
'''virtual void numbers()=0;'''
 
'''virtual void numbers()=0;'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we have '''pure virtual function''' as '''numbers '''initialized to '''0.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we have declared a''' virtual function''' named '''"numbers".'''
  
This is the declaration for virtual function.
+
It is initialized to '''0.'''
 
+
It is defined when it is used.
+
  
 
|-
 
|-
Line 128: Line 122:
  
  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then a non-virtual function '''input''' is declared.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have a '''non-virtual function'''.
  
And we have '''a''' and '''b''' as integer variables.
+
And two '''integer variables''' as '''a''' and '''b'''.
  
 
|-
 
|-
Line 144: Line 138:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In function '''input''' we take the value of '''a''' and '''b''' as user input.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we access the '''input function.'''
 +
 
 +
In this we accept the numbers '''a''' and '''b'''.
  
 
|-
 
|-
Line 153: Line 149:
  
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we create a''' derived class '''as''' add.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is a''' derived class''' named''' add.'''
  
It inherits the''' base class abstractinterface.'''
+
It inherits the properties of the '''base class "abstractinterface".'''
  
 
|-
 
|-
Line 173: Line 169:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we have '''function numbers'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we override the '''function "numbers"'''.
  
In this we add the values''' of a '''and''' b.'''
+
In this we perform addition of two numbers '''a '''and''' b.'''
  
And print the sum.
+
And store the result in '''integer variable "sum".'''
 +
 
 +
Then we print the result.
  
 
|-
 
|-
Line 187: Line 185:
  
 
'''public: '''
 
'''public: '''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create another '''derived class''' '''sub.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have another '''derived class '''as '''sub.'''
  
This also inherits the''' base class abstractinterface.'''
+
This also inherits the''' base class "abstractinterface".'''
  
 
|-
 
|-
Line 205: Line 203:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''numbers''' function.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this, again, we override the '''function "numbers"'''.
  
This calculates the difference of two numbers.
+
And here we calculate the difference of two numbers '''a''' and '''b'''.
  
And prints the result.
+
Then we print the difference.
  
 
|-
 
|-
Line 225: Line 223:
  
 
'''obj1.numbers();'''
 
'''obj1.numbers();'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, '''obj1''' is the object of '''class add.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create an '''object''' of '''class "add"''' as '''obj1.'''
  
Then we call function''' input '''and''' numbers '''using object''' obj1.'''
+
Then we call both the '''functions "input" '''and''' "numbers" '''using the '''object obj1.'''
  
 
|-
 
|-
Line 237: Line 235:
  
 
'''obj2.numbers();'''
 
'''obj2.numbers();'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we create an object '''obj2''' of '''class sub.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we create another '''object''' of '''class "sub"''' as '''obj2.'''
  
And we call the''' input '''and''' numbers function '''using object''' obj2.'''
+
Again, we call the two "functions''' using the '''object obj2.'''
  
 
|-
 
|-
Line 245: Line 243:
  
 
'''return 0;'''
 
'''return 0;'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the '''return''' statement.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our '''return statement'''.
 
+
|-
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Click on '''Save'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now Click on '''Save'''
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us execute the program.
  
 
|-
 
|-
Line 259: Line 253:
  
 
'''Ctrl, Alt and T'''keys simultaneously
 
'''Ctrl, Alt and T'''keys simultaneously
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the '''terminal window''' by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 271: Line 265:
  
 
'''./abs'''
 
'''./abs'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type  
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile, type:
  
 
'''g++ abstract.cpp -o abs'''
 
'''g++ abstract.cpp -o abs'''
  
To execute type
+
'''Press Enter'''
 +
 
 +
Type:
  
 
'''./abs'''
 
'''./abs'''
 +
 +
'''Press Enter'''
  
 
|-
 
|-
Line 285: Line 283:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| It is displayed as  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| It is displayed as  
  
Enter the numbers  
+
'''Enter the numbers'''
  
I will enter as 12
+
I will enter as 9 and 4.
  
4
+
The output is displayed as '''Sum is 13'''
  
The output is displayed as '''16'''
+
Again we see '''Enter the numbers'''.
  
Enter the numbers
+
I will enter as '''8''' and '''3'''.
  
I will enter as
+
The output is displayed as '''Diff is 5'''
 
+
9
+
 
+
3
+
 
+
The output is displayed as '''6'''
+
  
 
|-
 
|-
Line 307: Line 299:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
  
Let us move back to slides.
+
Come back to our slides.
  
 
|-
 
|-
Line 313: Line 305:
  
 
Summary
 
Summary
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we have seen
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize.
 +
 
 +
In this tutorial we learnt,
  
 
'''Abstract class'''
 
'''Abstract class'''
  
eg. class abstractinterface
+
'''eg. class abstractinterface'''
  
 
'''Pure virtual function'''
 
'''Pure virtual function'''
  
eg. virtual void numbers()=0;
+
'''eg. virtual void numbers()=0;'''
  
 
|-
 
|-
Line 329: Line 323:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| As an assignment  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| As an assignment  
  
Create an abstract class '''student.'''
+
*Create an abstract class '''student.'''
  
Create a pure virtual function as '''Info'''  
+
*Create a pure virtual function as '''Info'''  
  
Accept the '''name''' and '''roll no''' of the student in the function.
+
*Accept the '''name''' and '''roll no''' of the student in the function.
  
Create two derived class '''marks''' and '''sports'''.
+
*Create two derived class '''marks''' and '''sports'''.
  
In marks accept marks of three subjects.
+
*In marks, accept marks of three subjects.
  
In sports enter marks scored in '''sports.'''
+
*In sports, enter marks scored in '''sports.'''
  
Calculate the '''total marks.'''
+
*Calculate the '''total marks.'''
  
Then create another derived class '''result.'''
+
*Then create another derived class '''result.'''
  
In this display the''' name, roll-no, total marks''' of the student.  
+
I*n this, display the''' name, roll-no, total marks''' of the student.  
  
 
|-
 
|-

Latest revision as of 23:40, 12 December 2013

Title of script: Abstract class and pure virtual function in C++

Author: Ashwini Patil

Keywords: abstract class, pure virtual function, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on abstract class and pure virtual function in C++.
Slide 2


In this tutorial we will learn,
  • Abstract Classes
  • Pure virtual function
  • We will do this through an example.
Slide 3 To record this tutorial, I am using
  • Ubuntu OS version 11.10
  • g++ compiler v. 4.6.1
Slide 4 Let us start with an introduction to abstract class.

Abstract class is used as a base class.

It contains at least one pure virtual function.

We cannot create an instance of abstract class.

Slide 5 Let us see pure virtual function.

A pure virtual function is a function with no body.

It is not defined in the base class.

It is declared as follows:

virtual void virtualfunname()=0;

A derived class must override the function.

Otherwise the compiler will give an error.

It is upto a derived class to implement the function.

Open abstract.cpp Let us look at an example

I have already typed the code on the editor.

Note that our filename is abstract.cpp

This example involves addition and subtraction of two numbers.

Point the cursor

abstract.cpp

Let us go through the code.
Highlight

#include<iostream>

This is our header file iostream.
Highlight

using namespace std;

Here are using std namespace.
Highlight

class abstractinterface


This is declaration for a class named abstractinterface.
Highlight

public:

Then we have public specifier.
Highlight

virtual void numbers()=0;

In this we have declared a virtual function named "numbers".

It is initialized to 0.

Highlight

void input();

int a, b;


Then we have a non-virtual function.

And two integer variables as a and b.

Highlight

void abstractinterface::input()

{

cout<< "Enter the numbers\n";

cin>>a>>b;

}

Here we access the input function.

In this we accept the numbers a and b.

Highlight

class add : public abstractinterface


This is a derived class named add.

It inherits the properties of the base class "abstractinterface".

Highlight

public:

void numbers()

{

int sum;

sum=a+b;

cout<<"sum is "<<sum<<"\n";

}

Here we override the function "numbers".

In this we perform addition of two numbers a and b.

And store the result in integer variable "sum".

Then we print the result.

Highlight

class sub : public abstractinterface

{

public:

Here we have another derived class as sub.

This also inherits the base class "abstractinterface".

Highlight

void numbers()

{

int diff;

diff=a-b;

cout<<"diff is "<<diff<<"\n";

}

In this, again, we override the function "numbers".

And here we calculate the difference of two numbers a and b.

Then we print the difference.

Highlight

int main()

This is our main function.
Highlight

add obj1;

obj1.input();

obj1.numbers();

Here we create an object of class "add" as obj1.

Then we call both the functions "input" and "numbers" using the object obj1.

Highlight

sub obj2;

obj2.input();

obj2.numbers();

Then we create another object of class "sub" as obj2.

Again, we call the two "functions using the object obj2.

Highlight

return 0;

And this is our return statement.
Now let us execute the program.
Open the terminal

Ctrl, Alt and Tkeys simultaneously

Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
Type

g++ abstract.cpp -o abs

To execute

Type

./abs

To compile, type:

g++ abstract.cpp -o abs

Press Enter

Type:

./abs

Press Enter

Highlight

Output

It is displayed as

Enter the numbers

I will enter as 9 and 4.

The output is displayed as Sum is 13

Again we see Enter the numbers.

I will enter as 8 and 3.

The output is displayed as Diff is 5

This brings us to the end of this tutorial.

Come back to our slides.

Slide 6

Summary

Let us summarize.

In this tutorial we learnt,

Abstract class

eg. class abstractinterface

Pure virtual function

eg. virtual void numbers()=0;

Slide 7

Assignment

As an assignment
  • Create an abstract class student.
  • Create a pure virtual function as Info
  • Accept the name and roll no of the student in the function.
  • Create two derived class marks and sports.
  • In marks, accept marks of three subjects.
  • In sports, enter marks scored in sports.
  • Calculate the total marks.
  • Then create another derived class result.

I*n this, display the name, roll-no, total marks of the student.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to,

contact@spoken-tutorial.org

Slide Number 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey