Quantcast

Maximum PC

It is currently Thu Dec 18, 2014 4:46 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: need some C++ help here
PostPosted: Wed Sep 15, 2004 11:23 am 
Little Foot
Little Foot

Joined: Sun Jun 27, 2004 7:11 am
Posts: 124
Location: 127.0.0.1
hello, can someone explain to me the meaning of the following line?

Code:

class MyClass {

public:
...
...
virtual void Execute() = 0;
...
..
};


Well, I know what a virtual function is, but what i don't understand is why there is a "=0" after the virtual function? What difference is there between
Code:
virtual void Execute();

and
Code:
virtual void Execute() = 0;

???

THANKS


Top
  Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 1:05 pm 
I judge you GUILTY!
I judge you GUILTY!
User avatar

Joined: Tue May 25, 2004 4:38 pm
Posts: 162
Location: New York City
Code:
virtual void Execute() = 0;


The = 0 is a way to make a function a pure virtual function.
This in effect makes the class an abstract base class in which there is no implementation of the functions.

An abstract base class is a class that has no implementation. You cannot instantiate objects from abstract base classes. However you can create derived classes from these ABC's.

The purpose of an ABC is to create a generic class that you can derive from and then have a set of functions that you can implement differently for each derived class.

When a class has a virtual function, it just means that you can use a function name and have the compiler figure out which class to call. You can also define virtual functions in the implementation. Pure Virtual functions just say "hey don't make an object out of me, derive me first, then use my name". You cannot implement them.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 2:29 pm 
Little Foot
Little Foot

Joined: Sun Jun 27, 2004 7:11 am
Posts: 124
Location: 127.0.0.1
hello dude-x. thanks for the detailed explanation.

what makes an abstract base class different from an interface? since neither can be directly instantiated into an object?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 3:20 pm 
I judge you GUILTY!
I judge you GUILTY!
User avatar

Joined: Tue May 25, 2004 4:38 pm
Posts: 162
Location: New York City
Quote:
what makes an abstract base class different from an interface? since neither can be directly instantiated into an object?


In C++, an interface is basically a class (a collection of functions and data types). Formally, an interface is the exposed part of a class that the programmer expects not to change(the public functions and variables in C++)

The difference between an abstract base class and and a regular class is that abstract base classes cannot be used to create (instantiate) objects. They serve as a template for derived classes.

Therefore, an ABC meets the formal definition of an interface. In practice, since you cannot define pure virtual functions in abstract base classes, they are not usable at all unless you derive the class.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 9:57 am 
Little Foot
Little Foot

Joined: Sun Jun 27, 2004 7:11 am
Posts: 124
Location: 127.0.0.1
thanks, dude-x. always learning something new here...


Top
  Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 1:02 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Dude-X wrote:
Code:
virtual void Execute() = 0;


The = 0 is a way to make a function a pure virtual function.
This in effect makes the class an abstract base class in which there is no implementation of the functions.

An abstract base class is a class that has no implementation. You cannot instantiate objects from abstract base classes. However you can create derived classes from these ABC's.

Deja Vu? :D


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.