Thread: Method Override

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

    Method Override

    is this method override? ,

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
       protected:
               void show()
               {
                    cout<<"A";     
               }
          
    };
    
    class B:public A
    {
       public:
              void show()
              {
                   cout<<"B"; 
                   A::show();    
              }   
    };
    
    int main() 
    { 
        A *a = new B();
        a->show();                          // tries to call A::show() 
        getchar();
        return 0;
    }

    a->show() is calling A::show() rather than B::show() , so i suspect this isnt method override

    however if it isnt, this means B has not overriden A's show(), and hence B has 2 show() methods with same signature name and return type, but different access specifiers

    so i tried this:-

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {           
       protected:
               void show()
               {
                    cout<<"A";     
               }
               
       public:
               void show()
               {
                    cout<<"B";     
               }      
    };
    
    
    int main() 
    { 
        A *a = new A();
        a->show(); 
        getchar();
        return 0;
    }
    now compiler reports error in overloading show() because both have same signature, that means 2 same functions cannot even exist in different access specifiers

    both conclusions are contradicting each other

    which is correct?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vb.bajpai View Post
    however if it isnt, this means B has not overriden A's show(), and hence B has 2 show() methods with same signature name and return type, but different access specifiers
    Untrue. The names are in different namespaces. One function is A::show, the other is B::show. There's no reason these two distinct names can't have different accessibilities.

    both conclusions are contradicting each other
    No, because A::show and B::show are different things. So both observed behaviors are correct.

    And you aren't overriding the method, because it isn't virtual. You're just masking the parent's definition when in the scope of class B.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    in example 2 you are overloading the function show() within the same namespace (a class in this case). The problem is that you have the same function signature for both functions (specifically void A::*() )

    In example one, you are shadowing the function show(). if you add the keyword "virtual" to the base function it will work as you expect.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you're calling A::show() in B::show(). this has nothing to do with overriding a method.

    the sequence is

    Code:
        A *a = new B(); // creates a B object with an A interface
        a->show(); // lookup virtual function table and call B::show()
            == in B::show()========
            cout<<"B";  // prints B
            A::show(); // directly calls A::show(). no vtable lookup
                == in A::show() ========
                cout << "A"; // prints A
    Last edited by ChaosEngine; 06-21-2007 at 04:07 PM. Reason: fixed case
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM