views:

89

answers:

6

Possible Duplicates:
Overriding vs Virtual
How am i overriding this C++ inherited member function without the virtual keyword being used?

I am learning C++ at this moment, but I'm not completely in the dark when it comes to programming languages. Something makes no sense to me. My understanding is that a VIRTUAL function in a class can be overridden in sub-classes. However, isn't that allowed by default? For example:

class Color {
    public:
        void Declare() { std::cout <<"I am a generic color."; }
};

class Purple : public Color {
};

If I create an instance of Purple and then call its Declare function, then it will obviously output the console window "I am a generic color." If I want to override this function in the Purple class, I can simply define it there to produce:

class Purple : public Color {
    public:
        void Declare() { std::cout <<"I am purple."; }
};

This, of course, outputs "I am purple." to the console. If I can override functions by default, then what is the point of having VIRTUAL functions to specifically tell the compiler it can be overridden? Sorry for the dumb question. :/

A: 

Virtual functions are useful when dealing with polymorphism. Non-virtual functions are looked up at compile time, so creating a variable of type Color and calling its Declare() method will always result in Color::Declare() being called, even if the object in the variable is a Purple.

Ignacio Vazquez-Abrams
Oh, that makes sense.
Reznor
+1  A: 

the difference is if you have a function foo which takes a colour and calls its declare method, if they are virtual, and you pass in a purple, it will say i am purple, if the method is not virtual, it will say i am a generic colour...

tobyodavies
A: 

No, its not redundant.

Try:

Color* color = new Purple();
color->Declare();

You want it to print "I am purple." And that will only happen if you declare Declare as virtual.

The choice of allowing to overwrite a function name in a derived class without it being virtual is a minor source of bugs in C++ programs. Different languages have different additudes on this.

jdv
A: 

Virtual functions allow polymorphism --- you can have a pointer to the base class and it still calls the function in the derived class. In your example

Color *c=new Purple;
c->Declare();

will output "I am a generic color". If the Declare() function in Color was virtual then the function in Purple would override it, and thus this code would print "I am purple."

Anthony Williams
+1  A: 

The important difference is what happens in this case:

Purple p;
Color* pC = &p;
pc->Declare();

If the function Declare is defined as non-virtual then the call will call go to the base class. If the function is delcared as virtual then the call will go to the derived class. So having a function will allow you to call a derived class method using the base class pointer. That is basically it supports polymorphism.

Naveen
+1  A: 

Consider this code:

class Color
{
public:
    void Declare() { std::cout << "I am a generic color"; }
};

class Purple : public Color
{
public:
    void Declare() { std::cout << "I am purple"; }
}

Color* color = new Purple();
color->Declare();

As written, this will print out "I am a generic color", i.e. calling Declare() via the base class pointer (Color*) will invoke the base class's implementation of the function.

If you add the keyword virtual in front of Color's declaration of Declare(), then "I am purple" will be printed. The virtual keyword tells the compiler that a call to the function via the base class pointer should be dispatched to the concrete class (Purple) implementation.

Gareth Stockwell