Method Override

This is a discussion on Method Override within the C++ Programming forums, part of the General Programming Boards category; is this method override? , Code: #include<iostream> using namespace std; class A { protected: void show() { cout<<"A"; } }; ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  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, 03:22 PM

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