By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,377 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

Getting confused in Virtual calls

P: n/a
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

Dec 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Priya Mishra wrote:
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.


dynamic binding.
http://www.parashift.com/c++-faq-lit...functions.html

Dec 15 '05 #2

P: n/a
Priya Mishra wrote:
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

Dec 15 '05 #3

P: n/a
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;
}

Dec 15 '05 #4

P: n/a
On 2005-12-15, Priya Mishra <mi*******@gmail.com> wrote:
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.

--
Neil Cerutti
Dec 15 '05 #5

P: n/a

Neil Cerutti wrote:
On 2005-12-15, Priya Mishra <mi*******@gmail.com> wrote:
virtual void SetControls();

Then what does this really mean in the program.


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.

Dec 15 '05 #6

P: n/a
On 2005-12-15, BigBrian <wo**@brianmielke.com> wrote:

Neil Cerutti wrote:
On 2005-12-15, Priya Mishra <mi*******@gmail.com> wrote:
> virtual void SetControls();
>
> Then what does this really mean in the program.


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.


Yes, but not that specific function. ;-)

--
Neil Cerutti
Dec 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.