Why would you like to declare a function non-virtual inside a inheritance hierarchy using more than two levels? Can someone please giv me a good example?

I understand exactly what the difference is and what goes on, however i can't really grasp why so when for you to do it.

From the semantic perspective: To ensure that it cannot be overriden.

From the practical perspective: If you will find no virtual functions, then there's no requirement for a vptr, so each instance will occupy slightly less memory. Also, as @Billy states inside a comment, this means the member function will not be known as not directly.

The primary reason behind this really is that after you are making a technique virtual, you're telling subclasses "proceed and modify behavior here." If you do this, you limit ability for that base class to become transformed. Essentially, it is then simple for subclasses to finish up based on implementation particulars from the base class, so when one attempts to alter the base class, break an unknowably great deal of code.

Classes have to be made to be inherited from, and do something to future proof themselves within their design. You won't want to take such effort and consideration against techniques that there's no variation between subclasses.

The initial question is the reason why you'd want an inheritance hierarchy of a lot more than two levels? The commonest inheritance hierarchy would easily be an abstract base class along with a single degree of classes which be a consequence of it. (A minimum of when derivation has been accustomed to implement OO inheritance&mdashit's not rare for C++ derivation for use for other activities.)

You will find two frequent designs in which a much deeper inheritance hierarchy does occur. The very first happens whenever you extend an interface used, there isn't much distinction between this and drawing from one abstract base, and it might be a mistake to be a consequence of the course supplying the implementation anyway. The second reason is in which the implementation is easy to customize while using template method pattern. And within this situation, you actually want to render the implementation functions inside it final: anybody getting out of this class should only implement the brand new virtual functions accustomed to define the personalization otherwise, you most likely can't guarantee all the necessary invariants.

You need to prevent derived classes from overriding it, because...

  1. Virtual functions expose areas of your class towards the derived class which you may not need uncovered - if your derived class overrides this, altering when and how the function is known as might break the derived class.
  2. Performance / inlining. An online getter function inside a base class isn't good if things get really performance critical.
  3. You're calling the function out of your (base class) constructor, so overriding the function is pointless. (When known as in the base class constructor, the bottom class version from the function will be invoked).

/* we consider why there's you don't need to declare all functions in multilevel inheritance as virtual,because you will find some functions/actions that needs to be
carried out by only BASE CLASS. we do hope you understand with this simple example here Parent can easily see there children Report card and may sign up it. while Large_brother are only able to read report but cant sign, similarly small_brother also cant sign but could see clearly */

#include<cstring>
#include<iostream>
using namespace std;

class parent
{
private:
    int signeture;
protected:
    int read;
public:
    parent():read(0),signeture(0){}
    void sign()
    {
    signeture=1;
    cout<<"\nparent sign";
    }
    virtual void readReport()
    {
    cout<<"\nDAD can also read"<<read;
    }
};


class big_brother:public parent
{
public:
void readReport()
{
    read=3;
    cout<<"\nbig brother card"<<read;
}
//some other function which only Big_brother can performed are 
//declared here asnon_virtual

};



class small_brother:public big_brother
{
public:
void readReport()
{
    read=4;
    cout<<"\n small brothers card"<<read;

}
};
int main()
{
parent x;
big_brother y;
small_brother z;
x.readReport();
y.readReport();
z.readReport();
cout<<"only parent can sign";
x.sign();

return 0;
}