While working with objects in Object Oriented Paradigm, we often want to implement user defined functionalities. C++ gives us the facility to define the use of the already existing operators by our own choice. One such operator is the assignment or equality operator ( = ). It is used for transferring the contents of one object into another object. It should be defined by the user in his class as a good programming approach and to ensure smooth execution of the program. There are some important things to be kept in mind while overloading assignment operator, described here.
File handling is an important feature in C++. File handling has a basic purpose to input and output information from a source or to a destination. What file handling basically assists in is that it saves efforts to input a large amount of data every time you run a program. File handling consists of reading and writing with any type of file, but in this post, we will keep ourselves to text files with .txt extension in Windows.
C allows almost all the operators to be overloaded both as member and non-member functions. Up till now we have overloaded all the operators as member functions. Today we shall overload addition operator for our class as a non-member function. We shall also look at the advantages and disadvantages of declaring an overloaded function for the operator as a non-member function.
There are various types of operators in C++. In our recent posts, we have covered overloading of a bunch of arithmetic operators. There are some arithmetic operators which perform compound functions. It means they handle more than one mathematical function at a time. The Plus Equal or Addition Assignment Operator or plus equals to operator (+=) is one of these. As the name implies, it adds and assigns the value to the variable at the same time. The plus equal operator is quite often used in programming. We shall learn how to overload it.