1. When can you tell that a memory leak will occur?

Memory leaks generally occur when a program loses the ability to free a block of dynamically allocated memory because it has no reference to it. Memory leaks leads to wastage of memory.

2.What is a parameterized type?

Template is a parameterized construct or type that contains generic code that can use or manipulate any other type. It is called parameterized because an actual type is a parameter of the code body. Polymorphism may be achieved through parameterized types. This type of polymorphism is called parameteric polymorphism

3. Differentiate between a deep copy and a shallow copy?

Deep copy is a mechanism in which the contents of one object is used to create another instance of the same class. In this approach, the two objects may contain ht same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects.
Shallow copy is a mechanism in which the contents of one object is copied into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable.
Explanation:
For the sake of explanation we may consider the following scenario,
Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a shallow copy, only the addresses of pointers that are members are copied and not the value the address is pointing to. The data values of the object would then be inadvertently altered by the function. When the function goes out of scope, the copy of the object with all its data is popped off the stack.
If the object has any pointers a deep copy needs to be executed. With the deep copy of an object, memory is allocated for the object in free store and the elements pointed to are copied. A deep copy is used for objects that are returned from a function.

4. What is an opaque pointer?

A pointer can be called as opaque if the definition of the type it points to is not included in the current translation unit. A translation unit is the outcome of merging an implementation file with all its headers and header files.

5. What is a smart pointer?

Smart pointer are objects that acts, looks and feels like a normal pointer but offers more functionality as compared to a normal pointer. Smart pointers are implemented as template classes that encapsulate a pointer and override standard pointer operators. They have several advantages over regular pointers. Smart pointer are 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 T>
   class smartPointer
   {
              public:
                   smartPointer();                          // makes a null pointer
                   smartPointer(const T& x)            // makes pointer to copy of x

                   T& operator *( );
                   const T& operator*( ) const;
                   T* operator->() const;

                   smartPointer(const smart_pointer <T> &);
                   const smartPointer<T> & operator =(const smart_pointer<T>&);
                   ~smartPointer();
    };
This class implement a smart pointer to an object of type T. The object itself is located on the heap. Here is how to use it:
            smartPointer<employee> ptr= employee("Santosh",1000);
Like other overloaded operators, ptr will behave like a regular pointer,
cout<<*ptr;
ptr->raise_salary(1.245);

6. What is reflexive association?

'is-a' is called a reflexive association because the reflexive association allows classes to bear the is-a association not only with their super-classes but also with themselves. It 

7.  What is slicing?

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 baseclass
              {
                     ...
                     baseclass & operator =(const baseclass&);
                     baseclass(const baseclass&);
              }
              void function( )
              {
                    baseclass obj1=m;
                    obj1=m;
              }
As baseclass 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. 

8. What is name mangling?

Name mangling is the process used by C++ compilers give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name. Thus name mangling can be considered as an important aspect in C++.
Example:
            Commonly, member names are uniquely generated by concatenating the name of the member with that of the class e.g. given the declaration:
    class Class1
     {
            public:
                int val;
                ...
      };
val becomes something like:
      // a possible member name mangling
     val__11Class1


9. What are proxy objects?

 Proxy objects are the objects that points to other objects. Its an object that provides the same interface as its server object but does not have any functionality.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
10. Differentiate between declaration and definition in C++.

A declaration provides a name to the program; a definition provides a unique description of an entity (e.g. type, instance, and function) within the program. Declarations can be repeated in a given scope, it introduces a name in a given scope. 

A declaration is a definition unless:
    • Declaration declares a function without specifying its body,
    • Declaration contains an extern specifier and no initializer or function body,
    • Declaration is the declaration of a static class data member without a class definition,
    • Declaration is a class name definition,

            A definition is a declaration unless:
    • Definition defines a static class data member,
    • Definition defines a non-inline member function.

1 comments:

  1. thanks a lot man !!! i really need this i never seen some question you write on blog!!!

    ReplyDelete

Ads