Tuesday, 26 November 2013

C++,virtual function and abstract class

C++ Virtual Functions,Abstract Class,Templates,Overloading,Special Member Function

Introduction

This article describes basic concepts of C++ Virtual functions and abstract class

Virtual Functions

  • By default, C++ matches a function call with the correct function definition at compile time.This is called static binding
  • You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding
  • You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific functions
  • The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference, but by the type of object that the reference refers to.
  • a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.
  • A class that declares or inherits a virtual function is called a polymorphic class.
  • You redefine a virtual member function, like any member function, in any derived class. Any member function declared as virtual with same name in derived class is also considered as virtual,irrespective of the parameter type
  • A virtual function cannot be global or static because, by definition, a virtual function is a member function of a base class and relies on a specific object to determine which implementation of the function is called. You can declare a virtual function to be a friend of another class.
  • If a function is declared virtual in its base class, you can still access it directly using the scope resolution (::) operator. In this case, the virtual function call mechanism is suppressed and the function implementation defined in the base class is used.
  • In addition, if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base classes
  • The return type of an overriding virtual function may differ from the return type of the overridden virtual function. The derived class return a pointer or reference to a class which is a direct or indirect base class of type returned by the Base class.
  • You cannot override one virtual function with two or more ambiguous virtual functions. This can happen in a derived class that inherits from two nonvirtual bases that are derived from a virtual base class.
  • As long as ambiguous function is not called,the compiler will not give error
  • The access for a virtual function is specified when it is declared. The access rules for a virtual function are not affected by the access rules for the function that later overrides the virtual functions
  • If a virtual function is called with a pointer or reference to a class object, the type of the class object is not used to determine the access of the virtual function. Instead, the type of the pointer or reference to the class object is used.
  • An abstract class is a class that is designed to be specifically used as a base class. . An abstract class contains at least one pure virtual function
  • You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
  • 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 classes
  • A function declaration cannot have both a pure specifier and a definition.A pure declaration forces a definition in the derived class.
  • 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.
  • Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function.
  • 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.
  • If you declare a base class destructor as virtual, a derived class destructor will override that base class destructor, even though destructors are not inherited.