Programming concepts – A Brushing up I

posted in: C, C++, Java, Programming Concepts | 0

Lets see some important and interesting programming concepts needed  to be known in the coding environment.

Virtual functions: Any function with the keyword “virtual” in the declaration is a virtual function

e.g  virtual int sum();

sum() is a virtual function.

Pure virtual functions: It is any virtual function when added with a expression ” = 0 ” in the declaration.

( e.g ) class example
        {
           public :

             virtual void add() = 0;

         };

add() is a pure virtual function.

Upcasting: When an address of the derived class object is stored in the base class pointer, then it is called upcasting.

Object slicing: When an derived class object is assigned to a object of the base class, the compiler accepts it readily.But it slices off the derived class portion and copies only the base class portion.This is the reason why up casting of object is always prevented.

what is the difference between iterator and iteration?                                                                      An iterator is an object that just moves through the container( like array, hash) to access each element stored in the container.

The process of accessing element in the container one after the other is called iteration.

Friend function: A function which is global and can access all the data members of a class is called a friend function. A function is made a friend function to access the private data of a class from non member function and to increase the functionality of the operators which are overloaded.

Name mangling: The compiler provides different names to different versions of the overloaded function.

(e.g) void add( int i)
      {
      }
      void add (float j)
      {
      }
      void add ( int i, float j)
      {
      }

now the compiler changes the name as follows

add( int i )--- ?$$@@ZAHN@z(int i)
add( float j )--- ?$$@@ZAHR@z(float j)
add( int i )--- ?$$@@ZAHNR@z(int i,float j)

See the changes in bold

Advantages of inline over macro: In the case of inline function,the compiler decides whether to insert a code or not depending on the size of code and the compiler’s optimisation settings. But that is not possible in the case of Macro’s.It always gets expanded.

Memory leak:  Sometimes memory may be allocated dynamically and the allocated memory may be present too but then the way to reach the memory is lost. This scenario is called memory leak.

Dangling Pointers: pointers are variables that store address of other variables or functions. At times when a chunk of memory is allocated and the address of this memory is given to the pointer.But even after the memory is freed and the pointer points to the already freed location, then such a pointer is called dangling pointer.

Leave a Reply