This document discusses operator overloading in C++. It defines operator overloading as allowing an operator to perform operations on user-defined types. It covers the concept, syntax, rules, advantages, disadvantages and provides an example of overloading the + and - operators for an integer class. Operations that can be overloaded include arithmetic, logical, relational, and pointer operators. The main advantage is improved readability and code reuse when working with user-defined types.
2. Contents:
• Introduction
• What is Operator overloading?
• Concept of Operator overloading
• Syntax of Operator overloading
• Rules for Operator overloading
• Advantages of Operator overloading
• Disadvantages of Operator overloading
• Example
• Conclusion
3. Introduction:
• Operator overloading is an important concept in C++.
• It is a type of polymorphism in which an operator is
overloaded to give user defined meaning to it.
• Overloaded operator is used to perform operation on user-
defined data type.
4. What is Operator overloading?
• C++ allows you to specify more than one definition for an
operator in the same scope, which is called operator
overloading.
• Operator overloading provides ability to an operator to
perform other manipulation.
• It preserves the original function of the operators and work
with user-defined types only.
5. Operations that can be perform:
1. Athematic operations: + – * / %
2. Logical operations: && and ||
3. Relational operations: == != >= <=
4. Pointer operators: & and *
5. Memory management operator: new, delete []
7. Concept of Operator overloading:
• Operator overloading is a compile-time polymorphism in
which the operator is overloaded to provide the special
meaning to the user-defined data type.
• Operators overloading is used to overloaded or refines
most of the operators available in C++.
8. Syntax of Operator overloading :
class ClassName
{
...
public:
…
return-type operator op(params-line)
{
//body of the function
…
}
…
};
9. Rules for Operator overloading:
• Only built-in operators can be overloaded. New operators can
not be created.
• Arity of the operators cannot be changed.
• Precedence and associativity of the operators cannot be
changed.
• Overloaded operators cannot have default arguments expect
the function call operator() which can have default arguments.
• Operators cannot be overloaded for built in types only. At
least one operand must be used defined type.
10. Rules cont:
• Assignment (=),subscript ([]),function call(“()”); and
member selection (->) operators must be defined as member
functions.
• Expect the operators specified in point 6, all other operators
can be either member functions or a non member functions.
• Some operators like (assignment)=, (address)& and comma(,)
are by default overloaded.
11. Advantages of Operator overloading:
• The main advantage of function overloading is that it
improves code readability and allows code reusability.
• Operator overloading enables programmers to use notation
closer to the target domain.
• Operator overloading provides similar syntactic support of
build-in types to user-defined types.
• Operator overloading makes the program easier to understand.
• It speeds up the execution of the program.
12. Disadvantages of Operator overloading:
• We can only overloaded the operators that exist and cannot
cerate new operators or rename existing operators.
• It is not possible to change the number of operands of an
operator supports.
• Conditional[?:], size of , scope(::), member selector(.),
member pointer selector(.*)and the casting operators.
• All operators keep their default precedence and
associations(what they use for). Only built-in operators can
be overloaded.
• Atleast one of the operands in overloaded must be user-
defined which means we cannot overload the minus operator
to work with one integer and one double.
13. Example:
• Write a program that adds and subtracts two integer values
using binary C++ Operator Overloading:
include <iostream>
#include<conio.h>
using namespace std;
class temp
{
private:
int data;
public:
void getvalue()
{
cin>>data;
}
15. cout<<"enter an integer value for obj1: ";
obj1.getvalue();
cout<<"Enter an integer value for obj2: ";
obj2.getvalue();
sum= obj1+obj2;
sub=obj1-obj2;
cout<<"Addition result is = "<<sum.display()<<endl;
cout<<"Subtraction result is = "<<sub.display()<<endl;
getch();
}
Output:
16. Conclusion:
• Operator overloading is one of the feature of C++ that’s
implements compile time polymorphism.
• Overloading code should be written with greater care.
• Meaning of the operator should be preserved.
• Use any of the two method for declaring the operator
function.