What is difference between constructor and member function | By Example

(Last Updated On: March 14, 2018)

Answer: Here is the difference between constructor and member function in C++ programming.

  • Constructor name must be same as class name but functions cannot have same name as class name.
    C++ Code Example :
  • Constructor does not have return type whereas functions must have.
    C++ Code Example :
  • Member function can be virtual, but, there is no concept of virtual-constructor in C++. ( NOTE: virtual destructor to maintain destructor call order in inheritance is available in C++ language)
    C++ Code Example :
  • Constructors are invoked at the time of object creation automatically and cannot be called explicitly but functions are called explicitly using class objects.
    C++ Code Example :