Skip to main content

Posts

Showing posts from August, 2009

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

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 defau

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 a

Explain Mutable Object Member or data member

When a data member is declared mutable, then it is legal to assign a value to it from a const member function. The following example may demonstrate when it is needed: class Buffer { void * data; //raw data buffer to be transmitted on the net size_t size; //size of the data mutable int crc; //used to verify that no errors occurred during transmission; //a mutable variable is allowed to be modified from a const //member function. public: //... int GetCrc() const; void Transmit() const; //copmutation of crc_val should be done here }; void f() { Buffer buffer; //...fill buffer with data buffer.Transmit(); //crc can be modified here; other members may not } Class buffer uses a data buffer transmitted via communication link after it has been filled. The filling process is quite slow and repetitive, so there's no point in calculating the value of crc every time a few bytes are appended to data. On the other hand, the receiving size has to get the right crc of the data in order

Explain Calling an object's member function from its constructor

An object's member functions (both virtual and non-virtual) can be called from its constructor. The invoked virtual is guaranteed to be the one defined in the current object (or higher, if it has not been overridden in the current object). However, virtuals of objects derived from the one whose constructor is being executed are not called. class A { public: virtual void f() {} virtual void g() {} }; class B: public A { public: void f () {} //overriding A::f() B() { f(); //calls B::f() g(); //g() not overriden in B, therefore calling A::g() } }; Mind that if the object's member functions use object data members, it is the implementor's responsibility to initialize them first, preferably by a mem-initializer list: class C { int n; int getn() const { cout< public: C(int j) : n(j) { getn(); } //Fine: n initialized before getn() //is called; otherwise - n would //have an undefined value };

explain Static member function

A static member function in a class can access only other static members of a class or variables which are not members of its class. It can be invoked even without an object instance, unlike any other member functions: class stat{ int num; public: stat(int n = 0) {num=n;} static void print() {cout <<"static member function" < }; void main() { stat::print(); //no object instance required stat s(1); s.print(); //but still can be called from an object }//end main Static members are used when all other data members of an object are also static; when the function does not depend on any other object member (like print() above); or simply when a global function is undesirable so it is wrapped in a class.

When is virtual inheritance needed?

Multiple inheritance is a powerful and useful feature in C++, but it can lead to a problem known as the DDD or "Deadly Diamond of Derivation", as in this case: class ElectricAppliance{ int voltage, int Hertz ; public: //...constructor and other useful methods int getVoltage () const { return voltage; } int getHertz() const {return Hertz; } }; class Radio : public ElectricAppliance {...}; class Tape : public ElectricAppliance {...}; class RadioTape: public Radio, public Tape { //multiple inheritance //... }; void main() { RadioTape rt; int voltage = rt.getVoltage(); //compilation Error -- ambiguous //call; //two copies getVoltage() exist in rt: //one from Radio and one from Tape. //Also: which voltage value should be //returned? }//end main() The problem is clear: rt is derived simultaneously from two base classes, each having its own copy of the methods and data members of ElecctricAppliance. As a result, rt has two copies of ElectricAppliance. This is the DDD. However, givin

IsA or HasA?

When designing a class hierarchy, you may face a decision between inheritance (aka IsA ) vs. containment (aka HasA) relation. For instance, if you are designing a Radio class, and you already have the following classes implemented for you in some library: Dial, ElectricAppliance. It is quite obvious that your Radio should be derived from ElectricAppliance. However, it is not so obvious that Radio should also be derived from Dial . How to decide? You can check whether there is always a 1:1 relation between the two, e.g., do all radios have one and only one dial? You may realize that the answer is “no”: a radio can have no dial at all (a transmitter/receiver adjusted to a fixed frequency) or may have more than one (both an FM dial and AM dial). Hence, your Radio class should be designed to have Dial(s) instead of being derived from it. Note that the relation between Radio and ElectricAppliance is always 1:1 and corroborates the decision to derive Radio from ElectricAppliance

Why qsort is Still Useful in C++

C++ defines a set of generic algorithms such as sort and find. However, the corresponding C algorithms, qsort and bsearch, are still useful in C++ programs for at least three reasons: • Legacy code. Familiarity with C algorithms is needed to maintain legacy C code. • Efficiency. You cannot apply STL algorithms to items that are not stored in an STL container. To apply these algorithms to a built-in array, you first have to copy it into a container--an operation that incurs runtime overhead. • Applicability to non-OO data types. STL algorithms rely on operators == and >. However, these operators are either meaningless or not defined when applied to plain structs or built-in arrays. C algorithms do not rely on these operators to work.

What is Early Binding and Dynamic Binding?

The term binding refers to the connection between a function call and the actual code executed as a result of the call. Early Binding: If which function is to be called is known at the compile-time it is known as static or early binding. Dynamic Binding: If which function is to be called is decided at run time it is called as late or dynamic binding. Dynamic binding is so called because the actual function called at run-time depends on the contents of the pointer. For example, call to virtual functions, call to functions to be linked from dlls use late binding.

What problem does the namespace feature solve?

Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library's external declarations with a unique namespace that eliminates the potential for those collisions.

How I can make sure that a single instance of my app should be executing at a time?

Answers: Create one Mutex object with a unique name. Since Mutex is a Kernel object that can be identified uniquely and will be availble for all applications to check. ex. hMutex = CreateMutex ( NULL, TRUE, _T("UniqMutexName") ); When application is launched first time above mutex will be created. But if it is tried to launched second time. it will try to create another Mutex with same name used for creating mutex object at the time of first launch of application. Thus it will fails to create the mutex object and application can return from further launching.