Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream.h>
#include <conio.h>

class A
{
      public:
             void show()
             {
                  cout<<"Class A's show()";
             }
};

class B : public A
{
      public:
             void show()
             {
                  cout<<"Class B's show()";
             }
};

int main()
{
    B o;
    o.show();
    getch();
    return 0;
}
Posted
Updated 3-Aug-11 0:49am
v3

Virtual functions won't help you on that (unless you call the base class method in the derived class one).
However, since B is A you may do:
C++
int main()
{
  B b;
  b.A::show();
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 18:43pm    
Correct, my 5. I added just my two cents to it, please see.
--SA
Adding to Cpallini solution,

you can do like this,
C#
class B : public A
{
      public:
             void show()
             {
                  cout<<"Class B's show()";
                  A::show();   //call base class show().
             }
};
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 18:42pm    
Correct, a 5. I added just my two cents to it, please see.
--SA
Just a note: you did not override show in derived class as both functions are not virtual, you have simply hidden the function of the base class; so the resolution shown by CPallini and venkatmakam is needed. Using virtual function means that the overridden version replaces the function entry in the vtable of derived class. I'm afraid to say: if you don't understand it, you have no idea of OOP — just yet. You really need to learn it.

—SA
 
Share this answer
 
Comments
CPallini 4-Aug-11 10:28am    
My 5.
You know, my resolution (or venkatmakam's) is needed even if he/she overrides show.
Sergey Alexandrovich Kryukov 4-Aug-11 15:36pm    
Thank you. I agree: you resolve inherited hidden function or inherited implementation of a virtual function in the same way, even though its run-time dispatching is quite different. Good point.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900