Lab Sheet 5
Lab Sheet 5
Understanding the Concept of Friend Function/Class and Operator Overloading
Friend Function and Class
In some cases, we need to access the private data members of a class from non-member functions. In such situations, we must declare the function as a friend function of the class. This friend function seems to violate the data hiding feature of the OOP concept. However, the function that accesses private data must be declared as a friend function within the class. With friend functions, data integrity is still maintained.
Sometimes we may need to make, one or all the member functions of a class friend to another class. For that, we declare a class or member function as a friend to the other class so that one or all the member functions of the declared class can access the private members of the former class.
The whole class can be made a friend of another class as follows.
Now all the functions of class B can access private members of class A.
We have already studied that the user-defined data types that we make behave in much the same way as the built-in types. C++ also permits us to use operators with user-defined types in the same way as they are applied to the basic types. The data items are represented by objects and operations on those objects can be extended (defined) with the already existing operators. The method of extending the operation on user-defined types (objects) is called operator overloading. Operators can be overloaded for different operations. Most programmers implicitly use overloaded operators regularly. For example, the addition operator (+) operates quite differently on integers, float and doubles, and other built-in types because the operator (+) has been overloaded in the C++ language itself and with some library objects such as strings. Operators are overloaded by writing a function definition as we normally do, except that the function name now becomes the keyword operator followed by the symbol for the operator being overloaded. Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators for our class.
The syntax of operator overloading is as follows:
To access the private members of the class complex, this operator function must be declared as a friend in the class complex otherwise there should be a mechanism to return real and imag values of the objects of the complex class.
After overloading the function the operation on objects of the complex class can be done as
The operator function can be defined as a member function of a class or non-member function. The number of parameters required for overloading operators is different for member function and non-member functions. The operator overloading function when defined as the member function has the following form.
The usage is similar to the one when the operator function is a non-member as follows.
Write a class for instantiating the objects that represent the two-dimensional Cartesian coordinate system.
Write a class to store x, y, and z coordinates of a point in three-dimensional space. Overload addition and subtraction operators for addition and subtraction of two coordinate objects. Implement the operator functions as non-member functions (friend operator functions).
Write a program to compare two objects of a class that contains an integer value as its data member. Make overloading functions to overload equality(==), less than(<), greater than(>), not equal (!=), greater than or equal to (>=), and less than or equal to(<=) operators using member operator functions.
Write a class Date that overloads prefix and postfix operators to increase the Date object by one day, while causing appropriate increments to the month and year (use the appropriate condition for leap year). The prefix and postfix operators in the Date class should behave exactly like the built-in increment operators.