0% found this document useful (0 votes)
7 views

Operator Overloading Final2

The document discusses operator overloading in C++. It explains what operator overloading is, some examples of overloaded operators, and restrictions on overloading operators. It also covers overloading unary and binary operators, provides code examples, and discusses advantages and disadvantages of operator overloading.

Uploaded by

medhachaaki
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Operator Overloading Final2

The document discusses operator overloading in C++. It explains what operator overloading is, some examples of overloaded operators, and restrictions on overloading operators. It also covers overloading unary and binary operators, provides code examples, and discusses advantages and disadvantages of operator overloading.

Uploaded by

medhachaaki
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Submitted To:

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

Operators that cannot be overloaded


. .* :: ?: sizeof
Restrictions on Operator Overloading
• Overloading restrictions
– Precedence of an operator cannot be changed
– Associativity of an operator cannot be changed
– Arity (number of operands) cannot be changed
• Unary operators remain unary, and binary operators remain binary
• Operators &, *, + and - each have unary and binary versions
• Unary and binary versions can be overloaded separately
• No new operators can be created
– Use only existing operators
• No overloading operators for built-in types
– Cannot change how two integers are added
– Produces a syntax error
Overloading Unary Operators
• Overloading unary operators
– Can be overloaded with no arguments or one argument
– Should usually be implemented as member functions
• Avoid friend functions and classes because they violate the
encapsulation of a class
– Example declaration as a member function:
class String {
public:
bool operator!() const;
...
};
Overloading Unary Operators
– Example declaration as a non-member function
class String {
friend bool operator!( const String
& )
...
}
PLEASE CLICK ON Ctrl and the below text at the
same time to open the code

Overloading Unary Operator Code1


Overloading Binary Operators
• Overloaded Binary operators
– Non-static member function, one argument
– Example:
class String {
public:
const String &operator+=(
const String & );
...
};
– y += z is equivalent to y.operator+=( z )
Overloading Binary Operators
– Non-member function, two arguments
– Example:
class String {
friend const String &operator+=(
String &, const String
& );
...
};
– y += z is equivalent to operator+=( y, z )
Overloading Binary Operator Code1
Advantages:
1.Intuitive Interface:
Operator overloading allows you to provide an intuitive interface to
users of your class. By redefining operators, you can make user-defined types
(classes) behave similarly to built-in types. For example, overloading the +
operator for a custom class like Complex enables you to add two objects of that
class using the familiar + syntax.

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.

2.Misuse and Abuse:


Overloaded operators can be misused or abused. Some programmers
might overload operators in non-intuitive ways, leading to unexpected
behaviour.For instance, overloading the << and >> operators for custom I/O
streams can be powerful, but it can also lead to confusion if not done carefully.
Disadvantages:
3.Performance Impact:
Overloaded operators can introduce performance overhead. For
instance, a seemingly innocent operator like + or [] can be turned into something
that consumes a lot of CPU time. Developers sometimes underestimate the
performance impact of overloaded operators, assuming that their execution time
is proportional to the number of characters they type.
4.Loss of Special Properties:
When operators like && or || are overloaded, they lose their special
properties, such as short-circuit evaluation. This can lead to unexpected
behaviour in expressions.
Similarly, overloading the comma operator (,) can alter its sequencing behaviour,
which might not be what you intend.
Disadvantages:
5.Restrictions on Overloading:
Recedence and associativity of an operator cannot be
changed.Arity (number of operands) cannot be changed. Unary operators
remain unary,and binary operators remain binary.No new operators can be
created; only existing operators can be overloaded.

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

Overloading Unary Operator Code2

Overloading Binary Operator Code2


Conclusion:
Operator Overloading allows us to provide an
intuitive interface to users of our class, plus makes
it possible for templates to work equally well with
classes and built-in/intrinsic types. Operator
overloading allows C/C++ operators to have user-
defined meanings on user-defined types (classes).
THANK
YOU

You might also like