473,394 Members | 1,752 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.

differnce b/w pure virtual and virtual.......

is pure virtual function has more memory than virtual function?????
Dec 15 '13 #1
3 3008
weaknessforcats
9,208 Expert Mod 8TB
First, what is a virtual function?

Virtual functions are used to implement polymorphism in C++. With polymorphism, you have a hierarchy of classes and the derived class objects are to be used only with a base class pointer. So, if you have a base class pointer pointing at a derived class object, and you call a base class method, the virtual keyword diverts the call to the derived class method because you really have a derived class object.

Obviously, in the example above, there is a base class method with the same name and arguments as the derived class method. The compiler as a choice: either call the base class method or call the derived class method.

So what do you do when you always want to call the derived class method and never call the base class method. Like maybe the base class method is just a function prototype of what the derived class should implement?

Enter the pure virtual function. The =0 only means that you cannot call the function using an object of the class that declares it.

You are permitted to call the pure virtual function from the derived class since the derived class is not the class declaring the pure virtual function. This is a nifty to stash code that is common to all derived objects rather than duplicating the common code in each derived class.

There is nothing about memory here. Memory is not part of C++ but is part of how C++ is implemented by the compiler. Commonly, a class with virtual functions has an associated virtual function table, or vtable, that the compiler has constructed to keep all these virtual calls straight. This table has the addresses of the correct virtual functions to call using an object of this class. The address of this table is kept inside the object so you will see objects like this are larger, by the size of an address, from objects that do not have virtual functions.
Dec 15 '13 #2
Sherin
77 64KB
Virtual functions:

1) It has its definition in base class.

2) Virtual Function is declared with keyword 'virtual' at the declaration.
Ex : virtual return_type function_name(function arguments);

3) All derived classes may or may not override, virtual function

4) These are hierarchical in nature so it does not affect if any derived class does not inherit it.

5) No concept of abstract classes.



Pure Virtual functions:


1) It has no definition in base class

2) Pure Virtual Function is declared as
Ex : virtual return_type function_name(function arguments) = 0;

3) All derived classes must override,pure virtual function.

4) Gives compilation error if derived classes are not inherited.

5) If class is pure virtual function, then class is declared as abstract class
Sep 28 '20 #3
Banfa
9,065 Expert Mod 8TB
Pure Virtual functions:

1) "It has no definition in base class"
Not strictly true, it normally doesn't have a definition in the base class but you can give it a definition there if you choose; however this definition cannot be given as part of the declaration i.e.
Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3.     virtual int exFun(int param) = 0
  4.     {
  5.       // Code
  6.     }
  7. }
is invalid. Also it can only be called explicitly from a base class. Finally I have never managed to think of a situation where this feature is useful.

3) "All derived classes must override,pure virtual function."
Again strictly not true but if they don't override it the function remains pure virtual in the derived class and the derived class is abstract although it itself could then be inherited and the pure virtual function overridden.

4) "Gives compilation error if derived classes are not inherited."
Not quite sure what you mean here, do you mean that if you fail to override a pure virtual function in a derived class but still try to instantiate the derived class you get a compilation error? This is true because the derived class is still abstract and you can't instantiate abstract classes.
Sep 28 '20 #4

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

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
23
by: ctick | last post by:
A reason for declaring a "virtual destructor" for a Base class is to make sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived. ...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
6
by: cppsks | last post by:
Consider the following: class BaseInterface { public: virtual void something() = 0; }; class AbstractSubclass : public BaseInterface {
2
by: sam++ | last post by:
Hi, I created a pure virtual class P and its derived sub-class D. In class P, declared a pure virtural function pure(); in sub-class D define its implementation. eg. class P {
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
10
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
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;"...
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: 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
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?
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
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
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...

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.