Welcome to the spoken-tutorial on abstract class and pure virtual function in C++.
In this tutorial, we will learn:
Abstract Classes, Pure virtual function.
We will do this through an example.
To record this tutorial, I am using:
Ubuntu OS version 11.10
g++ compiler version 4.6.1
Let us start with an introduction to abstract class.
Abstract class is always a base class.
It contains at least one pure virtual function.
We cannot create an instance of abstract class.
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:
virtual void virtualfunname()=0;
A derived class must override the function.
Otherwise the compiler will give an error.
It is up to a derived class to implement the function.
Let us look at an example.
I have already typed the code on the editor.
I will open it.
Note that our file name is abstract.cpp.
This example involves addition and subtraction of two numbers.
Let us go through the code.
This is our header file as iostream.
Here we are using the std namespace.
This is declaration for a class named abstractinterface.
Then we have public specifier.
In this, we have declared a virtual function named numbers().
It is initialized to 0.
Then we have a non-virtual function.
And two integer variables as 'a' and 'b'.
Here, we access the input function.
In this, we accept the numbers 'a' and 'b'.
This is a derived class named add.
It inherits the properties of the base class "abstractinterface".
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.
Here we have another derived class as sub.
This also inherits the base class "abstractinterface".
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.
This is our main() function.
Here we create an object of class "add" as obj1.
Then we call both the functions input() and numbers() using the object obj1.
Then we create another object of class "sub" as obj2.
Again, we call the two functions using the object obj2.
And this is our 'return' statement.
Now let us execute the program.
Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
To compile, type: g++ space abstract dot cpp space hyphen o space abs
press Enter.Type: dot slash abs
press Enter.
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.
Let us summarize.
In this tutorial, we learnt:Abstract class e.g. class abstractinterface.
Pure virtual function e.g. virtual void numbers()=0;
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 classes 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 as result.
In this, display the name, roll-no and total marks of the student.
Watch the video available at the link shown below.
It summarizes the Spoken-Tutorial project.
If you do not have good bandwidth, you can download and watch it.
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
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 the link shown below: http://spoken-tutorial.org/NMEICT-Intro
This is Ashwini Patil from IIT Bombay, signing off.Thank You for watching.