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

From Embedded Systems Learning Academy
Jump to: navigation, search
(Pure Virtual)
(Basics)
Line 6: Line 6:
 
class printer {
 
class printer {
 
     public :
 
     public :
         void print_pixel() {
+
        // Pure virtual
             puts("printer pixel");
+
        // Abstract class, or an interface
 +
         void pixel()
 +
        {
 +
             puts("printer pixel()");
 +
        }
 +
        void draw_image() {
 +
            // Some complicated algorithm that uses our method ()
 +
            printf("draw_image() is using : ");
 +
            pixel();
 
         }
 
         }
 
};
 
};
Line 14: Line 22:
 
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)
 
{
 
{
 +
    setvbuf(stdout, 0, _IONBF, 0);
 +
    setvbuf(stdin, 0, _IONBF, 0);
 +
 
     /* 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 "printer pixel" */
  
 
     printer p;
 
     printer p;
     p.print_pixel(); /* Prints "model-a pixel" */
+
     p.pixel(); /* Prints "model-a 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>''' :

Revision as of 20:33, 14 July 2013

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.

/* Base class */
class printer {
    public :
        // Pure virtual
        // Abstract class, or an interface
        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)
{
    setvbuf(stdout, 0, _IONBF, 0);
    setvbuf(stdin, 0, _IONBF, 0);

    /* Let's go over simple stuff */
    model_a a;
    a.pixel(); /* Prints "printer pixel" */

    printer p;
    p.pixel(); /* Prints "model-a 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 parent class to provide the method otherwise it will be compiler error.

TODO - need to finish the rest