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 .
Posts Tagged ‘oop’
We have been discussing templates in detail for over two weeks. First we explored function templates with a single parameter, then with multiple parameters and also learnt how to specialize template functions. Currently we are exploring class templates. Today we shall see how we can specialize class templates for a particular data type. They are specialized like function templates and behave identically as well but they are declared with a different syntax.
C++ is a very powerful and fast language. One potential danger because of this is the ability of language to call methods and convert data into other types automatically without asking the programmer. So the programmer has an added responsibility of keeping an eye on the compiler and watch out for all such errors. One such common problem is the implicit conversion through a constructor. To avoid that, we use an explicit constructor in C++. Let us see a sample code for that and then I shall explain to you what actually happens.
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++.