[#12] – Operator Overloading and Type Conversion – Questions

C-plus-plus-programming-questions-techhyme

Like function overloading, C++ also support a powerful concept called operator overloading. C++ contains a rich set of operators such as +,-,*,>>,<,++ etc which work on built-in types such as int, float, char so as to perform arithmetic, relational, logical and various other operations. Let us consider the statement c = a + b;. Here the + operator can be used to perform add operation on operands a and b which can either be of int, float, double types etc. Thus, the + operator is performing multiple tasks as it can perform addition of any of the built in types and hence already overloaded.

Index:

The compiler has prior knowledge about how to perform this add operation on built-in types. However if a,b,c are variables (objects) of user-defined type (class) then the statement c = a + b; will not work as compiler doesn’t understand how to add variables (objects) of user-defined type (class). The compiler will give us an error if addition of two objects is carried out. So in order to make the above operation work for the variables of user-defined type also, we need to overload the + operator explicitly by making a function to perform the desired task.

This property of giving additional meaning to existing operators so that they can work with variables of user-defined types is called operator overloading.

Key Points To Remember –

  1. Giving additional meaning to existing C++ operators so that can work with variables of user-defined types is called operator overloading . Operator overloading does not change the original meaning of operator but it simply extends the functionality of the operator.
  2. Operator overloading increases the readability, understandability, extends the functionality of existing operators and make debugging of the programs easier. The properties like syntax, precedence associativity remain the same even after overloading the operators.
  3. An operator is overloaded using the keyword operator followed by an operator symbol. An operator function defines the operation that overloaded operator will perform when used with objects of a relative class.
  4. An operator function can be member function of a class or a non-member function of a class (friend function). The operators that can be overloaded are either be unary or binary operators. Overloading an operator as a non-member (friend function) always require one more arguments to pass explicitly as compared to overloading the same operator as a member of the class.
    Overloaded operator function Type of Operator Number of argument(s) passed explicitly in operator function
    member Unary
    Binary
    No-argument
    1-argument
    non-member Unary
    Binary
    1-argument
    2-arguments
  5. The following are the limitations of operator overloading.
    • New operators cannot be created for overloading.
    • All operators cannot be overloaded.
    • The syntax, precedence or associativity of the operators cannot be changed while overloading an operator.
    • All the member function of a class can be defined by overloading various operators even though it may lead to poor readability and understandability of program code. If the meaning of an operation to be performed by overloading an operator is unpredictable or doubtful to the user, it is advisable to use function with appropriate name.
    • You cannot redefine the operators for basic types. This means you cannot do something ridiculous such as redefine ‘-‘ to perform addition of integer values.
    • The operations predefined for primitive data types are not automatically transferred to user-defined types.
    • Overloaded operator functions cannot have default arguments.
    • While performing data conversion function performing the same operation is not allowed as compiler cannot distinguish which one to use.
    • Data conversion involves converting data from one type to another. Data conversion between basic data types is performed implicitly by the compiler. But explicitly data conversion is required in the following cases.
      • Conversion from basic to user-defined data type (class type) – This is performed using single parameter constructor in the class.
      • Conversion from class type to basic data type – This is performed by overloading the typecast operator conversion function in the class.
      • Conversion from one class type to another class type – This is performed either by defining a single parameter constructor in the destination object’s class or by defining an overloaded typecast operator conversion function in the source object’s class.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. Which operators cannot be overloaded ?
    • Dot operator(.), .* , conditional operator (?;), scope resolution operator(::), sizeof operator.
  2. What are different ways of overloading an operator ?
    • An overloaded operator function can be a member function of the class or a non-member (friend) of the class.
  3. How many arguments do you need to pass while overloading an operator ?
    • While overloading an unary operator member function, there is no need to pass any argument to the overloaded function, whereas a binary operator overloaded member function requires one argument to pass, Thus overloading an operator as a member function of the class always require one less argument to that of its operands. However, to overloaded an operator using non member (friend function) of the class, the unary overloaded operator function requires one arguments to pass. Thus, overloading an operator using friend always requires arguments equal to the number of operands used with operator.
  4. How can you distinguish between a prefix and postfix increment operator while overloading ?
    • They are distinguished by dummy int-parameter. The prefix version of overloaded increment operator takes no parameter whereas the postfix version takes a dummy (unused) parameter of type int. So, declaration of prefix overloaded operator function is
      return_ type operator ++(int);
      and declaration of overloaded postfix operator function is,
      return_ type operator ++(int);
  5. Is it possible to change the precedence of operators using operator overloading ?
    • No
  6. If c1 and c2 of two objects of class complex then will the statement c2=5, 0+c1; work by overloading operator + which is a member function of class complex ?
    • The statement,
      c2=5. 0+c1;
      will not work although we overload ‘+’ operator as a member function. This happens because the left operand of the ‘+’ operator is not the object of class complex but a value of float type. So, there is no way to generate a call to the overloaded operator ‘+()’ member function.
      To overcome this problem, overloaded ‘+’ operate using friend function.
  7. Will the following statement work ?
    t1=10;
    where t1 is an object of class time ?

    • It will work only if we have an one argument constructor conversion function in the class time.
  8. Will the following statement work?
    int min=t1;
    where t1
    is an object of class time ?

    • It will work only if we overload the int type cast operator in the class time.
  9. If objX and objY are two objects of different classes X and Y respectively then how this statement ,
    objX=objY, works ?

    • In order to execute the statement objx = objy; we need to make the conversion routine either in the source class (class Y) or in the destination class (class X) . If the conversion routine is defined in the source class then we use overloaded cast operator function and if it is defined in the destination class then we use one-argument constructor or overloaded equal to ‘=’ operator function.

Other Similar Questions – 

  1. What is operator overloading ?
  2. What is the need of overloading an operator.
  3. Define operator function with proper syntax and examples.
  4. List the operators that can be overloaded and one that cannot be overloaded . Give reasons why some operators cannot be overloaded .
  5. Using an example, explain the concept of overloading a unary operator.
  6. What are the limitations of overloading the unary increment and decrement operators. How can it be removed.
  7. How will you overload a binary operator.
  8. Why do we pass only one argument while overloading binary operators.
  9. When do we need friend function for overloading an operator. Explain with suitable example.
  10. What are the pitfalls of operator overloading.
  11. What is a conversion function ?
  12. Explain the concept of implicit type conversion using an appropriate example.
  13. What is the need of conversion function in operator overloading ?
  14. How will you perform a conversion from basic to user-defined type and vice-versa ?
  15. How will data conversion be performed
    • if the source object’s class contains the conversion function.
    • If the destination object’s class contains the conversions function.
  16. What is the advantage of overloading subscript [] operator ?
  17. How will you overload a function call () operator ?
  18. Write a program to print Fibonacci series by overloading prefix++ increment operator.
  19. Create a class time and overland the increment (++) operator that operate in both prefix and postfix notation and display the calculated time.
  20. Create a class matrix having data members m and n representing rows and columns of a matrix. Overload the +,- arithmetic operators to perform addition and subtraction of two matrices.
  21. Create a class polynomial. Each term of polynomial is represented as an array which consists of coefficient and exponent. For example the term 4x3 has the coefficient 4 and exponent 3. Overload the + and – arithmetic operators for adding and subtracting objects of type polynomial (polynomial of order n … a0+a1x+a2x2+…+anxn).
  22. Create a fraction class which contains the data member numerator and denominator. Overload the greater than (>) and less than (<) operators on the objects of fraction class.
  23. Overload the unary operator Not(!) to test whether an object of class string is empty or not.
  24. Create a class point having data members x and y which represents coordinates of a point. Using a friend function overload the +=, ==, != operators on the objects of point class.
  25. Create a class weight having data members kg and gram. Overload all the arithmetic operators on the objects of weight class.
  26. Define two classes Fahrenheit and Celsius to represent temperature in Fahrenheit and Celsius respectively. Use conversion function to convert from one system to the other.
  27. Define two classes Degree and Radians to represent angle in degree and radians respectively. Use conversion function to convert from one system to the other.
You may also like:

Related Posts

Leave a Reply