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
    23,820
    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
    23,820
    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, 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