Lets say that you have a class Shape and standalone function int area(Shape*).
Then you have class Circle : public Shape and function int area(Circle*). This function definitely knows how to compute the area of a circle.
Now someone hands you a valid pointer to Shape. The pointed to object might be a Circle, or Square, or ...
You cannot simply call the int area(Shape*) for it would not know how to do the Right Thing.
You could test for all the possibilities during runtime in order to call the right area(), but that is tedious and you have to update it every time you create a new Shape subclass, like Triangle. Luckily, compiler can do the testing for us. Automatically.
We add virtual member int area() const to Shape and the subclasses. When we call the virtual area() via pointer to Shape, a correct function will be called.
I have executed the following program
class Base
{
public:
void display()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout << "Derived Class";
}
};
void main()
{
Derived d;
d.display();
}
Is the above code follows concept of method overriding because the function display() in class "Derived" overrides the function display() in class "Base".
If it is method overriding then why virtual function is used to achieve method overriding since we can achieve method overriding without using virtual function.
What you're doing is not overriding. It is actually shadowing. Try the following:
1 2 3 4 5 6 7 8
void main()
{
Base *b;
Derived d;
b = &d;
d.display(); // This shadows display() of Base
b->display(); // This will output "Base class" even though it points to Derived
}
Now try the same by adding virtual in front of display()
@abc1
If the method were correctly overridden, a Base pointer which points to a Derived object would successfully be able to use polymorphism and call the Derived's display() method from the Base pointer.
But as coder777 said, it doesn't. Even when your Base pointer points to a Derived object, b->display() will not call Derived::display().
According to that,method overriding is same what I have written above.Is it right?
method overriding is achieved without virtual function?Then why virtual function is used?
I am confused with or without using virtual function in program.So please help me in
this by giving code example.
13.3/2
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, ...etc... as Base::vf is declared,
then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf. http://eel.is/c++draft/class.virtual
I think the example in that explains it well. @abc1, the article you link is simply using the term "override" too broadly. It only has a specific meaning in OOP, whether we're talking about C++, C#, or Java -- overriding means to change the implementation of a superclass virtual method inside the subclass's method.
I am not understanding. Please explain virtual function in simple way without applying pointers.
The difference between overridden virtual functions and shadowed non-virtual functions only matters when using pointers or references. If you don't use pointers or references, there is no difference. @coder777 gave you an example using a pointer. @JLBorges gave you an example using a reference. Those are the only 2 scenarios that matter.
Have you tried compiling and running the examples given? Do it and look at the output. With @JLBorges' code, just click the gear symbol and you will be able to compile and run the program on-line. Look at the results and see why each specific function was called.
If we are talking about C++, we use the terminology that is used by the C++ community. The C++ standard specifies that the term override is used only for virtual functions, and that is the only accepted definition in C++.
As an aside, in C++, the term that is normally used and understood is 'member function' and not 'method'.