Monday, April 15, 2013

What is a pure virtual function?

A pure virtual function is a function that has the notation "= 0" in the declaration of that function. Why we would want a pure virtual function and what a pure virtual function looks like is explored in more detail below.
Here is a simple example of what a pure virtual function in C++ would look like:

 Example of a pure virtual function in C++

class ABC {
public:
   virtual void pure_virtual() = 0;  // a pure virtual function
   // note that there is no function body  
};

The pure specifier

The "= 0" portion of a pure virtual function is also known as the pure specifier, because it’s what makes a pure virtual function “pure”. Although the pure specifier appended to the end of the virtual function definition may look like the function is being assigned a value of 0, that is not true. The notation "= 0" is just there to indicate that the virtual function is a pure virtual function, and that the function has no body or definition. Also note that we named the function “pure_virtual” – that was just to make the example easier to understand, but it certainly does not mean that all pure virtual functions must have that name since they can have any name they want.Can a pure virtual function have an implementation?
The quick answer to that question is yes! A pure virtual function can have an implementation in C++ – which is something that even many veteran C++ developers do not know. So, using the SomeClass class from our example above, we can have the following code:
class SomeClass {
public:
   virtual void pure_virtual() = 0;  // a pure virtual function
   // note that there is no function body  
};

/*This is an implementation of the pure_virtual function
    which is declared as a pure virtual function.
    This is perfectly legal:
*/
void SomeClass::pure_virtual() {
    cout<<"This is a test"<<endl;
 
 } 
 
 
For Read More http://www.programmerinterview.com/index.php/c-cplusplus/pure-virtual-function/ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Static Function Members:


By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to the this pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not.
Let us try the following example to understand the concept of static function members:
#include <iostream>
 
using namespace std;

class Box
{
   public:
      static int objectCount;
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // Increase every time object is created
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      static int getCount()
      {
         return objectCount;
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

// Initialize static member of class Box
int Box::objectCount = 0;

int main(void)
{
  
   // Print total number of objects before creating object.
   cout << "Inital Stage Count: " << Box::getCount() << endl;

   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2

   // Print total number of objects after creating object.
   cout << "Final Stage Count: " << Box::getCount() << endl;

   return 0;
}
When the above code is compiled and executed, it produces following result:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2