© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

15. What is the handle class?


Ans:

A handle is a class that maintains a pointer to an object that is programmatically accessible through the public interface of the handle class.
Explanation:

In case of abstract classes, unless one manipulates the objects of these classes through pointers and references, the benefits of the virtual functions are lost. User code may become dependent on details of implementation classes because an abstract type cannot be allocated statistically or on the stack without its size being known. Using pointers or references implies that the burden of memory management falls on the user. Another limitation of abstract class object is of fixed size. Classes however are used to represent concepts that require varying amounts of storage to implement them.
A popular technique for dealing with these issues is to separate what is used as a single object in two parts: a handle providing the user interface and a representation holding all or most of the object's state. The connection between the handle and the representation is typically a pointer in the handle. Often, handles have a bit more data than the simple representation pointer, but not much more. Hence the layout of the handle is typically stable, even when the representation changes and also that handles are small enough to move around relatively freely so that the user needn’t use the pointers and the references.   

Ex 1: What is an action class?

Ans:
The simplest and most obvious way to specify an action in C++ is to write a function. However, if the action has to be delayed, has to be transmitted 'elsewhere' before being performed, requires its own data, has to be combined with other actions, etc then it often becomes attractive to provide the action in the form of a class that can execute the desired action and provide other services as well. Manipulators used with iostreams is an obvious example.
Explanation:
            A common form of action class is a simple class containing just one virtual function.
         class Action
       {
               public:
                    virtual int do_it( int )=0;
                    virtual ~Action( );
         }
Given this, we can write code say a member that can store actions for later execution without using pointers to functions, without knowing anything about the objects involved, and without even knowing the name of the operation it invokes. For example:
class write_file : public Action
     {
              File& f;
              public:
                  int do_it(int)
                 {
                       return fwrite( ).suceed( );
                 }
      };
     class error_message: public Action
     {
                response_box db(message.cstr( ),"Continue","Cancel","Retry");
                switch (db.getresponse( ))
                {
                        case 0: return 0;
                        case 1: abort();
                        case 2: current_operation.redo( );return 1;
                 }
      };  
 
A user of the Action class will be completely isolated from any knowledge of derived classes such as write_file and error_message.

SLogix Student Projects
bottom