IT Acumens Forum for Discussion

Tech News => Programming Discussions for Engineers => Topic started by: Kalyan on Jun 28, 2008, 11:49 PM

Title: Pure Virtual Function - C++ - with Examples
Post by: Kalyan on Jun 28, 2008, 11:49 PM
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
Title: Re: Pure Virtual Function - C++ - with Examples
Post by: PURUSHOTHAMAN M on Oct 20, 2012, 08:48 PM
it is very useful for my exam and also know the view of pure virtual function
Title: Re: Pure Virtual Function - C++ - with Examples
Post by: Garima Bajpai on Nov 22, 2012, 01:13 PM
 :educated
Title: Re: Pure Virtual Function - C++ - with Examples
Post by: jamemu on Sep 18, 2017, 03:15 PM
Very helpful to me too. I'm learning about this language, it is hard for beginners like me. Thank you for this material.