Thread: Virtual Functions

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Virtual Functions

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
      public:
             virtual void disp()
             {
                     cout<<"A";        
             }    
    };
    
    class B: public A
    {
      public:
             void disp()
             {
                     cout<<"B";        
             }      
    };
    
    class C: public B
    {
      public:
             virtual void disp()
             {
                     cout<<"C";        
             }      
    };
    
    int main()
    {
        B *a = new C();
        a->disp();
        getchar();
        return 0;                    
    }
    here B::disp() is not virtual , so i suggest dont use dynamic binding with B class pointer containing address of any derived object.

    but still output is C

    i guess it has something to do with virtual A::disp() ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A::disp() is virtual. B is a child of A. Therefore B::disp() is virtual too, even though it is not explicitly stated as such.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    ok thanx

    one more doubt -

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
                          
    };
    
    class B: public A
    {
      public:
            void disp()
            {
                     cout<<"B";        
            }      
    };
    
    
    int main()
    {
        A *a = new B();
        ((B *)a)->disp();            // Static Binding ??
        getchar();
        return 0;                    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not entirely sure on this, but I would say that it is static binding, since for such a cast to be valid, a has to point to a B object, so the type of the underlying object is known at compile time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 04:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM