virtual functions

hi,
I am very new to virtual functions of c++.
what is virtual function?why it is used?

please help me in learning this concept.
Last edited on
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.
Last edited on
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()
Last edited on
@coder777,
Why the above code is not method overriding? Display method in class derived
overriding display method in class base.
Names are not so important. The important thing is virtual and non-virtual are different and exist for different purposes.

See:
https://isocpp.org/wiki/faq/strange-inheritance
particularly:
https://isocpp.org/wiki/faq/strange-inheritance#hiding-rule
https://isocpp.org/wiki/faq/strange-inheritance#overload-derived
@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().
Last edited on
please refer this link

https://www.programiz.com/cpp-programming/function-overriding

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.
Last edited on
> According to that,method overriding is same what I have written above.Is it right?

According to that, it is the same. However, that is wrong: only virtual functions (in a base class) can be overridden.

Here is a simple code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

struct base
{
    virtual void foo() const { std::cout << "base::foo\n" ; }
    /* not virtual */ void bar() const { std::cout << "base::bar\n" ; }
};

struct derived : base
{
    // base::foo is virtual: derived::foo overrides base::foo
    virtual void foo() const override { std::cout << "derived::foo overrides base::foo\n" ; }

    // base::bar is not virtual: derived::bar hides base::bar
    // uncomment the override specifier to get a compile-time error
    void bar() const /* override */ { std::cout << "derived::bar hides base::bar\n" ; }
};

int main()
{
    derived ddd ;
    base& bbb = ddd ;

    ddd.foo() ; // derived::foo overrides base::foo
    bbb.foo() ; // derived::foo overrides base::foo

    ddd.bar() ; // derived::bar hides base::bar
    bbb.bar() ; // base::bar
}

http://coliru.stacked-crooked.com/a/9bcef14b7e33add9

From the horse's mouth:
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
@JLBorges,
Why it is not method overiding?

I am not understanding. Please explain virtual function in simple way without applying pointers.
please refer this link
It is not correct.


In C++11 they added the override spedifier. See:

http://en.cppreference.com/w/cpp/language/override

It is actually an error to try to override a non virtual function.

There is overloading (without virtual). It makes sense for operators only. See:

http://en.cppreference.com/w/cpp/language/operators
coder777 wrote:
http://en.cppreference.com/w/cpp/language/override

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.

Side note, not directed at OP: If I could reverse time, I would totally make C++ have to follow the same thing that C# does. C++'s version of override keyword is very similar to Java's @Override. Both are safety checks, but not actually enforced by the language, unlike C#.
http://www.dotnettricks.com/learn/csharp/understanding-virtual-override-and-new-keyword-in-csharp
https://softwareengineering.stackexchange.com/questions/245393/why-was-c-made-with-new-and-virtualoverride-keywords-unlike-java
Last edited on
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.
Last edited on
> Why it is not method overiding?

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'.
Topic archived. No new replies allowed.