I have two classes, and the methods in them are shown below;
|----AVL----| |-----RB------| | | | | | | | | | - insert | | -balance | | | | | | - balance | | | | | | | |-----------| |-------------|
inside “insert” method of AVL, it calls “balance”.
RB inherits AVL, so I can use insert method of AVL. Now when I call RB::insert(), it calls AVL::insert() & then AVL::balance(), but I want it to call RB::balance() from AVL::insert(), when a RB object calls “insert”.
This is a classic case for virtual methods: make
AVL.balancevirtualand override it inRB. The correct implementation will then be called depending on what type of object callsbalance— it doesn’t matter that the code that callsbalancewill be written as part ofAVL.