Saturday, 2 October 2010

ABSTRACT CLASS



ABSTRACT CLASS
1)      An abstract class contains at least one Pure Virtual Function. Abstract classes are designed to be specifically used as base classes. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
The following is an example of an abstract class:
class AB {
public:
  virtual void f() = 0;   // A Pure Virtual Function
};
Function AB::f is a pure virtual function. A function declaration cannot have both a pure specifier and a definition. For example, the compiler will not allow the following:
struct A {
  virtual void g() { } = 0;   //Error. Pure Virtual Function cannot have body
};
2)      It is not possible to create objects of abstract classes. You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class. You can, however, declare pointers and references to an abstract class. The following example demonstrates this:
struct A {
  virtual void f() = 0;
};
struct B : A {
  virtual void f() { }
};

A g();  //Error: Class A is an abstract class
void h(A);  //Error: Class A is an abstract class
A& i(A&);    //Not an Error,

int main()
{
    A a;   // Error: Class A is an abstract class
    A* pa;   //Not an error
    B b;
    static_cast <A> (b);  // Error: Class A is an abstract class
}

Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A), declaration of object a, nor the static cast of b to type A.
3)      Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.
For example:
class AB {
public:
  virtual void f() = 0;
};

class D2 : public AB {
  void g();
};

int main() {
  D2 d;     //Error since D2 did not override the Pure Virtual Function g()
}
The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g().
4)      Note that you can derive an abstract class from a non-abstract class, and you can override a non-pure virtual function with a Pure Virtual Function.

5)      You can call member functions from a constructor or destructor of an abstract class. However, the results of calling (directly or indirectly) a Pure Virtual Function from its constructor are undefined. The following example demonstrates this:
class A {
  A() {
    direct();
    indirect();
  }
  virtual void direct() = 0;
  virtual void indirect() { direct(); }
};
The constructor of A calls the pure virtual function direct() both directly and indirectly (through indirect()).
The compiler issues a warning for the direct call to the pure virtual function, but not for the indirect call.

Pure Virtual Function

No comments:

Post a Comment