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.

Example:

class breadth;

class length

{

private:

int data;

......

public:

......

friend int add(length, breadth); //friend function declaration

};

class breadth

{

private:

int data;

......

public:

......

friend int add(length, breadth); //friend function declaration

};

int add(length l, breadth b) {

int r;

r=l.data+b.data;

return r;

}

The whole class can be made a friend of another class as follows.

class A;

class B

{

private:

int data;

......

public:

......

friend class A; //Friend class declaration

};

Now all the functions of class B can access private members of class A.


Operator Overloading

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:

return_type operator operator_symbol (parameters)

{

statements;

}

Example:

complex operator + (complex c1,complex c2)

{

return complex(c1.real+c2.real,c1.imag+c2.imag);

}

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

complex cnum1,cnum2,cnum3;

//...

cnum3=cnum1+cnum2;

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.

class test

{

private:

int data;

......

public:

......

test operator + (test t);

};

test test::operator + (test t)

{

//Addition code.

}

The usage is similar to the one when the operator function is a non-member as follows.

test t1,t2,t3;

//...

t3=t1+t2;


Exercises

  1. Write a class for instantiating the objects that represent the two-dimensional Cartesian coordinate system.

A. Make a particular member function of one class as a friend function of another class for addition.

B. Make the other three functions to work as a bridge between the classes for multiplication, division, and subtraction.

C. Also write a small program to demonstrate that all the member functions of one class are the friend functions of another class if the former class is made friend to the latter.

Make least possible classes to demonstrate all the above in a single program without conflict.

  1. 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).

  2. 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.

  3. 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.