News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Pure Virtual Function - C++ - with Examples

Started by Kalyan, Jun 28, 2008, 06:19 PM

Previous topic - Next topic

Kalyan

Pure Virtual Function - C++

A virtual function body is known as Pure Virtual Function. In above example we can see that the function is base class never gets invoked. In such type of situations we can use pure virtual functions

Example : same example can re-written
class base
{
public:
virtual void show()=0; //pure virtual function
};

class derived1 : public base
{
public:
void show()
{
cout<<"\n Derived 1";
}
};

class derived2 : public base
{
public:
void show()
{
cout<<"\n Derived 2";
}
};

void main()
{
base *b; derived1 d1; derived2 d2;
b = &d1;
b->show();
b = &d2;
b->show();
}


Rules for Virtual Functions

1. The virtual function must be member of class
2. They cannot be static members
3. They are accessed by using object pointers
4. Prototype of base class function & derived class must be same
5. Virtual function in base class must be defined even though it is not used
6. A virtual function can be friend function of another class
7. We could not have virtual constructor
8. If a virtual function is derived in base class, it need not be necessarily redefined in the derived class
9. Pointer object of base class can point to any object of derived class but reverse is not true
10. When a base pointer points to derived class, incrementing & decrementing it will not make it point to the next object of derived class

PURUSHOTHAMAN M

it is very useful for my exam and also know the view of pure virtual function


jamemu

Very helpful to me too. I'm learning about this language, it is hard for beginners like me. Thank you for this material.