Operator Overloading and Type Conversion

How to use Operator Overloading and Type Conversion in CPP/C++
  • Operator overloading provides a flexible option for the creation of new definition for most of the C++ operators.
  • When an operator is overloaded its original meaning is not lost.
      Consider the following example:
Q. what is operator overloading in C++?
Operator overloading means giving different meaning to operators or to overload it. The compiler distinguish between different meanings of the operator by examining the types of its operand e.g. while adding two integers it will give their sum. when adding two strings it will give concatenated string. As the user defined data types/classes are not known to the compiler using the operator overloading is used to make user defined data types to behave like built in data type. Operator overloading is achieved by operator function which tells the compiler the new meaning of the operator wrt the class. For example with operator overloading we can add two objects with the same syntax as in built in integers. If we do not give the operator function for this then it will throw error as compiler is not aware with user defined class and its objects.


Q. Syntax of Operator Function for operator overloading in C++
Return_type class_name :: Operator op_to_ol (argumrnts)
{

Operator function body
}
Q. Which are the operators that cannot be overloaded in C++?(neither by mf or global/friend function)

1. sizeof operator
2. class member access operator(., .*)
3.Conditional operator(:?)
4.Scope Resolution operator


Q. Which are the operators that cannot be overloaded by global/friend function(means operator that can be over loaded as mf only)
1. assignment operator(=)
2.class member access operator(->)
3.subscripting operator []
4.function call operator ()


Q. Operator function must be either member function or friend function.? Answer is Yes. Difference is only the number of argements.
In mf unary oo takes zero arguments and binary oo takes one argument while
In global/friend oo unary oo takes one argument and binary oo takes two arguments.


Q. Compilers does not support automatic type conversion for user defined data type. It is done by casting operator functions.
Q. Basic to class type is converted with the help of constructor or operator overloading? Answer is constructor
Q. Class to basic type is converted with the help of constructor or operator overloading? Answer is operator overloading
Q. Class to class type is converted with the help of constructor or operator overloading? Answer constructor or conversion functions


Coming Soon More Updates.
How to use Operator Overloading and Type Conversion in CPP/C++
How to use Operator Overloading and Type Conversion in CPP/C++
How to use Operator Overloading and Type Conversion in CPP/C++
How to use Operator Overloading and Type Conversion in CPP/C++


 

 

1 comment:

  1. Coming Soon
    Operator Overloading and Type Conversion
    Operator Overloading and Type Conversion
    Operator Overloading and Type Conversion
    Operator Overloading and Type Conversion
    Operator Overloading and Type Conversion Operator Overloading and Type Conversion

    ReplyDelete