After talking about constructors and destructors, next up in my series on object oriented programming is how to use setters. Setters are functions that are used to change the values of data members inside classes. They are very important, as they are the only route through which we can change the value of our data members after an object is instantiated.
Now that we”ve had a look at the types and uses of constructors, let us move on to the concept and use of destructors in C . Destructors are important in object oriented programming, as they are called whenever the life of an object ends. To understand destructors, first we must know that when dealing with classes, we often encounter a situation where pointers are used. A class with a pointer/pointers as data members are usualy allocated memory in a constructor. As a simple example, let us use a class called “point”, with a redundant data member called int * ptr.
A constructor in a class is a special function called at instantiation of an object, or declaration of an object. What it does is simple: it does everything that you require to be done automatically every time an object of your class is created. This helps us to avoid system crashes and makes sure that the object we create does not always create variable or data that is unacceptable for our program. This will become clearer to you as you read on.
I previously mentioned in Basics of Object Oriented Programming that the main body of any class that we write in our program is usually written in a separate file called .cpp file and its declarations in another header file .h file. This is done in order to avoid clustering of the class definition with the execution body of the program. It makes it pretty easy and efficient to put all the classes in separate files in a large program. This is then included later in a main file and its functions are used in the manner we like. Here is the detailed procedure for it.
This article is the first in a series which will be centered around object oriented programming in C++ for beginners. However, the hope is that programmers of a more intermediate to advanced level who are looking to refresh their basic object oriented programming skills. First we start with an introduction to object oriented programming using C++.