Overriding vs Virtual

Posted by anonymous on Stack Overflow See other posts from Stack Overflow or by anonymous
Published on 2010-05-28T22:29:08Z Indexed on 2010/05/28 22:32 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

What is the purpose of using the reserved word virtual in front of functions? If I want a child class to override a parent function, I just declare the same function such as "void draw(){}".

class Parent{ public:   void say(){ std::cout << "1"; }};
class Child : public Parent{public:void say(){ std::cout << "2"; } };
int main()
{
    Child* a = new Child();
    a->say();
    return 0;
}

The output is 2.

So again, why would the reserved word "virtual" be necessary in the header of say() ?

Thanks a bunch.

© Stack Overflow or respective owner

Related posts about c++

Related posts about functions