+ 17
What exactly does virtual function means?
why are the used ? what is the difference between virtual and ordinary fuction and what is its advantage?
12 Respostas
+ 54
A virtual function is a function that behaves differently when called by derived functions.
They are used when there is a base class who shares actions of the derived classes.
It can be thought as: An animal has to eat. A dog is an animal, so it has to eat too; each animal has a distinct method to eat. In this case eat would be a virtual method of class Animal, and Dog class would have its own implementation of eat.
Let me ask you a question, how does an animal-the concept of an animal- eat? Well, the concept of an animal cannot eat, it depends on the animal. This is where function eat of class Animal becames pure virtual, meaning that the class Animal becames abstract. Let me implement this example.
struct Animal {
void eat() = 0; // pure virtual, the concept of an animal cannot eat
};
struct Dog : public Animal {
void eat() { cout << "A dog has eaten"; } // Every derived class needs an implementation of Animal::eat()
};
struct Cat : public Animal {
void eat() { cout << "A cat has eaten"; }
};
int main() {
Animal* dog = new Dog();
Animal* cat = new Cat();
dog->eat(); // Calls Dog::eat()
cat->eat(); // Cals Cat::eat()
Animal anim; // Error, Animal is abstract
}
+ 6
if we have a same function in both base class and derived class, we keep virtual to the function in base class
+ 6
What does it mean?
When you define a base class pointer and set it equal to the memory address of a subclass object, you are able to call the special version of the subclass version of a method you defined as virtual in the base class.
+ 5
What is the difference to an ordinary function?
Example: Let "vehicle" be a Base Class with the method "move". Now you can create an instance of a subclass where this method is also defined and overrides its "parent" version of this method.
Now the code
car hotWheels;
vehicle * transport = &hotWheels;
means that a pointer of type vehicle points at an instance of its subclass car. Okay, now the interesting part is what happenes when you call
transport->move;
In case the method is NOT virtual in the base class but overridden in the subclass "car", the base class version is called (no special moving behavior of the car is invoked). BUT if you define it as virtual in the base class, the scenario above would result in "passing" the call to the subclass object's version of this function, i.e. the object the base class pointer points to: You get the special moving behavior of the car.
+ 5
Why are they used?
One argument may be efficiency:
You declare an array of pointers of type "vehicle" and assign each entry to an object like car, boat, etc.
Now, in a loop you may call
for(int i=0; i<size; ++i){
vehicle[I] -> move
}
which, in case "move" is virtual in the class "vehicle", the correct method for each instance of car, boat, etc gets called.
This is also useful if you write a routine like this without knowing what kind of objects actually are created with e.g. via decisions of the user, etc.
Advantages?
Well, this can't really be argued as it depends on the scenario and what you plan to do. As soon as you understand what it is for and learn to implement it, you will find areas of application for it yourself.
It sometimes just poses as a mean to structure your project such that it is easy to understand and readable by others.
+ 4
virtual function means that a call to the function would be resolved at runtime depending on the actual instance of object that invoked the method through Base class pointer or reference. This is also called as "Late Binding". "Late Binding" is that magic, which makes polymorphism work in C++.
In c++, polymorphic behavior is achieved using inheritance and virtual keyword in front of function declaration/definition like
class Base
{
public virtual void Draw() {}
};
class Derived: public Base
{
public virtual void Draw() {}
};
// invocation examples
Base b;
b.Draw(); // invokes base class Draw(). Actual object is of Base class
Derived d;
d.Draw();// invokes Derived class Draw(). Actual object is of Derived class
b = d;
b.Draw(); // invokes Base class Draw(). You may think Derived class Draw should have been called since you are assigning derived class object to base class object. But 'b' too is an object (of Base class) and since this is a direct assignment(also called upcasting), the compiler chops of whatever extra was in 'd' (this is also called slicing) and what remains is a Base class object, hence Base class Draw() is called.
Base &bref = d;
bref.Draw(); // calls Derived class Draw. The actual object is of Derived class. Here slicing doesn't happens because the Derived class object is referenced through Base class reference object;
Base *bptr = &d;
bptr->Draw(); // calls Derived class Draw. The actual object is of Derived class. Here slicing doesn't happens because the Derived class object is referenced through Base class pointer object.
Polymorphism is not virtual functions. Polymorphisms is achieved through virtual functions (and inheritance).
+ 2
I'm new to the whole coding scene but I gotta say it's amazing!
+ 1
The difference between virtual functions and ordinary functions is that in case of ordinary functions, the call to the function is resolved at compile time while calls to virtual function is resolved at runtime.
0
@MMK,
efficiency is not the factor with virtual methods. On the contrary virtual functions adds runtime overhead since virtual table (vtable) needs to be created for every class and calls needs to be resolved).
As the depth of inheritance increase, the size of vtable increase.
Execution wise, virtual methods are marginally slower than normal methods.
With virtual methods you sacrifice marginal execution efficiency for dynamic behavior (polymorphism).
0
when we use same function name in both the base class and drived class so base class function declared as virtual .using virtual keyword like as
virtual void func1(){ cout<<"base class";}
0
virtually is the function in c++
- 8
animal