Question

4. Consider virtual functions. a) How does C++ allow some functions to be used in a program even before tions are defined? Describe the mechanism that make this possible the func and give examples b) How does C++ implement virtual functions? c) What are the two main disadvantages of using virtual functions?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1. How does c++ allows some functions to be used in a program even before the functions are defined ? Describe the mechanism that makes it possible and give examples.

Solution:
  
    The mechanism is called : runtime plymorphism or dynamic/late binding.
  
    In C++ we can achieve runtime polymorphism using virtual function.

    A virtual member function is a function which is declared within the base class
    but gets defined in child class.
    Functions are declared with virtual keywords in base class.
      
    By refering to a child class object using pointer of parent class we can call
    a virtual function which is declared in parent class but defined in child class.
  
    The resolving of function call is done at runtime.
    At runtime C++ compiler will find out which child class is being referred by a parent class pointer
    and will call the function which is defined in child class.

    e.g

    #include<iostream>
    using namespace std;
  
    // Parent class with a virtual function.
    class Parent
    {
        public:
            // Virtual funuction
            virtual void print ()
            {
                cout<< "print Parent class" <<endl;
            }
      
            void show ()
            {
                cout<< "show Parent class" <<endl;
            }
    };
  
    // Child class extending parent class and re-defining virtual function of parent class
    class Child:public Parent
    {
        public:
            //Virtual function declared in Parent class got redfined here
            void print ()
            {
                cout<< "print child class" <<endl;
            }
          
            // Non-virtual function of child class itself.
            void show ()
            {
                cout<< "show child class" <<endl;
            }
    };
  
    int main()
    {
        // Pointer of parent class.
        Parent *bptr;
      
        //Child class object
        Child d;

        //Parent class referring to child class object
        bptr = &d;
      
        /*
        * virtual function binded at runtime. Parent class pointer is pointing to child class object hence
        * print method from child class will be called.
        */

        bptr->print();

        // Non-virtual function, binded at compile time. Compiler will check whether show function is present in Parent class or not.
        // If not it will throw error.

        bptr->show();
    }
  
    Sample Output:

        print derived class
        show base class


Q2. How does c++ implements virtual function ?

Solution:

Virtual functions are implemented using virtual table in C++.
The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner.
Virtual table is also called as “vtable”, “virtual function table”, “virtual method table”.

First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is
given its own virtual table. This table is simply a static array that the compiler sets up at compile time.
A virtual table contains one entry for each virtual function that can be called by objects of the class.
Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is
set (automatically) when a class instance is created so that it points to the virtual table for that class.
Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references,
*__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer.
It also means that *__vptr is inherited by derived classes, which is important.

Q3. Disadvantages of virtual function ?

Solution:

    Slower:
    The function call takes are slower because the compiler doesnt know
    which function to call at compile time. It lookup every time in vtable for this.

    Memory:
    Additional 4 bytes for every instance of a class that has at least one virtual function.
    The code becomes also complex to understand sometimes.

   

Add a comment
Know the answer?
Add Answer to:
4. Consider virtual functions. a) How does C++ allow some functions to be used in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT