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

Virtual Funciton from compilers point of view

Can any help me with understanding of Virutal function concept how it is realized from compiler point of view.

If it is a huge doc please let me know any site i can refer to.....
Sep 6 '06 #1
1 1915
Banfa
9,065 Expert Mod 8TB
A virtual function in a C++ class simply indicates to the compiler that sub-classes of the current class may override that function.

in such a case the compiler ranages to call the overridden function rather than the base cass function if a base class pointer is used.

Consider this program

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3.  
  4. class A
  5. {
  6. public:
  7.     A(){};
  8.     void print(){puts("class A");};
  9. };
  10.  
  11.  
  12. class B : public A
  13. {
  14. public:
  15.     B(){};
  16.     void print(){puts("class B");};
  17. };
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21.     B b;
  22.     A *pA = &b;
  23.  
  24.     pA->print();
  25.     b.print();
  26.  
  27.     return EXIT_SUCCESS;
  28. }
  29.  
The output is

class A
class B

Even though pA points to a class of type B it still calls the print function in class A, however if class A is changed to

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     A(){};
  5.     virtual void print(){puts("class A");};
  6. };
  7.  
the the program now outputs

class B
class B

because the print function is declared virtual the compiler knows that sub-classes may override that function so when it access the instance of the class through the pointer it will check and if the function is overridden it will call the function from the sub-class.

You can also have pure virtual functions, if print is a pure vrtual function then class A becomes

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     A(){};
  5.     virtual void print() = NULL;
  6. };
  7.  
class A is now an abstract function, that is you can no longer declare an instance of class A because it has no implementation of print. This tells the compiler that the is a class of general type A but that it will be sub-classed and the pure virtual functions will be over-ridden in order to be used.

A class that sub-classes from an abstract class (class B in this instance) must override all the pure-virtual functions in the base class or be an anstract class itself.
Sep 6 '06 #2

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

Similar topics

3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
9
by: Neil | last post by:
I've been discussing here a SQL 7 view which scrolls slowly when linked to an Access 2000 MDB. After trying various things, I've distilled it down to the following: when the linked view has a...
12
by: Daniel Kay | last post by:
Hi Folks! Everytime I work with virtual and pure virtual methods I ask myself if there is a difference between class B and class C (see below). Is it redundant to repeat the virtual keyword in a...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
8
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
6
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
If i have an interface like template <class K> struct IComparableTo { virtual int compareTo(const K& key) const = 0; }; some compilers (e.g. gcc) warn me about that the class has 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.