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.
Posts Tagged ‘object oriented programming’
While working with classes, we often want to have the user defined functionalities which help enormously during the rest of the program. When we deal a lot with arrays, we often want to add the contents of two arrays into a single larger array. This could tediously be done by declaring another array of the combined size of the two and adding them one by one in it. The better option to do this is to overload the addition operator ( ) once which saves us doing this process repeatedly if we add two arrays time and again in our program. Let us see how we can do that efficiently.
Just like function templates, we can also make and use class templates in our program. With the help of class templates, a whole class could be made generic. Each function declared within the class is a template function by default. Class templates behave much like the function templates though there is a lot more to be associated and understood with them. Class templates can also be specialized like specializing function templates. For this article, I would explain only how to make a simple class, a template class.
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.