Difference between revisions of "Advanced-C++/C2/Polymorphism/English"
(Created page with ''''Title of script''': polymorphism in C++ '''Author: '''Ashwini Patil '''Keywords: polymorphism, virtual members, Video tutorial.''' {| style="border-spacing:0;" | style="b…') |
|||
Line 26: | Line 26: | ||
'''Virtual members.''' | '''Virtual members.''' | ||
− | We will do this | + | We will do this through an example. |
|- | |- | ||
Line 35: | Line 35: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using | ||
− | '''Ubuntu OS '''version 11. | + | '''Ubuntu OS '''version 11.10 |
'''gcc '''and '''g++ compiler '''v. 4.6.1 | '''gcc '''and '''g++ compiler '''v. 4.6.1 | ||
Line 43: | Line 43: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with the introduction to '''polymorphism.''' | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with the introduction to '''polymorphism.''' | ||
− | '''Polymorphism '''is the ability | + | '''Polymorphism '''is the ability to take different forms. |
It is the mechanism to use a '''function with same name''' in different ways. | It is the mechanism to use a '''function with same name''' in different ways. | ||
Line 49: | Line 49: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5 | | style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5 | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Virtual''' function is the member function of a class. | + | | style="background-color:#ffffff;border-top:none;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see virtual functions. |
+ | |||
+ | '''Virtual''' function is the member function of a class. | ||
It can be overriden in its '''derived class.''' | It can be overriden in its '''derived class.''' | ||
Line 56: | Line 58: | ||
'''Virtual function''' call is resolved at '''run-time.''' | '''Virtual function''' call is resolved at '''run-time.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 65: | Line 64: | ||
Point the cursor to the file name | Point the cursor to the file name | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| 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. | |
|- | |- | ||
Line 98: | Line 97: | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| 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. | ||
|- | |- | ||
Line 111: | Line 114: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''set_values''' | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''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. | |
|- | |- | ||
Line 130: | Line 135: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''This is our virtual function area.''' |
− | Here we calculate the area of | + | Here we calculate the area of parallelogram. |
|- | |- | ||
Line 141: | Line 146: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have class '''Rectangle''' as '''derived class.''' |
+ | |||
+ | It inherits the properties of base class parallelogram. | ||
|- | |- | ||
Line 158: | Line 165: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we override the function area. |
+ | |||
+ | Then we calculate the area of the rectangle. | ||
+ | |||
+ | And print the value. | ||
|- | |- | ||
Line 167: | Line 178: | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have another derived class as triangle. |
+ | |||
+ | This also inherits properties of the base class parallelogram. | ||
+ | |||
+ | Here again we override the function area. | ||
Line 187: | Line 202: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we calculate the area of the triangle. |
+ | |||
+ | And print the value. | ||
|- | |- | ||
Line 202: | Line 219: | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create an''' object '''of''' class parallelogram as p.''' |
+ | |||
+ | '''We can see here pointer parallel.''' | ||
|- | |- | ||
Line 208: | Line 227: | ||
'''<nowiki>*parallel, p;</nowiki>''' | '''<nowiki>*parallel, p;</nowiki>''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| ''' | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''This''' is the '''pointer''' of '''class''' '''parallelogram.''' |
This is called as''' Base pointer.''' | This is called as''' Base pointer.''' | ||
− | + | '''Pointer of base class can point to the object of the derived class.''' | |
− | + | ||
− | '''Pointer | + | |
|- | |- | ||
Line 225: | Line 242: | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create objects of class''' Rectangle and Triangle.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 238: | Line 253: | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Parallel is assigned to the address of p.''' | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, '''Parallel is assigned to the address of p.''' |
− | + | Then we pass''' arguments '''as''' 3 '''and''' 2.''' | |
|- | |- | ||
Line 262: | Line 277: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Parallel is assigned to the address of rect''' | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, '''Parallel is assigned to the address of rect''' |
− | + | '''rect is the object of class Rectangle.''' | |
− | Again we call the''' function area.''' | + | Again we pass '''arguments '''as''' 4 '''and''' 5.''' |
+ | |||
+ | And we call the''' function area.''' | ||
|- | |- | ||
Line 279: | Line 296: | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| ''' | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''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.''' | ||
|- | |- | ||
Line 290: | Line 311: | ||
'''return 0;''' | '''return 0;''' | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the return statement | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the return statement | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program |
|- | |- | ||
Line 303: | Line 320: | ||
'''Ctrl, Alt and T '''keys simultaneously | '''Ctrl, Alt and T '''keys simultaneously | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard. |
|- | |- | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type | ||
− | '''g++ virtual.cpp -o | + | '''g++ virtual.cpp -o vir''' |
To execute | To execute | ||
Line 314: | Line 331: | ||
Type | Type | ||
− | '''./ | + | '''./vir''' |
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type: |
− | '''g++ virtual.cpp -o | + | '''g++ virtual.cpp -o vir''' |
− | + | '''Press Enter''' | |
− | Type | + | Type: |
+ | |||
+ | '''./vir ''' | ||
− | ''' | + | '''Press Enter''' |
|- | |- | ||
Line 329: | Line 348: | ||
'''Output''' | '''Output''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We can see that, The output is displayed as: |
'''Area of parallelogram is 6 ''' | '''Area of parallelogram is 6 ''' | ||
Line 339: | Line 358: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch back to the slides | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch back to the slides | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Come back to the slides. |
− | Let us | + | Let us summarize. |
|- | |- |
Latest revision as of 00:40, 10 May 2013
Title of script: polymorphism in C++
Author: Ashwini Patil
Keywords: polymorphism, virtual members, Video tutorial.
|
|
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
|
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=▭ 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
|
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. |