Difference between revisions of "Advanced-C++/C2/Abstract-Class/English"
(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.''' {| …') |
Nancyvarkey (Talk | contribs) |
||
(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 | + | |
|- | |- | ||
| 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. | + | *'''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;"| ''' | + | | 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;"| | + | | 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. | ||
− | + | 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. | ||
− | |||
− | |||
|- | |- | ||
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;"| | + | | 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 | + | |
|- | |- | ||
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;"| | + | | 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 ''' | + | | 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".''' |
− | + | It is initialized to '''0.''' | |
− | + | ||
− | It is | + | |
|- | |- | ||
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 ''' | + | | 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 | + | 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;"| | + | | 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;"| | + | | 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;"| | + | | 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 | + | In this we perform addition of two numbers '''a '''and''' b.''' |
− | And | + | 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 | + | | 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;"| | + | | 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"'''. |
− | + | And here we calculate the difference of two numbers '''a''' and '''b'''. | |
− | + | 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 | + | | 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 | + | 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;"| | + | | 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.''' |
− | + | 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;"| | + | | 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;"| | | 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;"| | + | | 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 | + | | 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''' | ||
− | + | '''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 | + | 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''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | The output is displayed as ''' | + | |
|- | |- | ||
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. | ||
− | + | 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 | + | | 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.''' |
− | + | 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.
|
|
Slide 1 | Welcome to the spoken tutorial on abstract class and pure virtual function in C++. |
Slide 2
|
In this tutorial we will learn,
|
Slide 3 | To record this tutorial, I am using
|
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
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
|
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. |