473,618 Members | 3,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual vs pure virtual member function

Hi Everyone,

I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
Thanks in advance!!!

Apr 30 '07 #1
7 24535
<sa*****@yahoo. co.inwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
Hi Everyone,

I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
Thanks in advance!!!
class MyClass
{
public:
virtual void MyFunction() { }; // Virtual
};

class MyDerived: public MyClass
{
};

That will compile.

class MyClass2
{
public:
virtual void MyFunction() = 0; // Pure Virtual
};

class MyDerived2: pubic MyClass2
{
};

That will not compile. Anything that derives from a class with a pure
virtual must override the method as none is defined.

A method needs to be virtual so the compiler will look for a derived
override in a base class. If a method is pure virtual, a derived class must
override the method.
Apr 30 '07 #2
On Apr 30, 9:31 am, sam_...@yahoo.c o.in wrote:
Hi Everyone,

I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
Thanks in advance!!!
If you declare a virtual function in a class say for example B, you
actually say: The function may be redefined in derived classes.

class B {
public:
virtual void f() { /* ... */ }
};

You define the f() for B and you usually redefine the function in
derived class:
class D : public B {
public:
void f() {/* another defintion */ }
};
virtual function is a primary tool for polymorphic bevaviour. If you
offer no defintion for D::f(), you will use the definition of B::f(),
because as an Inheritance principle, D inherited the public members of
B. Also you can declare f() in D as a virtual function, and the
redefinition process is continued in the classes that derived form D.
pure virtual function is a kind of virtual functions with a specific
syntax:
class B {
public:
virtual void f() =0; // =0 means pure virtual
};
if a class has at least one pure virtual function, it will be abstract
class, so instance creation is impossible. B::f() says you should
implement f() in derived classes:
class D : public B {
void f() { /* ... */ }
};
If you do not implement f() in D, the D is abstract class by default,
because it inherits all the pure virtual functions of class B.
for more detailed (and of course better) description, please see the
following references:
. C++ Standard Draft: Chapter 10
. The C++ Programming Language (3rd edition) by Bjarne Stroustrup:
Chapter 12.
. Design and Evolution of C++ by Bjarne Stroustrup: Chapter under
the title of "Class Concepts Refinements"

Regards,

Apr 30 '07 #3
On Apr 30, 7:31 am, sam_...@yahoo.c o.in wrote:
I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
A class which contains a pure virtual function is abstract, and
cannot be instantiated. A function call using dynamic
resolution results in undefined behavior if it would resolve to
a pure virtual function, and a pure virtual function is not
automatically considered "used", and so doesn't have to be
implemented unless it is actually called (e.g. by means of a
scope resolution operator, or implicitly from the destructor of
a derived class, if it is the destructor).

Thus:

class Base
{
public:
virtual void f() ; // Must be defined...
virtual void g() = 0 ;
virtual void h() = 0 ;

Base()
{
f() ; // calls Base::f()...
g() ; // undefined behavior...
Base::h() ; // calls Base::h, which
// must be defined.
}
} ;

Note that it is not necessary to defined Base::g() unless some
derived class calls Base::g().

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 30 '07 #4
class MyClass
{
public:
virtual void MyFunction() { }; // Virtual

};

class MyDerived: public MyClass
{

};

That will compile.

class MyClass2
{
public:
virtual void MyFunction() = 0; // Pure Virtual

};

class MyDerived2: pubic MyClass2
{

};

That will not compile. Anything that derives from a class with a pure
virtual must override the method as none is defined.
No. It will compile. MyDerived2 will be an abstract class and you
cannot create an instance of it.
If a method is pure virtual, a derived class must
override the method.
Not necessarily, unless one wants to create an instance of the derived
class.

Apr 30 '07 #5
sa*****@yahoo.c o.in wrote:
Hi Everyone,

I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
Your question is actually a FAQ. You can find the answer at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Apr 30 '07 #6
Prashanth wrote:
>class MyClass
{
public:
virtual void MyFunction() { }; // Virtual

};

class MyDerived: public MyClass
{

};

That will compile.

class MyClass2
{
public:
virtual void MyFunction() = 0; // Pure Virtual

};

class MyDerived2: pubic MyClass2
{

};

That will not compile. Anything that derives from a class with a pure
virtual must override the method as none is defined.

No. It will compile. MyDerived2 will be an abstract class and you
cannot create an instance of it.
If it compiles for you, it means your compiler is broken. This is what I
get with gcc 4.1.2:

[zzz@cherry data_create]$ g++ jj.cpp -o jj
jj.cpp: In function ‘int main()’:
jj.cpp:13: error: cannot declare variable ‘a’ to be of abstract type
‘MyDerived2’
jj.cpp:8: note: because the following virtual functions are pure
within ‘MyDerived2’:
jj.cpp:4: note: virtual void MyClass2::MyFun ction()

BTW there is a typo in the example. This is correct:

class MyClass2
{
public:
virtual void MyFunction() = 0;
};

class MyDerived2 : public MyClass2
{
};

int main()
{
MyDerived2 a;
}
>
> If a method is pure virtual, a derived class must
override the method.

Not necessarily, unless one wants to create an instance of the derived
class.
What is a point of a class, if it is not used??
May 1 '07 #7
On May 1, 3:25 pm, anon <a...@no.nowrot e:
Prashanth wrote:
class MyClass
{
public:
virtual void MyFunction() { }; // Virtual
};
class MyDerived: public MyClass
{
};
That will compile.
class MyClass2
{
public:
virtual void MyFunction() = 0; // Pure Virtual
};
class MyDerived2: pubic MyClass2
{
};
That will not compile. Anything that derives from a class with a pure
virtual must override the method as none is defined.
No. It will compile. MyDerived2 will be an abstract class and you
cannot create an instance of it.
If it compiles for you, it means your compiler is broken.
The standard requires that it compile. It compiles with every
compiler I've ever used.
This is what I
get with gcc 4.1.2:
[zzz@cherry data_create]$ g++ jj.cpp -o jj
jj.cpp: In function ?int main()?:
jj.cpp:13: error: cannot declare variable ?a? to be of abstract type
?MyDerived2?
jj.cpp:8: note: because the following virtual functions are pure
within ?MyDerived2?:
jj.cpp:4: note: virtual void MyClass2::MyFun ction()
Obviously, you've added some code which is illegal.
BTW there is a typo in the example. This is correct:
class MyClass2
{
public:
virtual void MyFunction() = 0;
};
class MyDerived2 : public MyClass2
{
};
int main()
{
MyDerived2 a;
Mainly, this. That wasn't in Prashanth's example, and in fact,
he explicitly said that it wasn't legal.
}
If a method is pure virtual, a derived class must
override the method.
Not necessarily, unless one wants to create an instance of the derived
class.
What is a point of a class, if it is not used??
Who said you cannot use it? You cannot create an instance of it
(as a full object, at any rate). You can still use it as a base
class, for further derivation, or use it in any context that
doesn't require an instance.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 2 '07 #8

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

Similar topics

11
4346
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 it inline. Like this.
37
4151
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 ]
10
2968
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 constructor change its behaviour accounting to the derived class. I tried to make A::fun() not pure virtual but virtual. It doesn't generate any error. But A::fun() is called in A's construction, while I
10
7292
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
6
3468
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
2
1395
by: Steven T. Hatton | last post by:
This is under the heading of "One Definition Rule" in the Standard, in a paragraph explaining what it means for an object or non-overloaded function to be 'used': "A virtual member function is used if it is not pure." The subsequent paragraph says this: "Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required." Is that saying that a virtual member...
3
1639
by: keith | last post by:
Dear mentors and gurus, I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note that it is possible to provide a definition for a pure virtual function, but this usually confuses novices and is best avoided until later". I've read through all the later stuff, and (although I may have missed it) I can't find anything further on this. Can someone please explain why in all the Halls of Hades you would declare a member...
14
2741
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
13
7255
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
0
8150
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8650
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8453
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7124
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.