8/31/09

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed.
Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism:
Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are.
Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives the same message. We can expect close behaviors:
vector < int > vi; vector < string > names;
string name("Bjarne");
vi.push_back( 5 ); // add an integer at the end of the vector
names.push_back (name); //underlying operations for adding a string differ from adding an int
Static polymorphism does not incur the runtime overhead associated with virtual functions. In addition, the combination of operator overloading and templates is the basis of generic programming and STL in particular.

8/30/09

Explain using static class member ..

A class member declared static is a single instance of that member shared by all instances of this class (that's why it is sometimes termed a class variable, as opposed to object variable). This feature has many uses: for instance, in order to create a file lock, one can use a static bool class member. An object trying to access this file has to check first whether the static (i.e., shared) flag is false. If it is, the object turns the flag on and processes the file safely, since other objects will now find that the flag is now on, and hence -- they cannot access the file. When the object processing the file is done, it has to turn off the flag, enabling another object to access it. How is a static member created?
class fileProc {
FILE *p;
static bool isLocked; //only a declaration; see definition below...
public:
bool isLocked () const {return isLocked; }
};

//somewhere outside the class definition:

bool fileProc::isLocked; //definition; initialized to 'false' by default. Note: no 'static' here

8/29/09

Explain keyword friend...

for A class can declare external functions or other classes as friends. Friendship grants full access to all of the grantor's members, even private and protected ones:
void encrypt (string & rep) {/*..*/} //global function

class spy {
public:
static void transmit(const string& rep) { /*..*/}
//...
};

class secret {
friend class spy;//spy can access all members of 'secret'
friend void encrypt(string & rep);//...and so can encrypt
private:
string report;
public:
void scramble() { ::encrypt(report); }
void transmit() const { spy::transmit(report); }
};
Notes about friendship:
A friend declaration exposes implementations details of its class, so it should be used wisely. However, friendship has the advantage of allowing code re-use in a simple manner; in fact, many of the standard functions and overloaded operators are used in standard containers (like string<>) by means of friendship.
Friendship is not inherited, so non-public members of any class derived from secret are not accessible to spy and encrypt.

ITUCU