1. 1. Differentiate persistent & non-persistent objects?

    Persistent refers to an object's ability to transcend time or space. A persistent object stores/saves its state in a permanent storage system with out losing the information represented by the object.

    A non-persistent object is said to be transient or ephemeral. By default objects are considered as non-persistent.

  2. How can a '::' operator be used as unary operator?

    Answer:

    The scope operator can be used to refer to members of the global namespace. Because the global namespace doesn’t have a name, the notation :: member-name refers to a member of the global namespace. This can be useful for referring to members of global namespace whose names have been hidden by names declared in nested local scope. Unless we specify to the compiler in which namespace to search for a declaration, the compiler simple searches the current scope, and any scopes in which the current scope is nested, to find the declaration for the name.

  3. Describe the main characteristics of static functions.

    Answer:

    The main characteristics of static functions include,

    • It is without the a this pointer,
    • It can't directly access the non-static members of its class
    • It can't be declared const, volatile or virtual.
    • It doesn't need to be invoked through an object of its class, although for convenience, it may.
  4. What is name mangling?

    Answer:

    Name mangling is the process through which your c++ compilers give each function in your program a unique name. In C++, all programs have at-least a few functions with the same name. Name mangling is a concession to the fact that linker always insists on all function names being unique.

    Example:

    In general, member names are made unique by concatenating the name of the member with that of the class e.g. given the declaration:

    class Bar

    {

    public:

    int ival;

    ...

    };

    ival becomes something like:

    // a possible member name mangling

    ival__3Bar

    Consider this derivation:

    class Foo : public Bar

    {

    public:

    int ival;

    ...

    }

    The internal representation of a Foo object is the concatenation of its base and derived class members.

    // Pseudo C++ code

    // Internal representation of Foo

    class Foo

    {

    public:

    int ival__3Bar;

    int ival__3Foo;

    ...

    };

    Unambiguous access of either ival members is achieved through name mangling. Member functions, because they can be overloaded, require an extensive mangling to provide each with a unique name. Here the compiler generates the same name for the two overloaded instances(Their argument lists make their instances unique).

  5. What is slicing?

    Answer:

    Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object.

    Explanation:

    Consider the following class declaration:

    class base

    {

    ...

    base& operator =(const base&);

    base (const base&);

    }

    void fun( )

    {

    base e=m;

    e=m;

    }

    As base copy functions don't know anything about the derived only the base part of the derived is copied. This is commonly referred to as slicing. One reason to pass objects of classes in a hierarchy is to avoid slicing. Other reasons are to preserve polymorphic behavior and to gain efficiency.

  6. What is a smart pointer?

    Answer:

    A smart pointer is an object that acts, looks and feels like a normal pointer but offers more functionality. In C++, smart pointers are implemented as template classes that encapsulate a pointer and override standard pointer operators. They have a number of advantages over regular pointers. They are guaranteed to be initialized as either null pointers or pointers to a heap object. Indirection through a null pointer is checked. No delete is ever necessary. Objects are automatically freed when the last pointer to them has gone away. One significant problem with these smart pointers is that unlike regular pointers, they don't respect inheritance. Smart pointers are unattractive for polymorphic code. Given below is an example for the implementation of smart pointers.

    Example:

    template

    class smart_pointer

    {

    public:

    smart_pointer(); // makes a null pointer

    smart_pointer(const X& x) // makes pointer to copy of x

    X& operator *( );

    const X& operator*( ) const;

    X* operator->() const;

    smart_pointer(const smart_pointer &);

    const smart_pointer & operator =(const smart_pointer&);

    ~smart_pointer();

    private:

    //...

    };

    This class implement a smart pointer to an object of type X. The object itself is located on the heap. Here is how to use it:

    smart_pointer p= employee("Harris",1333);

    Like other overloaded operators, p will behave like a regular pointer,

    cout<<*p;

    p->raise_salary(0.5);

  7. Name the operators that cannot be overloaded.

    Answer:

    sizeof . .* .-> :: ?:
  8. Name some pure object oriented languages.

    Ans:- Smalltalk, Java, Eiffel
  9. What is an adaptor class or Wrapper class?

    Answer:

    A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non- object- oriented implementation.

  10. What is a dangling pointer?

    Answer:

    A dangling pointer arises when you use the address of an object after its lifetime is over.

    This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

  11. What is an Iterator class?

    Answer:

    A class that is used to traverse through the objects maintained by a container class. There are five categories of iterators:

    • input iterators,
    • output iterators,
    • forward iterators,
    • bidirectional iterators,
    • random access.

    An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class.

    The simplest and safest iterators are those that permit read-only access to the contents of a container class. The following code fragment shows how an iterator might appear in code:

    cont_iter:=new cont_iterator();

    x:=cont_iter.next();

    while x/=none do

    ...

    s(x);

    ...

    x:=cont_iter.next();

    end;

    In this example, cont_iter is the name of the iterator. It is created on the first line by instantiation of cont_iterator class, an iterator class defined to iterate over some container class, cont. Succesive elements from the container are carried to x. The loop terminates when x is bound to some empty value. (Here, none)In the middle of the loop, there is s(x) an operation on x, the current element from the container. The next element of the container is obtained at the bottom of the loop.

  12. What is the use of ‘using’ declaration.

    Answer:

    A using declaration makes it possible to use a name from a namespace without the scope operator.

  13. Define namespace.

    Answer:

    It is a feature in c++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.