Google News
logo
CPP Interview Questions
Following are the differences between reference and pointer :

Reference
Pointer
Reference behaves like an alias for an existing variable, i.e., it is a temporary variable. The pointer is a variable which stores the address of a variable.
Reference variable does not require any indirection operator to access the value. A reference variable can be used directly to access the value. Pointer variable requires an indirection operator to access the value of a variable.
Once the reference variable is assigned, then it cannot be reassigned with different address values. The pointer variable is an independent variable means that it can be reassigned to point to different objects.
A null value cannot be assigned to the reference variable. A null value can be assigned to the reference variable.
It is necessary to initialize the variable at the time of declaration. It is not necessary to initialize the variable at the time of declaration.
* An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
* Array memory allocation is static and continuous while List memory allocation is dynamic and random.
* In Array, users don't need to keep in track of next memory allocation while In the list, the user has to keep in track of next location where memory is allocated.
* new() is a preprocessor while malloc() is a function.

* There is no need to allocate the memory while using "new" but in malloc() you have to use sizeof().

* "new" initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.

* The new() operator allocates the memory and calls the constructor for the object initialization and malloc() function allocates the memory but does not call the constructor for the object initialization.

* The new() operator is faster than the malloc() function as operator is faster than the function.
* A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

* A virtual function is a member function which is present in the base class and redefined by the derived class.

* When we use the same function name in both base and derived class, the function in base class is declared with a keyword virtual.

* When the function is made virtual, then C++ determines at run-time which function is to be called based on the type of the object pointed by the base class pointer. Thus, by making the base class pointer to point different objects, we can execute different versions of the virtual functions.
 
Rules of a virtual function :
 
* The virtual functions should be a member of some class.
* The virtual function cannot be a static member.
* Virtual functions are called by using the object pointer.
* It can be a friend of another class.
* C++ does not contain virtual constructors but can have a virtual destructor.
Stack Unwinding happens during exception handling. During an exception occurrence, the destructor is called to destroy all local objects for the place where the exception was thrown and where it was caught. An exception causes the control to pass on from a try block to the respective exception handler.  The destructors for all constructed automatic objects are called at run time which where created since the beginning of the try block. The automatic objects are destroyed in reverse order of their construction.
 
Note : The corresponding objects must be created before destruction of the same which takes place during Stack Unwinding.
 
The terminate() function is invoked during Stack Unwinding on a destructor for a unhandled exception.
 
 
Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, 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 involves copying the contents of one object 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.
 
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.
The local scope of a variable allows the variable to work inside a restricted block of code. For instance, if it is declared inside a function, you cannot access it from the main function without calling the function whereas the global scope of a variable allows the variable to be accessible throughout the entire program.
 
It is a well-established fact that whenever a program has a local and global variable, precedence is given to the local variable within that program.
In object-oriented programming, the entire program is divided into smaller units called objects and works on the bottom-up approach. Whereas, in procedural programming, the entire program is divided into smaller units called functions and works on the top-down approach.
 
In a nutshell, procedural programming does not provide any data security whereas, in contrast to it, object-oriented programming ensures data security with the help of access specifiers, proving to be more secure and efficient.
 
C++ supports object-oriented programming whereas C supports procedural programming.
Inline Function is nothing but simply a function for which the C++ compiler produces a copy of it every time the function is called during compilation. It is an important feature that rose out from classes and objects in C++. 
 
If any changes are made to an inline function, the section of the code containing the function is again compiled and necessary changes in the copies of the code are made, otherwise, it would continue to work according to the old code.
 
In order to indicate that a function is inline, we use the keyword “inline” in C++.
 
The general syntax for declaring an inline function in C++ is :
inline return_type function_name( arguments )
{
// BODY OF THE FUNCTION
}
While creating and allocating memory to a variable with the help of the “new” keyword, we can deallocate the memory associated with it by the help of the “delete[]” operator whereas, while creating and allocating memory to a non-array object with new, we can deallocate the memory with the help of the “delete” operator.