10.21 Write a program to overload > operator.
Explanation: In the above program, class A has one integer type data member and inA() and outA() are member functions. The function inA() is used to read an integer through the keyboard and outA() is used to display the contents of data variable on the screen. The operator > is overloaded. The comparison of explicit (K) and implicit (J) arguments is performed by the if statement. If J is greater, one is returned otherwise zero. If the return value is one, that is, J is greater in function main(), if block is executed otherwise else block is executed.
10.22 Write a program to overload ! = Operator.
Explanation: The above program is same as the previous one. The only difference is that (! =) not equal to operator is overloaded.
10.23 Write a program to overload & operator. When this operator is used with the object, the program should prompt for inputting a number.
Explanation: In the above program the operator & is overloaded. J and K are objects of class A. The equation K=J&K calls the overloaded operator. The overloaded function reads integer and assigns to member variables of objects J and K. The return value is assigned to object K. The outA() function displays the contents of member variable of calling object.
10.24 Write a program to overload < and > operator.
Explanation: In the above program the operator < and > are overloaded. The > operator is used for reading numbers and < is used for displaying numbers. The output of the program is shown above.
10.25 Write a program to add and multiply two complex objects. Use operator overloading.
Explanation: In the above program, the complex class has two data members of double data types. They are real and imaginary. The function setdata() is used to set values of class members. The function show() displays the contents of the object on the screen. The operators + and * are overloaded and defined two add and multiply two complex objects. In function main(), H, I, J, and K are four objects of the class complex. The objects H and I are initialized using the setdata() function. The statements J=H+I and K=H*I do the addition and multiplication of two objects by using the overloaded operator. The show() function displays the contents of object.
10.26 Write a program to declare matrix class and perform addition of matrix class objects.
Explanation: In the above program, the class matrix has one array element num . The objects x, y, and z are of matrix type. The objects x and y are initialized. The addition (+) operator is overloaded and it performs addition of elements of two matrix objects and assigns the sum to the third object. The function show() display the contents of the objects. The statement z=x+y executes the overloaded operator.
10.27 Write a program to overload ++ operator in postfix style.
Explanation: In the above program, the operator ++ is overloaded in suffix and postfix fashion. The difference in the two declaration statements of overloaded operator is the int in the parenthesis in second statement. It tells the compiler to make postfix version of operator (++). In function main(), objects A, B, and C are of postfix type. The objects A, B, and C are initialized to zero by the constructor.
The statement B=A++ assigns value of A to B and then increments object A. The value displayed for B is zero and A is one. The statement C=++A increments object A first and then assigns the value to C. The value of A before increment is one. The value displayed for C is two. The function show() is used to display the contents of the object.
10.28 Write a program to convert text to integer and integer to text.
Explanation: In the above program, constructor with one argument converts the integer to text (user-defined data type). The constructor is executed when an object of text class is created with one parameter. The itoa() function converts integer value to string. For conversion of text to integer, overloaded case operator is used. This operator revives object of text class and converts it to the integer type value.
10.29 Write a program to convert square to square root and vice versa.
Explanation: In the above program two classes square and root are defined. The conversion routines are defined in class root. The statement r=s; uses one-argument constructor for conversion, and the statement s=r; invokes the operator function for conversion.