![C-plus-plus-programming-questions-techhyme](https://techhyme.com/wp-content/uploads/2021/07/C-plus-plus-programming-questions-techhyme-1024x677.jpg)
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:
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
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 –
- 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.
- 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.
- 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.
- 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
BinaryNo-argument
1-argumentnon-member Unary
Binary1-argument
2-arguments - 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:
- Which operators cannot be overloaded ?
- Dot operator(.), .* , conditional operator (?;), scope resolution operator(::), sizeof operator.
- 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.
- 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.
- 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);
- 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
- Is it possible to change the precedence of operators using operator overloading ?
- No
- 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.
- The statement,
- 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.
- 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.
- 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 –
- What is operator overloading ?
- What is the need of overloading an operator.
- Define operator function with proper syntax and examples.
- List the operators that can be overloaded and one that cannot be overloaded . Give reasons why some operators cannot be overloaded .
- Using an example, explain the concept of overloading a unary operator.
- What are the limitations of overloading the unary increment and decrement operators. How can it be removed.
- How will you overload a binary operator.
- Why do we pass only one argument while overloading binary operators.
- When do we need friend function for overloading an operator. Explain with suitable example.
- What are the pitfalls of operator overloading.
- What is a conversion function ?
- Explain the concept of implicit type conversion using an appropriate example.
- What is the need of conversion function in operator overloading ?
- How will you perform a conversion from basic to user-defined type and vice-versa ?
- 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.
- What is the advantage of overloading subscript [] operator ?
- How will you overload a function call () operator ?
- Write a program to print Fibonacci series by overloading prefix++ increment operator.
- Create a class time and overland the increment (++) operator that operate in both prefix and postfix notation and display the calculated time.
- 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.
- 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).
- 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.
- Overload the unary operator Not(!) to test whether an object of class string is empty or not.
- 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.
- Create a class weight having data members kg and gram. Overload all the arithmetic operators on the objects of weight class.
- 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.
- 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.
- 80 Most Important Network Fundamentals Questions With Answers
- 100 Most Important SOC Analyst Interview Questions
- Top 40 Cyber Security Questions and Answers
- Top 10 React JS Interview Theory Questions and Answers
- CISSP – Practice Test Questions – 2024 – Set 20 (53 Questions)
- Part 2: Exploring Deeper into CCNA – Wireless (145 Practice Test Questions)
- Part 1: Mastering CCNA – Wireless (145 Practice Test Questions)
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 3
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 2
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 1