Advanced-C++/C2/Polymorphism/English

From Script | Spoken-Tutorial
Revision as of 12:47, 6 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 with the help of examples.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.04

gcc and g++ compiler v. 4.6.1

Slide 4 Let us start with the introduction to polymorphism.

Polymorphism is the ability of an entity to take different forms.

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

Slide 5 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

Let us see an example on virtual functions.

In this program,

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

Using virtual function.

I have already typed the program on the editor.

Lets go through it.

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;


This is class parallelogram the base class.

Then we have width, height and ar as variables.

Highlight

public:

void set_values (int a, int b)


set_values is the public function.

We have passed two arguments as int a and int b.

Highlight

virtual int area ()

{

ar=width*height;

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

}


Then we have virtual function area.

Here we calculate the area of the parallelogram.

Highlight

class Rectangle: public Parallelogram


Here Rectangle is the derived class.
Highlight

int area ()

{

ar=width * height;

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

}


In this we calculate the area of the rectangle.
Highlight

class Triangle: public Parallelogram


Now we have triangle as derived class.



Highlight

int area ()

{

ar=width * height / 2;

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

}


Here we calculate the area of the triangle.
Highlight

int main()

This is our main function.
Highlight

Parallelogram *parallel, p;


Then we create an object of class parallelogram.
Highlight

*parallel, p;

*parallel is the pointer to class parallelogram.

This is called as Base pointer.

p is the object of class parallelogram.

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

Highlight

Rectangle rect;

Triangle trgl;


Rect is the object of class Rectangle.

And trgl is the object of class Triangle.

Highlight

parallel=&p;

parallel->set_values(3,2);


Parallel is assigned to the address of p.

Here 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();


Parallel is assigned to the address of rect

We pass arguments as 4 and 5.

Again we call the function area.

Highlight

parallel=&trgl;

parallel->set_values(6,5);

parallel->area();


Now, Parallel is assigned to the address of trgl

We pass arguments as 6 and 5.

Then we call area function.

Highlight

return 0;

This is the return statement
Click on save Now Click on Save
Let us execute
Open the terminal

Ctrl, Alt and T keys simultaneously

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

g++ virtual.cpp -o virtual

To execute

Type

./virtual

To compile the program type

g++ virtual.cpp -o virtual

To execute

Type

./virtual

Highlight

Output

Here we see,

Area of parallelogram is 6

Area of rectangle is 20

Area of triangle is 15

Switch back to the slides This brings us to the end of this tutorial.

Let us come back to the slides.

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