473,396 Members | 1,758 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,396 software developers and data experts.

What are the significances of Virtual Functions?

Hey guys,
can anyone tell me what are the significances of virtual functions in C++?
Sep 28 '06 #1
1 1525
Banfa
9,065 Expert Mod 8TB
a virtual function is one where it is expected that a sub-class will over-ride the function. By declaring it virtual even if the function is envoked using a pointer to the classes base class the function in the sub class will be called

Expand|Select|Wrap|Line Numbers
  1. class BaseClass
  2. {
  3. public:
  4.     virtual void printMe(){ puts("I am the Base Class");}
  5. };
  6.  
  7. class DerivedClass : public BaseClass
  8. {
  9. public:
  10.     void printMe(){ puts("I am the Derived Class");}
  11. };
  12.  
  13. int main()
  14. {
  15.     DerivedClass dc;
  16.     BaseClass *pbc = &dc;
  17.  
  18.     pbc->printMe();
  19.  
  20.     return 0;
  21. }
  22.  
This outputs

I am the Derived Class

without the virtual keyword it would output

I am the Base Class
Sep 28 '06 #2

Sign in to post your reply or Sign up for a free account.

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...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
4
by: PengYu.UT | last post by:
Hi, It seems that the line with "//no error" and "//error" are the same. However, the second one give an error. I'm using g++-3.3. The error message was: g++-3.3 -g -c -o main.o main.cc...
5
by: alex goldman | last post by:
When compiling this code with g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4 ###################################### #include <vector> class b { public: virtual void f() = 0;...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
20
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
17
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.