Answer Posted / ritesh pal
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.
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is an object in c++?
How do c++ struct differs from the c++ class?
How one would use switch in a program?
Differentiate between a copy constructor and an overloaded assignment operator.
Is c++ vector dynamic?
How do you invoke a base member function from a derived class in which you’ve overridden that function?
In which header file does one find isalpha() a) conio.h b) stdio.h c) ctype.h
Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers
If a function doesn’t return a value, how do you declare the function?
the maximum length of a character constant can be a) 2 b) 1 c) 8
Are iterators pointers?
What is the use of "new" operator?
Write my own zero-argument manipulator that should work same as hex?
What is enum class in c++?
Is C++ case sensitive a) False b) Depends on implementation c) True