473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Virtual functions --- Explain me.

I would like to know what is virtual function in c++.
Besides what is virtual.

Jul 23 '05 #1
9 1448
aj********@gmail.com wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.


Get a good book and read about them. Virtual functions are
the basis for implementing run-time polymorphism and represent
a huge part of C++ language. It is impossible to cover them
in a newsgroup post.
Jul 23 '05 #2
ajay.so...@gmail.com wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.


The same as in Java.

Jul 23 '05 #3
Rapscallion wrote:
ajay.so...@gmail.com wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.

The same as in Java.


There are some small yet very important differences.
Jul 23 '05 #4
aj********@gmail.com wrote:
I would like to know what is virtual function in C++.
Besides what is virtual.
A virtual function is not necessarily
the actual function called when it is invoked:
cat main.cpp #include <iostream>

class Base {
public:
void f(void) const {
std::cerr << "Base::f(void)" << std::endl;
}
virtual
void g(void) const {
std::cerr << "Base::g(void)" << std::endl;
}
};

void test(const Base& b) {
b.f();
b.g();
}

class Derived: public Base {
public:
void f(void) const {
std::cerr << "Derived::f(void)" << std::endl;
}
virtual
void g(void) const {
std::cerr << "Derived::g(void)" << std::endl;
}
};

int main(int argc, char* argv[]) {
Base b;
test(b);
Derived d;
test(d);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp
./main

Base::f(void)
Base::g(void)
Base::f(void)
Derived::g(void)

The function test(const Base&) is passed a [const] *reference*
to an object of the Base type
so it must always call function f(void) for the Base type
but the actual function called by virtual function g(void)
depends upon the actual type of the object reference through b.
Jul 23 '05 #5
In article <11**********************@f14g2000cwb.googlegroups .com>,
<aj********@gmail.com> wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.


A virtual member function in a base class is overridden by a member
function of the same name in a derived class.

--
"Yes, I revere you much, honored ones, and wish to fart in response." --
Aristophanes, Clouds
Jul 23 '05 #6

"Gregory L. Hansen" <gl******@steel.ucs.indiana.edu> wrote in message
news:d6**********@rainier.uits.indiana.edu...
In article <11**********************@f14g2000cwb.googlegroups .com>,
<aj********@gmail.com> wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.


A virtual member function in a base class is overridden by a member
function of the same name in a derived class.


There's a bit more to the subject than that, isn't there? :-)

And, it doesn't *have* to be overridden...ever. You can make a function
virtual just *in case* you want to override it later.

-Howard
Jul 23 '05 #7
Victor Bazarov wrote:
Rapscallion wrote:
ajay.so...@gmail.com wrote:
I would like to know what is virtual function in c++.
Besides what is virtual.


The same as in Java.


There are some small yet very important differences.


eh?

What differences?

Aside from all methods are virtual in Java by default.
Java methods can be made non-virtual by appending the 'final' keyword to
them as in:
public final void someMethod() {
}
Jul 23 '05 #8
Andrew McDonagh wrote:
Victor Bazarov wrote:
Rapscallion wrote:
ajay.so...@gmail.com wrote:

I would like to know what is virtual function in c++.
Besides what is virtual.


The same as in Java.

There are some small yet very important differences.

eh?

What differences?


Methods in Java called virtually from constructors. In C++ inside
a constructor the virtual function is that of the object under
construction.
Aside from all methods are virtual in Java by default.
Java methods can be made non-virtual by appending the 'final' keyword to
them as in:
public final void someMethod() {
}


They are not "made non-virtual" -- wrong choice of words.

V
Jul 23 '05 #9
Howard wrote:
A virtual member function in a base class is overridden by a member
function of the same name in a derived class.

There's a bit more to the subject than that, isn't there? :-)

And, it doesn't *have* to be overridden...ever. You can make a function
virtual just *in case* you want to override it later.

-Howard


Also, the overriding function must not only have the same name, but the
same signature.

--
Matthias Kaeppler
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
7
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number;...
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
9
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
6
by: Gerhard Prilmeier | last post by:
Hello, I have an unmanaged C++ API that uses virtual functions, like this: class A { public: virtual void handleMe(){} };
14
by: vermarajeev | last post by:
Hi guys, I have problem understanding virtual functions??? Like how it works. My friend asked me some questions 1) What is a static function??? I replied functions to be used without...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.