3 Replies - 4018 Views - Last Post: 30 November 2007 - 06:12 AM Rate Topic: -----

#1 software_developer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-November 07

Pure Virtual function concept

Posted 29 November 2007 - 07:15 AM

Hello, I was reading a very good tutorial about polymorphism. It was written very well but I dont seem to be grasping a concept. The author mentioned that "An object of a class which contains a pure virtual function cannot be created. It seems logical enough ie. If you have classes triangle, square, circle derived from shape class we wouldn't want to make an object of the shape class."

Maybe Im not understanding this concept but isnt creating an object of a base class {you can use its functions and members} the same thing as deriving an object from the base class? Essentially; both do the same thing correct?

Thanks for your help...

This post has been edited by software_developer: 29 November 2007 - 07:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pure Virtual function concept

#2 musya   User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Pure Virtual function concept

Posted 29 November 2007 - 05:49 PM

View Postsoftware_developer, on 29 Nov, 2007 - 07:15 AM, said:

Hello, I was reading a very good tutorial about polymorphism. It was written very well but I dont seem to be grasping a concept. The author mentioned that "An object of a class which contains a pure virtual function cannot be created. It seems logical enough ie. If you have classes triangle, square, circle derived from shape class we wouldn't want to make an object of the shape class."

Maybe Im not understanding this concept but isnt creating an object of a base class {you can use its functions and members} the same thing as deriving an object from the base class? Essentially; both do the same thing correct?

Thanks for your help...

In a sence you can still use all the features of the base class, only the derived classes are a class built ontop of whatever class you derived, so say you have base class shape with int size and what not a derived square class would have that and things like int numOfSides; so in a class with a virtual function if you created an object of that class(which you cant) you wouldnt be able to do much with it, since its functions are virtual ( think of them as not assigned) hope this makes sense...maybe a bit hopefully.

wow its been a while since i posted or done any coding...school......ehh....
Was This Post Helpful? 0
  • +
  • -

#3 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Pure Virtual function concept

Posted 29 November 2007 - 06:16 PM

View Postsoftware_developer, on 29 Nov, 2007 - 02:15 PM, said:

Maybe Im not understanding this concept but isnt creating an object of a base class {you can use its functions and members} the same thing as deriving an object from the base class? Essentially; both do the same thing correct?

Thanks for your help...

To "Create an object" means, to create an entity in your program which occupies some space in memory. At the most primitive level, this can mean that you create an int with a variable name. The object created in memory when you create an int variable occupies a number of bytes in which you store data.

Here is an example of an int object being created. You can't "see" the object, you can only see its variable name. You can, however, see, the value 5 being copied into the object (that is, the value 5 is written into the memory occupied by the object).
int i = 5;


That's a real simple example. C++ has more complicated objects that come from classes. take string for example.
string s = "Hello, world";
What's happening here isn't quite so clear cut because string is a class and not a primitive type; The upshot of it is that what's going on behind the scenes may be pretty damn complex. however, what remains true is that some memory is allocated to store an object, and your program has assigned that object a variable name.


When you write a class, you don't actually create any objects at all. All you do is create a cookie cutter, from which objects can be created later on. (In the same way that string is a class, but no 'string objects' exist in your program until you do something which causes one to be created). A class is a set of blueprints which specify what its objects will be able to do, and what data those objects will hold.

the term Deriving from a class means that you create a new class (NOT an object). The new class that you create will be a specialisation of its "base". Meaning, that the new, derived class can do everything that the base class can do, but it can also do more (Depending of course, what you tell the new class it can do). Each different derived class you write may be specialised in a different way, but will always have that 'base' behind it. All the derived classes will effectively have the base class as their common ground. Some derived classes may also choose to override the methods/behaviour that it inherits from the base class. This is another way that the derived classes are specialised, and different from their base.


Since deriving a class creates a whole new class, there is no requirement that the 'base' class can meaningfully represent an object. Its perfectly reasonable that the 'base' class remains as a purely abstract one. in other words, a class whose sole purpose is to define the minimum behaviour for a whole bunch of other classes.

In fact, it doesn't even need to provide behaviour, it could just act as a "shell", which declares method names that have no implementation/definition. These "empty" methods are the 'pure virtual' methods.
- Doing this acts as a "contract" between you, the programmer, and your compiler, which obliges all your derived classes to create specialised versions of the 'pure virtual' methods. (This is like compulsory overriding).

The reason for doing this is simple. You can have a whole bunch of objects of different classes, they probably do different things and may well store different data, but if you have a contract in place that ensures they all have some of the same method signatures, then you can use those signatures from a base-class pointer, without needing to know what "kind of" object you are specifically dealing with. All you need to know is that the object you've got is a derivative of your base class.

This post has been edited by Bench: 29 November 2007 - 06:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 software_developer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-November 07

Re: Pure Virtual function concept

Posted 30 November 2007 - 06:12 AM

Bench, thank you so much your reply. This is very helpful.

View Postmusya, on 29 Nov, 2007 - 05:49 PM, said:

View Postsoftware_developer, on 29 Nov, 2007 - 07:15 AM, said:

Hello, I was reading a very good tutorial about polymorphism. It was written very well but I dont seem to be grasping a concept. The author mentioned that "An object of a class which contains a pure virtual function cannot be created. It seems logical enough ie. If you have classes triangle, square, circle derived from shape class we wouldn't want to make an object of the shape class."

Maybe Im not understanding this concept but isnt creating an object of a base class {you can use its functions and members} the same thing as deriving an object from the base class? Essentially; both do the same thing correct?

Thanks for your help...

In a sence you can still use all the features of the base class, only the derived classes are a class built ontop of whatever class you derived, so say you have base class shape with int size and what not a derived square class would have that and things like int numOfSides; so in a class with a virtual function if you created an object of that class(which you cant) you wouldnt be able to do much with it, since its functions are virtual ( think of them as not assigned) hope this makes sense...maybe a bit hopefully.

wow its been a while since i posted or done any coding...school......ehh....



Musya, thanks for your response and for your help in this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1