Advanced-C++/C2/Abstract-Class/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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