7.1

What is operator overloading?

Answer

The mechanism of giving special meaning to an operator is known as operator overloading.

7.2

Why is it necessary to overload an operator?

Answer

We can almost create a new language of our own by the creative use of the function and operator overloading techniques.

7.3

What is an operator function? Describe the syntax of an operator function.

Answer

To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. By which function this is done, is called operator function.
Syntax of operator function:

return type class name : : operator OP (argument list)
   {
              function body // task defined
    }
7.4

How many arguments are required in the definition of an overloaded unary operator?

Answer

No arguments are required.

7.5

A class alpha has a constructor as follows:
alpha(int a, double b);
Can we use this constructor to convert types?

Answer

No. The constructors used for the type conversion take a single argument whose type is to be converted.

7.6

What is a conversion function How is it created Explain its syntax.

Answer

C++ allows us to define an overloaded casting operator that could be used to convert a class type data to a basic type. This is referred to conversion function.
Syntax:

Operator type name ( )
 {
     (Function Statements)
  }
7.7

A friend function cannot be used to overload the assignment operator =. Explain why?

Answer

A friend function is a non-member function of the class to which it has been defined as friend. Therefore it just uses the functionality (functions and data) of the class. So it does not consist the implementation for that class. That’s why it cannot be used to overload the assignment operator.

7.8

When is a friend function compulsory? Give an example.

Answer

When we need to use two different types of operands for a binary operator, then we must use friend function.
Example:
A = B + 2;
or
A = B * 2;
is valid
But A = 2 + B
or
A = 2 * B will not work.
Because the left hand operand is responsible for invoking the member function. In this case friend function allows both approaches.

7.9

We have two classes X and Y. If a is an object of X and b is an object of Y and we want to say a = b; What type of conversion routine should be used and where?

Answer

We have to use one class to another class type conversion. The type-conversion function to be located in the source class or in the destination class.

7.10

State whether the following statements are TRUE or FALSE.

  • (a) Using the operator overloading concept, we can change the meaning of an operator.
  • (b) Operator overloading works when applied to class objects only.
  • (c) Friend functions cannot be used to overload operators.
  • (d) When using an overloaded binary operator, the left operand is implicitly passed to the member function.
  • (e) The overloaded operator must have at least one operand that is user-defined type.
  • (f)Operator functions never return a value.
  • (g) Through operator overloading, a class type data can be converted to a basic type data.
  • (h) A constructor can be used to convert a basic type to a class type data.
Answer
  • (a) FALSE
  • (b) TRUE
  • (c) FALSE
  • (d) FALSE
  • (e) TRUE
  • (f) FALSE
  • (g) TRUE
  • (h) TRUE

 


Next Previous