answersLogoWhite

0


Best Answer

To override a base class method you simply need to declare the base class method as being virtual. As well as creating a v-table, this also gives a visual hint to other developers that you expect the function to be overridden. The v-table ensures that all calls to the base class method are routed to the derived class method, thus ensuring objects behave polymorphically, according to their actual type, and not what we're actually pointing at.

Consider the following example:

#include <iostream>

class base

{

public:

virtual ~base();

virtual void PrintMe() const { std::cout << "I am a base class!" << std::endl; }

};

class derived: public base

{

public:

void PrintMe() const { std::cout << "I am a derived class!" << std::endl; }

};

int main()

{

base b;

derived d;

base* pb = &d;

b.PrintMe();

d.PrintMe();

pb->PrintMe();

return( 0 );

}

Output:

I am a base class!

I am a derived class!

I am a derived class!

Note that although pb points to the base class instance of d, it still knows that it really is a derived class, as can be seen from the third line of output.

Now try removing the virtual keyword from the base class method. The output will change as follows:

Output:

I am a base class!

I am a derived class!

I am a base class!

Now your derived class thinks it is a base class. This is because the v-table no longer has no entry for that method, and therefore the call cannot be routed to the overridden derived class method. The base class method is called because that's what we're actually pointing at and the object no longer behaves polymorphically according to its actual type.

Note also that if any method is declared virtual in a class, the class constructor must also be declared virtual. If you fail to do this, your classes will not be destroyed properly. The virtual keyword ensures that the most-derived class is always destroyed first, before working up the hierarchy of destructors to eventually destroy the least-derived class, the base class itself.

Consider the following example without a virtual destructor:

#include <iostream>

class base

{

public:

base(){ std::cout << "Base class created" << std::endl; }

~base(){ std::cout << "Base class destroyed" << std::endl; }

};

class derived: public base

{

public:

derived(){ std::cout << "Derived class created" << std::endl; }

~derived(){ std::cout << "Derived class destroyed" << std::endl; }

};

int main()

{

derived* d = new derived();

base* b = d;

delete( b );

return( 0 );

}

Output:

Base class created

Derived class created

Base class destroyed

As you can see, the derived class was created but was not destroyed. We've created a memory leak: that memory cannot be recovered until the program ends.

Now add the virtual keyword to the base class destructor:

#include <iostream>

class base

{

public:

base(){ std::cout << "Base class created" << std::endl; }

virtual ~base(){ std::cout << "Base class destroyed" << std::endl; }

};

class derived: public base

{

public:

derived(){ std::cout << "Derived class created" << std::endl; }

~derived(){ std::cout << "Derived class destroyed" << std::endl; }

};

int main()

{

derived* d = new derived();

base* b = d;

delete( b );

return( 0 );

}

Output:

Base class created

Derived class created

Derived class destroyed

Base class destroyed

Now we have the expected behaviour and have resolved the memory leak. Remember, if ANY method of a class is declared virtual, the destructor must also be declared virtual.

Note that although derived classes need not use the virtual keyword in front of overrides (it is implied by the base class), there is no harm in explicitly declaring them as such, if only to give a visual hint that the methods are expected to be overridden by derivatives of the derivative (multi-level inheritance).

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you can override base class member in derived classexplain with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What are Public and Private inheritance in c plus plus?

Public, protected and private inheritance determine how the public and protected base class members are inherited by the derived class. Private members are never inherited and are therefore unaffected by the type of inheritance (they remain private to the base class). The following table summarises how inheritance affects accessibility of base class members with respect to the derived class: public inheritanceprotected inheritanceprivate inheritancepublic member of base classpublic member of derived classprotected member of derived classprivate member of derived classprotected member of base classprotected member of derived classprotected member of derived classprivate member of derived classprivate member of base classprivate member of base classprivate member of base classprivate member of base class Note that accessibility to individual public and protected base class members can be overridden within the derived class, regardless of the type of inheritance specified.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


Can static member function be overloaded?

Yes. Any function can be overloaded. However you cannot override a static member function. Only instance members can be overridden.


What is an aporphine?

An aporphine is a member of a class of quinoline alkaloids from which apomorphine can be derived.


What is a member?

Someone who is a part of a group or bunch, for example, I am a member of my family.For males, a member may be a penis, for example, He grasped his flaccid member.


How can you make grains in a sentence?

Edible wheat is derived from the grains of a member of the grass family.


What fraction of representatives and senators must agree in order to override the presidents vetoes?

2/3 of the Reps and Senators voting much agree in order to override the President's veto. In numbers this means 290 representatives and 67 senators if every member votes .


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What is an amphotericin?

An amphotericin is a member of a group of amphoretic, antibiotic, and antifungal agents, derived from Streptomyces bacteria.


Is Example a member of the Illuminati?

you cant be a member of something that doesnt exist


Does Derive class inherits?

The derived class inherits all members and member functions of a base class.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.