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.''' {| …') |
|||
Line 26: | Line 26: | ||
Pure virtual function | Pure virtual function | ||
− | We will do this | + | We will do this through an example. |
|- | |- | ||
Line 35: | Line 35: | ||
| 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. | + | '''Ubuntu OS '''version 11.10 |
'''g++ compiler '''v. 4.6.1 | '''g++ compiler '''v. 4.6.1 | ||
Line 41: | Line 41: | ||
|- | |- | ||
| 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 an abstract class. |
− | It contains one '''pure virtual function.''' | + | '''Abstract class '''is used as a base class. |
+ | |||
+ | It contains atleast one '''pure virtual function.''' | ||
We cannot create an instance of '''abstract class.''' | We cannot create an instance of '''abstract class.''' | ||
Line 49: | Line 51: | ||
|- | |- | ||
| 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. |
+ | |||
+ | A '''pure virtual function''' is a function with no body. | ||
It is not defined in the base class. | It is not defined in the base class. | ||
Line 69: | Line 73: | ||
| 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 | | 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 87: | ||
'''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 99: | ||
'''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 113: | Line 120: | ||
'''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 i'''s initialized to '''0.''' | |
− | + | ||
− | It | + | |
|- | |- | ||
Line 128: | Line 133: | ||
− | | 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 149: | ||
'''}''' | '''}''' | ||
− | | 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 163: | ||
− | | 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 183: | ||
'''}''' | '''}''' | ||
− | | 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 199: | ||
'''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 217: | ||
'''}''' | '''}''' | ||
− | | 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 237: | ||
'''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 249: | ||
'''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 257: | ||
'''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 267: | ||
'''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 | + | | 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 on your keyboard. | ||
|- | |- | ||
Line 271: | Line 281: | ||
'''./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 the program type: |
'''g++ abstract.cpp -o abs''' | '''g++ abstract.cpp -o abs''' | ||
− | + | '''Press Enter''' | |
+ | |||
+ | Type: | ||
'''./abs''' | '''./abs''' | ||
+ | |||
+ | '''Press Enter''' | ||
|- | |- | ||
Line 287: | Line 301: | ||
Enter the numbers | Enter the numbers | ||
− | I will enter as | + | I will enter as: |
+ | |||
+ | 9 and | ||
4 | 4 | ||
− | The output is displayed as ''' | + | The output is displayed as |
+ | |||
+ | '''Sum is 13''' | ||
+ | |||
+ | Again we see | ||
Enter the numbers | Enter the numbers | ||
Line 297: | Line 317: | ||
I will enter as | I will enter as | ||
− | + | '''8''' and | |
− | 3 | + | '''3''' |
− | The output is displayed as ''' | + | The output is displayed as |
+ | |||
+ | '''Diff is 5''' | ||
|- | |- | ||
Line 307: | Line 329: | ||
| 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 335: | ||
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''' |
Revision as of 10:12, 10 May 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,
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 an abstract class.
Abstract class is used as a base class. It contains atleast 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 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 by pressing Ctrl, Alt and T keys
Simultaneously on your keyboard. |
Type
g++ abstract.cpp -o abs To execute Type ./abs |
To compile the program 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. In 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. |