Advanced-C++/C2/Abstract-Class/English
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 with the help of an example. |
Slide 3
|
To record this tutorial, I am using
Ubuntu OS version 11.04 g++ compiler v. 4.6.1 |
Slide 4 | Abstract class is used as a base class.
It contains one pure virtual function. We cannot create an instance of abstract class. |
Slide 5 | 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
This example involves addition and subtraction of two numbers. I have already typed the code on the editor. |
Point the cursor
abstract.cpp |
I have saved the file with the name abstract.cpp
Let me explain the code now. |
Highlight
#include<iostream> |
This is our header file iostream. |
Highlight
using namespace std; |
This is the using statement. |
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 pure virtual function as numbers initialized to 0.
This is the declaration for virtual function. It is defined when it is used. |
Highlight
void input(); int a, b;
|
Then a non-virtual function input is declared.
And we have a and b as integer variables. |
Highlight
void abstractinterface::input() { cout<< "Enter the numbers\n"; cin>>a>>b; } |
In function input we take the value of a and b as user input. |
Highlight
class add : public abstractinterface
|
Now we create a derived class as add.
It inherits the base class abstractinterface. |
Highlight
public: void numbers() { int sum; sum=a+b; cout<<"sum is "<<sum<<"\n"; } |
Now we have function numbers
In this we add the values of a and b. And print the sum. |
Highlight
class sub : public abstractinterface { public: |
Here we create another derived class sub.
This also inherits the base class abstractinterface. |
Highlight
void numbers() { int diff; diff=a-b; cout<<"diff is "<<diff<<"\n"; } |
Then we have numbers function.
This calculates the difference of two numbers. And prints the result. |
Highlight
int main() |
This is our main function. |
Highlight
add obj1; obj1.input(); obj1.numbers(); |
Here, obj1 is the object of class add.
Then we call function input and numbers using object obj1. |
Highlight
sub obj2; obj2.input(); obj2.numbers(); |
Now we create an object obj2 of class sub.
And we call the input and numbers function using object obj2. |
Highlight
return 0; |
This is the return statement. |
Click on Save | Now Click on Save |
Let us execute | |
Open the terminal
Ctrl, Alt and Tkeys simultaneously |
Open the terminal by pressing Ctrl, Alt and T keys simultaneously |
Type
g++ abstract.cpp -o abs To execute Type ./abs |
To compile the program type
g++ abstract.cpp -o abs To execute type ./abs |
Highlight
Output |
It is displayed as
Enter the numbers I will enter as 12 4 The output is displayed as 16 Enter the numbers I will enter as 9 3 The output is displayed as 6 |
This brings us to the end of this tutorial.
Let us move back to slides. | |
Slide 6
Summary |
In this tutorial we have seen
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. |