Getting confused in Virtual calls

P

Priya Mishra

Hi all

if I have the public function in the class which has the key word
virtual i.e

public:
virtual void SetControls();

Then what does this really mean in the program.

Thanks In Advance
Priya
 
S

shan_rish

Priya said:
Hi all

if I have the public function in the class which has the key word
virtual i.e

public:
virtual void SetControls();

Then what does this really mean in the program.

Thanks In Advance
Priya

The virtual keyword allows you to define an entire different function
with the same name and parameters(arguments) in a derived class. In
your program SetControls can be defined in a derived class with same
signature (same parameters and return values). Please get a good book
and read it.

Cheers
Shan
 
B

BigBrian

The virtual keyword allows you to define an entire different function
with the same name and parameters(arguments) in a derived class.

You can do this without the "virtual" keyword also, but you don't get
dynamic binding.

#include <iostream>

class Base
{
public:
int foo() { std::cout << "in Base::foo()" << std::endl; }
};

class Derived : public Base
{
public:
int foo() { std::cout << "in Derived::foo()" << std::endl; }
};

int main()
{
Base * bp = new Derived();

// no polymorphism because Base::foo() isn't virtual
bp->foo();

return 1;
}
 
N

Neil Cerutti

Hi all

if I have the public function in the class which has the key word
virtual i.e

public:
virtual void SetControls();

Then what does this really mean in the program.

It means that this class provides a function that may vary
polymorphically for its derived classes, in other words, derived
classes may override the function with their own implementation.
In addition, it promises to provide a default implementation that
derived classes can use if they don't want to override it.
 
B

BigBrian

Neil said:
In addition, it promises to provide a default implementation that
derived classes can use if they don't want to override it.

The original post asked what "virtual" means. A member function can be
virtual and still not provide a default implementation.
 
N

Neil Cerutti

The original post asked what "virtual" means. A member
function can be virtual and still not provide a default
implementation.

Yes, but not that specific function. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top