Sunday 27 December 2020

Friend functions & Unary Operator Overloading.

 

              Friend functions & Unary Operator Overloading

Objectives:

  To familiarize students with friend function, overloading increment and decrement operators.  

Tools:

Turbo C++ IDE 


friend function c++

Procedure: 

Writing C++ language program to perform the following tasks
       Displaying value of private data variable calling friend function
       To overload increment (++) unary operator

Friend function:

 The friend functions of a class have access to the private data members of class. For example, you can see picture of an instructor in class during lecture and this public interface, but you don’t know what is inside his mind which is private. The instructor has access to his own mind and feelings. But you do not have access to that. Any human being who has access to our mind and feelings is known as friend. Normally other people don’t know about our thoughts. Only friends know about it. Friends have access to the inner thoughts and have inner knowledge of a friend. Similarly, we keep the data members private and declare some specific functions that are not member of the class but friend of class. As friends, they have access to the inside data structure of the class despite not being members.

To declare a friend function – we can put it anywhere in class. As friend functions are not member of the class so their definition will be always outside the class. However, the prototype of the function will be written in the class. We use the keyword ‘friend’ before the prototype of the function as follow:

 friend return_type friend_function_name(int, char);


A function cannot declare itself friend of a class. Rather, a class has to declare itself that a function is friend of the class or not. So the class declares a friend function. These functions cannot declare themselves friend of a class from outside. Once, the friend functions are declared and the class is compiled, no one from outside cannot make his function friend of your class. Outside functions can only view the interface of the class. If we have a class, suppose ‘Date’ and want to declare a friend function of this class. In the definition of the class, we will write the friend function’s prototype with the keyword ‘friend’. To access the private data, friend function will need the object and usually in the parameter list of friend function, we provide the object of that class. As the friend function is not affected by the private or public keyword, so we can declare it anywhere inside the class definition. We can declare the friend functions at the top of the class definition as well followed by the private data and public data. We can also make a header file of the class definition and implementation in the other file. The member functions are defined in the implementation file and compiled to get an object file. 

Unary operator:

 Unary operators are the ones that require only one operator to work. Unary operators are applied to the left of the operand and take one argument like i++ or i--(Post Increment or post decrement operators for integers) or ++i, --i (Pre increment or pre decrement operator). The syntax of unary operator is as follows:

Class_name operator ++ ( );   // pre increment operator

Operator is a keyword used to overload an operator. When compiler finds ++ in main function, it will check data type of variable, if data type is of user-defined class then it will call operator overloading function and will increment value of a variable otherwise will consider ++ as belonging to a built-in data type.
Step 1: Create New C++ Program File and save it as lab8.cpp
Step 2: Write the following code as shown in figures below

Friend functions & Unary Operator Overloading

Figure 24 (a): friend function
Friend functions & Unary Operator Overloading

Figure 24 (b): friend function
Friend functions & Unary Operator Overloading

Figure 24 (c): friend function
Step 3: Save file again, compile and run it for required output as shown in figure below
Friend functions & Unary Operator Overloading

Figure 24 (d): friend function
Step 4: Over write lab7.cpp with the following code as shown below in figures
Friend functions & Unary Operator Overloading

Figure 25 (a): Unary operator overloading
Friend functions & Unary Operator Overloading

Figure 25 (b): Unary operator overloading
Friend functions & Unary Operator Overloading

Figure 25 (c): Unary operator overloading
Step 5: Save file again, compile and run it for required output as shown in figure below
Friend functions & Unary Operator Overloading

Figure 25 (d): Unary operator overloading
Try These Task(s) Your Self: Write C++ language program to perform following task
       Imagine there is a function to operate on objects of two different classes. The function will take objects to the two classes as arguments, and will did addition operation on their private data using friend function.

       Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and dB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DB object, depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeter depending on the object on display.

       Overload the unary operator i.e. --. A default constructor and an overloaded constructor should be used to initialize values of member data. Define an insert function to receive values from user. Finally define an operator overloading function to increment the member data of that object and a function to display the values of member data. 

No comments:

Post a Comment