Virtual Functions

This is a discussion on Virtual Functions within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; class A { public: virtual void disp() { cout<<"A"; } }; class B: public A ...

  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
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21