Difference between revisions of "Interview Preparation C++ Virtual, Polymorphism and Abstract class"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Created page with "== Basics == To learn about virtual, let's learn about the basics of inheritance. To properly learn about virtual, and polymorphism, you need to copy and paste the code in a ...")
 
(Pure Virtual)
 
(9 intermediate revisions by one other user not shown)
Line 2: Line 2:
 
To learn about virtual, let's learn about the basics of inheritance.  To properly learn about virtual, and polymorphism, you need to copy and paste the code in a C/C++ compiler and run it each step of the way.
 
To learn about virtual, let's learn about the basics of inheritance.  To properly learn about virtual, and polymorphism, you need to copy and paste the code in a C/C++ compiler and run it each step of the way.
  
 +
Here is the video you should watch AFTER you read through the tutorial :
 +
*  '''VIDEO : [http://www.youtube.com/watch?v=Mslceq6P_9Y Polymorphism C++]'''
 +
 +
<BR/>
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
/* Base class */
 
/* Base class */
 
class printer {
 
class printer {
 
     public :
 
     public :
         void print_pixel() {
+
         void pixel()
             puts("printer pixel");
+
        {
 +
             puts("printer pixel()");
 +
        }
 +
        void draw_image() {
 +
            // Some complicated algorithm that uses our method ()
 +
            printf("draw_image() is using : ");
 +
            pixel();
 
         }
 
         }
 
};
 
};
Line 14: Line 24:
 
class model_a : public printer {
 
class model_a : public printer {
 
     public :
 
     public :
         void print_pixel() {
+
         void pixel() {
             puts("model-a pixel");
+
             puts("model-a pixel (very sharp)");
 
         }
 
         }
 
};
 
};
  
void main(void)
+
int main(void)
 
{
 
{
 
     /* Let's go over simple stuff */
 
     /* Let's go over simple stuff */
 
     model_a a;
 
     model_a a;
     a.print_pixel(); /* Prints "printer pixel" */
+
     a.pixel(); /* Prints "model-a pixel" */
  
 
     printer p;
 
     printer p;
     p.print_pixel(); /* Prints "model-a pixel" */
+
     p.pixel(); /* Prints "printer pixel" */
  
 
     /* Let's use a pointer of type "printer" */
 
     /* Let's use a pointer of type "printer" */
 
     printer *ptr = NULL;
 
     printer *ptr = NULL;
 
     ptr = &a;
 
     ptr = &a;
     ptr->print_pixel(); /* Prints "printer pixel" */
+
     ptr->pixel(); /* Prints "printer pixel" */
 
     ptr = &p;
 
     ptr = &p;
     ptr->print_pixel(); /* Prints "printer pixel" */
+
     ptr->pixel(); /* Prints "printer pixel" */
  
     /* In both cases above, we will see "printer pixel" being printed
+
     return 0;
    * because the type of the pointer is "printer", so it will always
 
    * call the printer's print_pixel() method
 
    */
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
<BR/>
 
<BR/>
 +
 
== Virtual ==
 
== Virtual ==
 
Now, let's modify the function of the '''<code>printer</code>''' :
 
Now, let's modify the function of the '''<code>printer</code>''' :
 
<code>
 
<code>
Change :        void print_pixel() { puts("printer pixel"); }
+
<BR/>Change :        void print_pixel() { puts("printer pixel"); }
To    : virtual void print_pixel() { puts("printer pixel"); }
+
<BR/>To    : virtual void print_pixel() { puts("printer pixel"); }
 
</code>
 
</code>
  
Line 71: Line 79:
  
 
<BR/>
 
<BR/>
 +
 
== Pure Virtual ==
 
== Pure Virtual ==
 
Now, let's modify the function of the '''<code>printer</code>''' :
 
Now, let's modify the function of the '''<code>printer</code>''' :
 
<code>
 
<code>
Change :        void print_pixel() { puts("printer pixel"); }
+
<BR/>Change :        void print_pixel() { puts("printer pixel"); }
To    : virtual void print_pixel() = 0;
+
<BR/>  To    : virtual void print_pixel() = 0;
 
</code>
 
</code>
 +
 
By using the '''<code> = 0;</code>''' at the end, we make this function '''pure virtual''' which earns the base class the names of :
 
By using the '''<code> = 0;</code>''' at the end, we make this function '''pure virtual''' which earns the base class the names of :
 
*  '''Abstract class'''
 
*  '''Abstract class'''
Line 84: Line 94:
 
*  I am an incomplete, abstract class
 
*  I am an incomplete, abstract class
 
*  I do not know how to print a pixel
 
*  I do not know how to print a pixel
*  I require the parent class to provide the method otherwise it will be compiler error.
+
*  I require the derived class to provide the method otherwise it will be compiler error.
  
 
TODO - need to finish the rest
 
TODO - need to finish the rest

Latest revision as of 01:26, 22 May 2016

Basics

To learn about virtual, let's learn about the basics of inheritance. To properly learn about virtual, and polymorphism, you need to copy and paste the code in a C/C++ compiler and run it each step of the way.

Here is the video you should watch AFTER you read through the tutorial :


/* Base class */
class printer {
    public :
        void pixel()
        {
            puts("printer pixel()");
        }
        void draw_image() {
            // Some complicated algorithm that uses our method ()
            printf("draw_image() is using : ");
            pixel();
        }
};

/* A printer model class inheriting base class */
class model_a : public printer {
    public :
        void pixel() {
            puts("model-a pixel (very sharp)");
        }
};

int main(void)
{
    /* Let's go over simple stuff */
    model_a a;
    a.pixel(); /* Prints "model-a pixel" */

    printer p;
    p.pixel(); /* Prints "printer pixel" */

    /* Let's use a pointer of type "printer" */
    printer *ptr = NULL;
    ptr = &a;
    ptr->pixel(); /* Prints "printer pixel" */
    ptr = &p;
    ptr->pixel(); /* Prints "printer pixel" */

    return 0;
}


Virtual

Now, let's modify the function of the printer :
Change : void print_pixel() { puts("printer pixel"); }
To  : virtual void print_pixel() { puts("printer pixel"); }

Now, if you run the program again, the following will occur :

void main(void)
{
    /* Let's use a pointer of type "printer" */
    printer *ptr = NULL;
    ptr = &a;
    ptr->print_pixel(); /* Prints "model-a pixel" */
    ptr = &p;
    ptr->print_pixel(); /* Prints "printer pixel" */

    /* Now, model-a's pixel method will be called because the "virtual" is
     * basically an instruction that says :
     * "Here is the function, but if a parent has the same function defined,
     *  then I will call the parent's method instead of the base class method"
     */
}

TODO - need to finish the rest


Pure Virtual

Now, let's modify the function of the printer :
Change : void print_pixel() { puts("printer pixel"); }
To  : virtual void print_pixel() = 0;

By using the = 0; at the end, we make this function pure virtual which earns the base class the names of :

  • Abstract class
  • Interface class

In plain English, this is what the base class is dictating :

  • I am an incomplete, abstract class
  • I do not know how to print a pixel
  • I require the derived class to provide the method otherwise it will be compiler error.

TODO - need to finish the rest