Daily Archives: 04/03/2013

A Developer’s Experiences with Constructors And Destructors In C++

The most compelling feature of C++ programming language is it being object oriented. An object is a fundamental aspect of an object oriented language like C++. The objects come from classes which is a collection of related type of information. As in C, we have structures as a user defined data-type, which is a collection of data members of different data-types. On Similar lines, C++ introduces the concept of classes, though with a lot of basic differences like the concept of constructors and destructors etc.

Let’s discuss a few differences here :

  • C structures just have data members, whereas C++ classes, apart from data members, can also have member functions.
  • C structure data members are all publicly accessible, however C++ classes have the power to specify access as private, public or even protected.
  • Another vital difference are constructors and destructors. To initialize objects, including dynamic memory allocation, implicitly in C++, programmers define constructors, and similarly, to destroy an object, including freeing the memory, it uses destructors. In this article, the we shall have an in-depth understanding of the C++ constructors and destructors.

[Note that, in the explanation above, we have discussed about C structures... C structures carry a lot of differences with C++ structures.]

In this article, we will discuss in detail the intricacies of constructors and destructors from a developer’s point of view.

Continue reading