Saturday, February 2, 2013

Pure Virtual Function and Abstract Class


Pure virtual function:
A virtual function that does not have its own body and defined as empty function is known as pure virtual function.

                                    virtual void function_name()=0;

A pure virtual function is a function declared in a base class that has no definition. The compiler requires derived class to either define the function or re-declare it.
A class containing pure virtual function cannot be used to declare any object of its own class.


Abstract Class:
It acts as expression of general concept from which more specific classes can be derived. We cannot create an object of an abstract class type but we can use pointer and reference to abstract class type.
A class that contains at least one pure virtual function is called as abstract class. And classes derived from an abstract class must implement the pure virtual function.

For ex:

class  account
{
 private:
  int balance;
 public:
  void getBalance();
  virtual void printBalance()=0;
}

0 comments:

Post a Comment

Powered by Blogger.