Operator Overloading Final2
Operator Overloading Final2
M. A. Nur Quraishi
Lecturer
Department of Computer Science & Submitted By:
Engineering
Team Jamuna
Intake:52
Section:02
Members
Nafis Aziz…………………...............................................20234103043
Jahid Hasan Rifat………………………………………... 20234103047
Md. Saymun Ahmmed Limon…………………………... 20234103049
Md. Turag……………………………………….……..…20234103066
Kaniz Fatema………………………………….…………20234103066
Maroful Islam……………………………………….….. 20234103075
Md. Shahariar Ovi……………………………………….20234103080
Sheikh Ramisha Zubayda…………………………..…... 20234103236
Bani Sarker……………………………………………….20234103387
Operator
Overloading
•
Introduction
Operator overloading
– Enabling C++’s operators to work with class objects
– Using traditional operators with user-defined objects
– Requires great care; when overloading is misused, program
difficult to understand
– Examples of already overloaded operators
• Operator << is both the stream-insertion operator and the
bitwise left-shift operator
• + and -, perform arithmetic on multiple types
– Compiler generates the appropriate code based on the manner in
which the operator is used
Introduction
• Overloading an operator
– Write function definition as normal
– Function name is keyword operator followed by the symbol for
the operator being overloaded
– operator+ used to overload the addition operator (+)
• Using operators
– To use an operator on a class object it must be overloaded unless the
assignment operator(=)or the address operator(&)
• Assignment operator by default performs memberwise
assignment
• Address operator (&) by default returns the address of an object
Restrictions on Operator Overloading
C++ operators that can be overloaded
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Restrictions on Operator Overloading
C++ operators that cannot be overloaded
2.Simplification of Code:
When done correctly, operator overloading simplifies code by allowing
you to use operators with user-defined types just like you do with primitive types.
For instance, if you have a custom String class, overloading the + operator lets
you concatenate strings using the + symbol, which is more natural and concise.
Advantages:
3.Template Compatibility:
Operator overloading makes it possible for templates to work equally
well with classes and built-in types. Templates can use overloaded operators
seamlessly, allowing you to create generic code that works with various data
types.
4.Customized Behaviour:
By overloading operators, you can redefine their behaviour to suit your
specific needs. Define custom arithmetic operations for complex numbers,
fractional numbers , or other user-defined types. Implement custom comparison
operators (<, >, ==, etc.) for meaningful comparisons between objects.
Advantages:
5.Consistent Syntax:
Operator overloading ensures consistent syntax for operations involving
user-defined types. This consistency improves code readability and maintainability.
Users can apply the same operators to your custom classes as they do with built-in
types, leading to more natural and expressive code.
6.Enhanced Expressiveness:
Overloaded operators allow you to express complex operations
succinctly. For example, matrix multiplication or vector addition can be elegantly
represented using overloaded operators. This expressive power enhances the
readability of your code and promotes good software design practices.
Disadvantages:
1.Complexity and Confusion:
Overloading operators can make code more complex and harder to
understand. When operators are overloaded, their behaviour might not be
immediately obvious to other developers, leading to confusion. For example, if
you overload the + operator for a custom class, it might not behave the same way
as the built-in addition for primitive types.
6.Default Arguments:
Overloaded operators cannot have default arguments, except for the
function call operator () which can have them.
This limitation can affect the flexibility of operator overloading.
More Coding Example