What is operator overloading?
The mechanism of giving special meaning to an operator is known as operator overloading.
Why is it necessary to overload an operator?
We can almost create a new language of our own by the creative use of the function and operator overloading techniques.
What is an operator function? Describe the syntax of an operator function.
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
How many arguments are required in the definition of an overloaded unary operator?
No arguments are required.
A class alpha has a constructor as follows:
alpha(int a, double b);
Can we use this constructor to convert types?
No. The constructors used for the type conversion take a single argument whose type is to be converted.
What is a conversion function How is it created Explain its syntax.
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.
Operator type name ( )
A friend function cannot be used to overload the assignment operator =. Explain why?
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.
When is a friend function compulsory? Give an example.
When we need to use two different types of operands for a binary operator, then we must use friend function.
A = B + 2;
A = B * 2;
But A = 2 + B
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.
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?
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.
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.
- (a) FALSE
- (b) TRUE
- (c) FALSE
- (d) FALSE
- (e) TRUE
- (f) FALSE
- (g) TRUE
- (h) TRUE