Constructors and Destructors in C++

Constructors in C++

  1. Constructors are instance member functions and these are the first action which is been performed in lifetime of an object.
  2. Constructors are invoked implicitly at runtime.
  3. Constructors are used to initialize instance variables of an object with an appropriate value.
  4. Constructors do not have any return type , so no need to write void while explicitly defining it.
  5. Constructor overloading is possible.

Destructors in C++

  1. Like constructors destructors are also instance member functions .
  2. It is the last action been performed in the lifetime of an object.
  3. They do not have any return type and they don't take any arguments that's why destructor overloading is not possible due to same signature.
  4. Destructor main purpose is to free the memory resources being handled by instance variables of an object.

Saurabh Shukla

Founder MySirG.com, Teacher and YouTuber (890k+ Subscribers)

2 年

Good Work

要查看或添加评论,请登录

Utkarsh Singh Rajawat的更多文章

  • Static member variables vs Instance member variables in C++

    Static member variables vs Instance member variables in C++

    In a class we can define two kinds of variables : Static member variable Instance member variable Instance member…

  • Memory Leak

    Memory Leak

    Memory leak is a concept through which a reserved memory location can't be accessed and our program is left with :…

    1 条评论
  • This pointer in C++

    This pointer in C++

    this is a local object pointer and also a keyword in C++. Important things regarding this pointer : this pointer helps…

    2 条评论

社区洞察

其他会员也浏览了