Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Pure Virtual Function and Base Class

 

C++ Pure Virtual Function and Virtual Base Class

In this C++ tutorial, you will learn about pure virtual function, declaration of pure virtual function and virtual base class, virtual base class and how to implement a virtual base class, explained with examples.



What is Pure Virtual Function:

Pure Virtual Function is a Virtual function with no body.


Declaration of Pure Virtual Function:

Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.


General Syntax of Pure Virtual Function takes the form:



class classname //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};



The other concept of pure virtual function remains the same as described in the previous section of virtual function.


To understand the declaration and usage of Pure Virtual Function, refer to this example:



class Exforsys
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};

class Exf1:public Exforsys
{
public:
void example()
{
cout<<"Welcome";
}
};

class Exf2:public Exforsys
{
public:
void example()
{
cout<<"To Training";
}
};

void main()
{
Exforsys* arra[2];
Exf1 e1;
Exf2 e2;
arra[0]=&e1;
arra[1]=&e2;
arra[0]->example();
arra[1]->example();
}



Since the above example has no body, the pure virtual function example() is declared with notation =0 in the base class Exforsys. The two derived class named Exf1 and Exf2 are derived from the base class Exforsys. The pure virtual function example() takes up new definition. In the main function, a list of pointers is defined to the base class.


Two objects named e1 and e2 are defined for derived classes Exf1 and Exf2. The address of the objects e1 and e2 are stored in the array pointers which are then used for accessing the pure virtual function example() belonging to both the derived class EXf1 and EXf2 and thus, the output is as in the above example.


The programmer must clearly understand the concept of pure virtual functions having no body in the base class and the notation =0 is independent of value assignment. The notation =0 simply indicates the Virtual function is a pure virtual function as it has no body.


Some programmers might want to remove this pure virtual function from the base class as it has no body but this would result in an error. Without the declaration of the pure virtual function in the base class, accessing statements of the pure virtual function such as, arra[0]->example() and arra[1]->example() would result in an error. The pointers should point to the base class Exforsys. Special care must be taken not to remove the statement of declaration of the pure virtual function in the base class.


Virtual Base Class


In the above example, there are two derived classes Exf1 and Exf2 from the base class Exforsys. As shown in the above diagram, the Training class is derived from both of the derived classes Exf1 and Exf2. In this scenario, if a user has a member function in the class Training where the user wants to access the data or member functions of the class Exforsys it would result in error if it is performed like this:



class Exforsys
{
protected:
int x;
};

class Exf1:public Exforsys
{ };

class Exf2:public Exforsys
{ };

class Training:public Exf1,public Exf2
{
public:
int example()
{
return x;
}
};



The above program results in a compile time error as the member function example() of class Training tries to access member data x of class Exforsys. This results in an error because the derived classes Exf1 and Exf2 (derived from base class Exforsys) create copies of Exforsys called subobjects.


This means that each of the subobjects have Exforsys member data and member functions and each have one copy of member data x. When the member function of the class Training tries to access member data x, confusion arises as to which of the two copies it must access since it derived from both derived classes, resulting in a compile time error.


When this occurs, Virtual base class is used. Both of the derived classes Exf1 and Exf2 are created as virtual base classes, meaning they should share a common subobject in their base class.


For Example:



class Exforsys
{
protected:
int x;
;


class Exf1:virtual public Exforsys
{ };

class Exf2:virtual public Exforsys
{ };

class Training:public Exf1,public Exf2
{
public:
int example()
{
return x;
}
};




In the above example, both Exf1 and Exf2 are created as Virtual base classes by using the keyword virtual. This enables them to share a common subobject of their base class Exforsys. This results in only one copy that the member function example() of Class Training can access the member data x.



Read Next: C++ Friend Functions



 

 

Comments


p.veena said:

  Explanation is too gud. Its in such a way that every basic learner can understand it.
August 2, 2007, 5:55 am

p.veena said:

  Can I know what is the exact differece between these two?
In the above example, where pure virtual function is declared, if that function is made as virtual will the ouput get effected?
August 2, 2007, 6:06 am

okka mogadu said:

  These tutorial is nice for beginners....
January 23, 2008, 6:43 am

pankky said:

  this explanation is damn great
thnx alot
July 5, 2008, 12:37 pm

kavya said:

  good explanation.. simple yet convincing..
November 6, 2008, 3:31 am

Dileep Nayak said:

  - it it easy to understand.
November 6, 2008, 10:30 pm

vikash said:

  amazing.............
January 12, 2009, 12:21 pm

Sarika said:

  Good to understand.Thanks
January 17, 2009, 6:15 am

1 said:

  very nice explanation
February 5, 2009, 10:25 pm

0 said:

  Thanks A Lot
February 9, 2009, 2:28 pm

a said:

  Tell me what is the use of pure virtual function as it contains no definition or declaration.
April 24, 2009, 3:09 am

Hemant Sauparna said:

  Same question as above.. Why do we need pure virtual if it doesn't have any body at all?
We could have declared that function in derived class also where we might need that. Why was there a need to declare it as pure virtual in base class.
June 2, 2009, 5:29 am

vnd said:

  A pure virtual function is merely an interface and can be thought of as a way to enforce policy. A pure virtual function should be used when subclasses implement the same interface completely differently. This passes only a single copy of variables and functions to its grand childs, and hence avoid compile time confusion for inheriting which variable through which class, as in case of multiple inheritance.
June 12, 2009, 2:26 am

Fahad said:

  If you want to make a program on shapes..then first you will have to make a base class called shape...and the derived classes like circle,rectangle,square etc...all these shapes will have some common fucntions like calculating area length etc...so either defining AREA function in all the classes you must make the AREA function as pure virtual function in the base class SHAPE and then use it in every derived classes..since no definition would be possible of the class AREA in the base class you make it pure virtual
July 15, 2009, 6:17 pm

pradeep said:

  we can use a different function name in each derived class for implementing its own logic.(i.e) areasq for findin the area of square,arearect for rectangle and so on. so why do we need a pure virtual function ?
September 16, 2009, 10:35 am

sowjanya said:

  if we declare a function as a pure virtual function then object of that class which has pure virtual function is never created
September 29, 2009, 10:30 pm

Javawocky said:

  Pradeep: Since all classes that represent shapes will be children of your Shape class, you can have an array (or other data structure) of Shape objects, as below:

Shape myShapes[3] = {Circle(), Square(), Triangle()};

If you wanted to find the area of each Shape without a pure virtual function, you would first have to know which shape was in each index of the array. However, with a pure virtual function in the Shape class and defined methods in each of its children, you can call the area method without knowing which type of Shape you have. This is useful in loops:

for (int i = 0; i < 3; ++i) {
cout << myShapes[i].area();
//not possible w/o a pure virtual area() method in Shape
}
December 3, 2009, 10:48 pm

wallter said:

  your missing a semi Collen in the first 'quote' box after the =0(;)
December 17, 2009, 11:42 am

yuvaraj said:

  If you don't want to allow others to instantiate your class (i.e create objects of your class type), then you need to put a pure virtual function in your class. By doing this, others can derive from your class but they won't be able to create objects of your class.
January 4, 2010, 12:22 am

dan said:

  So the only difference between pure virtual and virtual is you can not instantiate a class with a pure virtual, but can with a virtual. To instantiate a class with a pure virtual you must derive a new class from it?
January 15, 2010, 5:40 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape