473,396 Members | 1,785 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's the different betteen pure virtual function and virtualfunction

Hi,

I meet a question with it ,

I did not get clear the different betteen them,

for example:

#include <iostream>

class Base
{
public:
virtual ~Base();
virtual void pure() = 0;
};

inline Base::~Base()
{}

inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}

class Derived: public Base
{
public:
virtual void pure();
};

inline void Derived::pure()
{
Base::pure();
std::cout << "Derived::pure() called\n";
}

int main()
{
Derived derived;

derived.pure();
derived.Base::pure();

Derived *dp = &derived;

dp->pure();
dp->Base::pure();
}

Jun 27 '08 #1
14 2719
Jack wrote:
I did not get clear the different betteen them,
Given there's no errors in the code below (am not familiar enough with
virtual to know for sure on all the calls), I can provide my input:
for example:
[..]
virtual ~Base();
virtual void pure() = 0;
inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}
I think the pure() = 0 assignment in the class definition has no effect
since you later define the function, actually I'm not even sure if it is
allowed by the compiler.
class Derived: public Base
{
public:
virtual void pure();
};
This defines the new function for Derived which gets called if you call
pure() for an object of type Derived.
inline void Derived::pure()
{
Base::pure();
std::cout << "Derived::pure() called\n";
}
This first calls the Base class pure() and then adds its own code.
derived.pure();
This calls Derived.pure() (which in turn calls Base.pure() first),
output will be:
Base::pure() called
Derived::pure() called
derived.Base::pure();
This calls Base.pure() for object derived, output
Base::pure() called
dp->pure();
dp->Base::pure();
These two calls do exactly the same, just with a pointer as variable,
instead of an object.

I'm not sure I understand your problem?

Lars
Jun 27 '08 #2
Jack <Ja**********@gmail.comwrote:
I meet a question with it ,

I did not get clear the different betteen them,
First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

class Derived1 { }; // will not compile

class Derived2 {
public:
void pure() { cout << "pure\n"; }
};

int main() {
Base1 b; // will not compile
}

Now virtual:

class Base1 {
public:
virtual void vert();
};

class Derived1 { }; // will compile

int main() {
Base1 b; // will compile
}

That's the difference.
Jun 27 '08 #3
Hi Daniel,

Just so I get to learn something from this:

Daniel T. wrote:
First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};
So _pure_ refers to a virtual function definition and the = 0 will allow
for class definition without an actualy function body declaration? And
then you may not use the base class, and also
class Derived1 { }; // will not compile
_must_ define the virtual function in the derived class that you want to
use?

And could you declare a pure virtual function, then derive another one
from that, then derive a third one and ONLY define the function body in
the third one, if that is the function you're going to use?
e.g.:
class Base { public: virtual void pure() = 0; };
class Derived : public Base { };
class TwiceDerived : public Derived { void pure() { cout << "twice
derived pure"; }

and then use
TwiceDerived td;
td.pure();
?

Best Regards,

Lars
Jun 27 '08 #4
Daniel T. wrote:
Jack <Ja**********@gmail.comwrote:
>I meet a question with it ,

I did not get clear the different betteen them,

First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

class Derived1 { }; // will not compile
Huh? On what compiler? You just defined an unrelated Derived1 class type.
You probably mean "class Derived1: public Base1 {};" but even in such a
case it will compile. It won't compile if you try to instantiate a Derived1
but you didn't say anything about instantiating it.
>
class Derived2 {
public:
void pure() { cout << "pure\n"; }
};
An unrelated Derived2 type that has a "void pure()" function. You probably
(again) mean "class Derived2: public Base1". In which case you will
override the pure virtual from Base1 and thus instances of Derived2 will be
allowed.
int main() {
Base1 b; // will not compile
}
Right.
Now virtual:

class Base1 {
public:
virtual void vert();
};

class Derived1 { }; // will compile
You mean "class Derived1: public Base1 {};" and "instantiating it will not
compile".
int main() {
Base1 b; // will compile
}
Right.
That's the difference.
Ok :)

--
Dizzy

Jun 27 '08 #5
In article <da****************************@earthlink.vsrv-
sjc.supernews.net>, da******@earthlink.net says...

[ ... ]
First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

class Derived1 { }; // will not compile
Of course it will. Right now, the 'Derived' part of the name is a lie --
this isn't derived from anything; it's just an empty class, which will
compile just fine.

Even if Derived1 was derived from Base1, it would still compile:

class Derived1 : public Base1 { }; // will compile

What _won't_ compile is if you attempt to _instantiate_ an object of
this type:

Derived1 x; // will NOT compile.

A pure virtual function does _not_ imply that every derived class must
override that function. Rather, it implies that when/if an object of a
derived class is instantiated, that _some_ class must have overridden
the virtual function. In some cases, it's quite reasonable to have two
(or more) levels of derivation, including an intermediate class that
does NOT override a pure virtual function declared in the base class:

#include <iostream>

class Base2 {
public:
virtual void pure() = 0;
};

class Derived2 : public Base2 {
public:
virtual void pure2() = 0;
};

class DoublyDerived2 : public Derived2 {
public:
void pure() { std::cout << "dd2_pure()\n"; }
void pure2() { std::cout << "dd2_pure2()\n"; }
};

class DoublyDerived3 : public Derived2 {
public:
void pure() { std::cout << "dd3_pure()\n"; }
void pure2() { std::cout << "dd3_pure2()\n"; }
};

As it stands, this probably doesn't make a lot of sense, so let me try
to make the example a bit more concrete:

namespace database {
class generic {
public:
virtual void open(char const *) = 0;
};

class SQL {
public:
cursor exec_query(char const *) = 0;
};

class Oracle : public SQL {
public:
// these two functions actually have to be implemented.
void open(char const *db_name);
cursor exec_query(char const *query);
};

class Postgres : public SQL {
/* like Oracle */
};

class MySQL : public SQL {
/* like Oracle */
};

class Paradox : public generic {
void open(char const *db_name) {
/* ... */
}
};

}

We can never (even attempt to) create an object of the SQL class -- but
the SQL class can exist (and provide utility) without overriding the
pure-virtual function in the base class. We can create objects of the
Postgres, Oracle or MySQL classes, and we can have a SQL* point at one,
or a SQL& refer to one.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #6
Lars Uffmann wrote:
Jack wrote:
>I did not get clear the different betteen them,

Given there's no errors in the code below (am not familiar enough with
virtual to know for sure on all the calls), I can provide my input:
>for example:
[..]
virtual ~Base();
virtual void pure() = 0;
>inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}

I think the pure() = 0 assignment in the class definition has no effect
since you later define the function, actually I'm not even sure if it is
allowed by the compiler.
Maybe you should familiarize yourself with pure virtual functions
before giving answers about things you don't know?

"virtual void pure() = 0;" is not an assignment. It's just a syntax
for declaring a pure virtual function (AFAIK the story of this goes
something like the C++ standardization committee not wanting to create
yet another reserved keyword for only this purpose).

It's perfectly valid to give a pure virtual function an
implementation. The function will still be purely virtual (ie. the class
must be inherited to instantiate it), but the implementation can be
called explicitly.
Jun 27 '08 #7
Lars Uffmann wrote:
So _pure_ refers to a virtual function definition and the = 0 will allow
for class definition without an actualy function body declaration?
No. A pure virtual function is one which must be reimplemented in a
derived class. The "=0" is just syntactical notation to say that.
(Imagine it being a keyword like "pure" instead.)

The "=0" has nothing to do with whether you have to implement the
function or not. Any function can be declared but not implemented. (It's
just that if you try to call the function and it's not implemented,
you'll get a linker error.)
Jun 27 '08 #8
Juha Nieminen schrieb:
Lars Uffmann wrote:
>Jack wrote:
>>I did not get clear the different betteen them,
Given there's no errors in the code below (am not familiar enough with
virtual to know for sure on all the calls), I can provide my input:
>>for example:
[..]
virtual ~Base();
virtual void pure() = 0;
inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}
I think the pure() = 0 assignment in the class definition has no effect
since you later define the function, actually I'm not even sure if it is
allowed by the compiler.

Maybe you should familiarize yourself with pure virtual functions
before giving answers about things you don't know?

"virtual void pure() = 0;" is not an assignment. It's just a syntax
for declaring a pure virtual function (AFAIK the story of this goes
something like the C++ standardization committee not wanting to create
yet another reserved keyword for only this purpose).
"void" would be nice:

virtual void pure(void) void; // not serious!
It's perfectly valid to give a pure virtual function an
implementation. The function will still be purely virtual (ie. the class
must be inherited to instantiate it), but the implementation can be
called explicitly.
Just to add:
In one case, you _have to_ give an implementation: In a pure virtual
destructor. Since the destructor of a derived class will call all its
base class destructors recursively, you need an implementation for all
destructors, even for pure ones.

--
Thomas
Jun 27 '08 #9
Juha Nieminen wrote:
>I think the pure() = 0 assignment in the class definition has no effect
since you later define the function, actually I'm not even sure if it is
allowed by the compiler.

Maybe you should familiarize yourself with pure virtual functions
before giving answers about things you don't know?
Maybe you should learn some basic politeness. And then not blurt out
assumptions like the one that seems to have driven you to this comment:
"virtual void pure() = 0;" is not an assignment.
Now where did I state I thought this to be an assignment? My "allowed by
the compiler" was referring to his pure() = 0 being followed by a
declaration of that very pure virtual function. As stated in my last
post. So - from my understanding, you not only managed to miss that
point AND correct me on something that didn't need correction, you also
failed at basic friendly communication... This is especially annoying
because I clearly stated that I wasn't an expert on the matter, and was
trying to be helpful anyways.

Hey - if you have a bad day, take it somewhere else, okay?

Sheesh...

Lars
Jun 27 '08 #10
In article
<da****************************@earthlink.vsrv-sjc.supernews.net>,
"Daniel T." <da******@earthlink.netwrote:
Jack <Ja**********@gmail.comwrote:
I meet a question with it ,

I did not get clear the different betteen them,

First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

class Derived1 { }; // will not compile

class Derived2 {
public:
void pure() { cout << "pure\n"; }
};

int main() {
Base1 b; // will not compile
}

Now virtual:

class Base1 {
public:
virtual void vert();
};

class Derived1 { }; // will compile

int main() {
Base1 b; // will compile
}

That's the difference.
Well, I posted the above without much due diligence! Sorry about that...

class Base {
public:
virtual void pure() = 0; // whether Base1::pure() is defined or not
};

class Derived1 : public /*or protected or private*/ Base1 { };

class Derived2 : public Base1 {
public:
void pure() { }
};

int main() {
Base b; // will not compile
Derived1 d; // will not compile
Derived2 d2; // will compile
}

now virtual:

class Base {
public:
virtual void virt() { }
};

class Derived1 : public /*or protected or private*/ Base1 { };

class Derived2 : public Base1 {
public:
void virt() { }
};

int main() {
Base b; // will compile
Derived1 d; // will compile
Derived2 d2; // will compile
}
Jun 27 '08 #11
Lars Uffmann <ar**@nurfuerspam.dewrote:
Daniel T. wrote:

Just so I get to learn something from this:
First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

So _pure_ refers to a virtual function definition and the = 0 will allow
for class definition without an actualy function body declaration?
That's part of it. Making the function pure (i.e., putting the "=0" at
the end,) means you don't have to define the function for that class
(you can if you want though.)
And then you may not use the base class, and also
You can use the base class, but you cannot instantiate an object
directly from the base class.
_must_ define the virtual function in the derived class that you want to
use?
More properly, some class in the hierarchy must define that member
function for objects of its type or you won't be able to directly
instantiate objects of that type.
And could you declare a pure virtual function, then derive another one
from that, then derive a third one and ONLY define the function body in
the third one, if that is the function you're going to use?
e.g.:
class Base { public: virtual void pure() = 0; };
class Derived : public Base { };
class TwiceDerived : public Derived { void pure() { cout << "twice
derived pure"; }

and then use
TwiceDerived td;
td.pure();
?
Yes.
Jun 27 '08 #12
On Jun 3, 12:39 pm, "Daniel T." <danie...@earthlink.netwrote:
Jack <Jack.L.Ch...@gmail.comwrote:
I meet a question with it ,
I did not get clear the different betteen them,
First, pure virtual:
class Base1 {
public:
virtual void pure() = 0;
};
class Derived1 { }; // will not compile
Why not? Derived1 is still abstract, so you can't instantiate
it, but you can still define it (and use it as a base class for
further derivation).

Declaring a member function pure virtual has three effects:

1. you're not required to define it (but you are allowed to);
2. you cannot instantiate an instance of a class with a pure
virtual function; and
3. if dynamic resolution resolves to the pure virtual function,
your code has undefined behavior (and with most compilers,
will crash).

Thus, for 1:

class Base
{
public:
virtual void notPure() ;
} ;

class Derived
{
public:
virtual void notPure() {}
} ;

int
main()
{
Derived b ;
}

has undefined behavior (and will often fail at link), because
you have not defined Base::notPure, and this despite the fact
that you never call Base::notPure. Making Base::notPure pure
renders the code legal.

For 2:

class Base
{
public:
virtual void pure() = 0 ;
} ;

int
main()
{
Base b ;
}

will not compile, because the class Base is abstract.

For 3:

class Base
{
public:
Base() { pure() ; }
virtual void pure() = 0 ;
} ;

class Derived
{
public:
virtual void pure() {}
} ;

int
main()
{
Derived d ;
}

has undefined behavior, because the call to pure() in Base
resolves to Base::pure(), which is pure virtual. Note that this
remains true even if you define Base::pure(). With most
compilers, this code will crash on execution (but you can't
count on it, since the behavior is undefined).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #13
Daniel T. wrote:
That's part of it. Making the function pure (i.e., putting the "=0" at
the end,) means you don't have to define the function for that class
(you can if you want though.)
[..more useful information..]
Thank you! :)
Jun 27 '08 #14
On Jun 3, 4:28 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Lars Uffmann wrote:
So _pure_ refers to a virtual function definition and the =
0 will allow for class definition without an actualy
function body declaration?
No. A pure virtual function is one which must be reimplemented
in a derived class. The "=0" is just syntactical notation to
say that. (Imagine it being a keyword like "pure" instead.)
You're right that the =0 is just syntactical notation (but it is
two separate tokens...you can insert white space between the two
characters, or even comments). But there's more to it than you
seem to be saying.
The "=0" has nothing to do with whether you have to implement
the function or not. Any function can be declared but not
implemented. (It's just that if you try to call the function
and it's not implemented, you'll get a linker error.)
Again, the rules are a bit more complicted. A function must be
implemented if it is "used". A function is used if it is
called, of course, or if its address is taken, but a virtual
function is also "used" anytime you create an instance of the
class, or of a class derived from it, unless it is pure. Making
a virtual function pure frees you from the obligation of having
to implement it (amongst other things).

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

Jun 27 '08 #15

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

Similar topics

3
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
9
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
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 ]
3
by: Adriano Coser | last post by:
Hi. I'm declaring a property on a base class that is represented by a pure virtual function. Something like this: public: __declspec( property( get=GetWidth ) ) int Width; virtual int...
6
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
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: asm23 | last post by:
Hi, I'm using Intel C++ compiler 9 to compiler a project. But, there are a lot of warning saying like "virtual function override intended....". I searched old messages in Google groups, someone...
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
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
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.