• Advertisement

Archived

This topic is now archived and is closed to further replies.

Hide or overwrite

This topic is 5995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What''s the difference between overwriting a base-class method(in a derived class), and hiding it? Elall "It''''s all about reading between the lines"

Share this post


Link to post
Share on other sites
Advertisement
In C++ you declare that you want to override a method with the 'virtual' keyword. That means that, instead of calling the method that is in the parent class, all methods must call the method appropriate to the class of the object.

Here is an example :

    

class Base
{
void Hidden()
{
cout << "Base::Hidden." << endl;
}
virtual void Overriden()
{
cout << "Base::Overriden." << endl;
}
void DoStuff()
{
Hidden();
Overriden();
}
}

class Child : public Base
{
void Hidden()
{
cout << "Child::Hidden." << endl;
}

void Overriden() // already virtual from Base

{
cout << "Child::Overriden." << endl;
}

void DoMoreStuff()
{
Hidden();
Overriden();
}
}

.
.
.

Base B;
Child C;

B.DoStuff()
// prints out

// Base::Hidden

// Base::Overriden


C.DoStuff()
// prints out

// Base::Hidden

// Child::Overriden


C.DoMoreStuff()
// prints out

// Child::Hidden

// Child::Overriden



Edit: Fixed the typo the next post points at.

Edited by - Fruny on November 22, 2001 3:42:52 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Typo in your Child class methods (forgot to change "Base" to "Child")

Share this post


Link to post
Share on other sites

  • Advertisement