Thus far in this series on object oriented programming in C , we have learnt the basics of classes and objects and have touched upon the private data members and public member functions, including use of getters and setters in C . When dealing with a class, we create different objects and call a number of functions on them. Have you ever wondered: How does the compiler know on which object the function was called and to change the variables of which object inside that function? This is done using this pointer in C .
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++.