Advanced-C++/C2/Polymorphism/English

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

Title of script: polymorphism in C++

Author: Ashwini Patil

Keywords: polymorphism, virtual members, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Polymorphism in C++.
Slide 2


In this tutorial we will learn,

Polymorphism.

Virtual members.

We will do this through an example.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.10

gcc and g++ compiler v. 4.6.1

Slide 4 Let us start with the introduction to polymorphism.

Polymorphism is the ability to take different forms.

It is the mechanism to use a function with same name in different ways.

Slide 5 Let us see virtual functions.

Virtual function is the member function of a class.

It can be overriden in its derived class.

It is declared with virtual keyword.

Virtual function call is resolved at run-time.

Open the file virtual.cpp


Point the cursor to the file name

Now let us see an example on virtual functions.

I have already written the code.

Let us go through it.

Note that our filename is virtual.cpp

In this program:

We will calculate the area of a rectangle, parallelogram and traingle.

Highlight

#include <iostream>

using namespace std;

This is our header file as iostream.

Here we are using std namespace.

Highlight

class Parallelogram

protected:

int width, height, ar;


Then we have a class parallelogram

This is the base class.

In this we have declared integer variables as width, height and ar.

These are declared protected.

Highlight

public:

void set_values (int a, int b)


Then we have function set_values declared as public.

Here we have passed two arguments as a and b.

Then we access the protected members using the public members.

Highlight

virtual int area ()

{

ar=width*height;

cout<< "Area of parallelogram is "<< ar<<"\n";

}


This is our virtual function area.

Here we calculate the area of parallelogram.

Highlight

class Rectangle: public Parallelogram


Then we have class Rectangle as derived class.

It inherits the properties of base class parallelogram.

Highlight

int area ()

{

ar=width * height;

cout<< "Area of rectangle is "<<ar <<"\n";

}


Here we override the function area.

Then we calculate the area of the rectangle.

And print the value.

Highlight

class Triangle: public Parallelogram


Here we have another derived class as triangle.

This also inherits properties of the base class parallelogram.

Here again we override the function area.



Highlight

int area ()

{

ar=width * height / 2;

cout<< "Area of triangle is "<< ar<<"\n";

}


Then we calculate the area of the triangle.

And print the value.

Highlight

int main()

This is our main function.
Highlight

Parallelogram *parallel, p;


Here we create an object of class parallelogram as p.

We can see here pointer parallel.

Highlight

*parallel, p;

This is the pointer of class parallelogram.

This is called as Base pointer.

Pointer of base class can point to the object of the derived class.

Highlight

Rectangle rect;

Triangle trgl;


Here we create objects of class Rectangle and Triangle.
Highlight

parallel=&p;

parallel->set_values(3,2);


Here, Parallel is assigned to the address of p.

Then we pass arguments as 3 and 2.

Highlight

parallel->area();


Then we call function area.
Highlight

parallel=&rect;

parallel->set_values(4,5);

parallel->area();


Here, Parallel is assigned to the address of rect

rect is the object of class Rectangle.

Again we pass arguments as 4 and 5.

And we call the function area.

Highlight

parallel=&trgl;

parallel->set_values(6,5);

parallel->area();


And at last we assign Parallel is assigned to the address of Triangle

trgl.

This is the object of class Triangle.

Here we pass arguments as 6 and 5.

And call function area.

Highlight

return 0;

This is the return statement
Let us execute the program
Open the terminal

Ctrl, Alt and T keys simultaneously

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
Type

g++ virtual.cpp -o vir

To execute

Type

./vir

To compile the program type:

g++ virtual.cpp -o vir

Press Enter

Type:

./vir

Press Enter

Highlight

Output

We can see that, The output is displayed as:

Area of parallelogram is 6

Area of rectangle is 20

Area of triangle is 15

Switch back to the slides Come back to the slides.

Let us summarize.

Slide 7

Summary

In this tutorial, we have seen,

Polymorphism.

Virtual function.

Slide 6

Assignment

As an assignment

Calculate the perimeter of rectangle, square and triangle.

Create perimeter as a virtual function.

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 Indiaabout:startpage

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