Archive for November 29th, 2010

Basics in CPP

Function Abstraction & Data Abstraction:

In functional abstraction, access to the function is provided through a specific interface defined to invoke the function. In contrast, in data abstraction, access to the data is provided through a specific set of operations defined to examine and manipulate the data. For instance, when a programmer is using C++ standard data types, this means that users are using the concept of data abstraction. When using data types, the users are not concerned with how the data is stored but they are concerned with what operations are provided and what properties are supported.

class AB {
public:
virtual void f() = 0;
};

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

int main() {
D2 d;
}
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().
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.

A virtual function must be one of the following:
Defined
Declared pure
Defined and declared pure
A base class containing one or more pure virtual member functions is called an abstract class.
C++ Virtual function – Call Mechanism:
   Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time
   The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.

C++ Virtual function – Example:
   This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.
     class Window // Base class for C++ virtual function example
     {
       public:
          virtual void Create() // virtual function for C++ virtual function example
          {
               cout <<"Base class Window"<
     class CommandButton : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class Command Button – Overridden C++ virtual function"Create();

         y = new CommandButton();
         y->Create();
     }

   The output of the above program will be,
                 Base class Window
                 Derived class Command Button

then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.
The main difference between an abstract base class and a regular polymorphic class is that because in abstract base classes at least one of its members lacks implementation we cannot create instances (objects) of it.

But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like:
 
CPolygon poly;

would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers:
1
2
CPolygon * ppoly1;
CPolygon * ppoly2;

would be perfectly valid.


RSS RSS

Twitter

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Categories

calender

November 2010
M T W T F S S
« Oct   Jan »
1234567
891011121314
15161718192021
22232425262728
2930  

Follow

Get every new post delivered to your Inbox.