Dr. Mujtaba Asad has a PhD from Shanghai Jiao Tong University and has 10 years of teaching and research experience, with 6 impact factor journal publications in the last 3 years. His research areas include deep learning, computer vision, and anomaly detection.
Operator overloading in C++ allows operators like + and << to be applied to user-defined classes by defining corresponding operator functions. For example, the + operator can be overloaded as a member function to add two complex number objects. The << operator can be overloaded as a non-member function to output complex number objects. Some operators like . and :: cannot be overloaded.
%in Durban+277-882-255-28 abortion pills for sale in Durban
OperatorOverloading.ppt
1. My Introduction
Dr. Mujtaba Asad
Education:
PhD: Shanghai Jiao Tong University
Subject QS Rank: 23, Overall QS Rank: 47
MS(CS), BS(TE): FAST-NUCES Lahore
Intermediate (I.C.S): Govt. College University (G.C.U) Lahore
Experience:
10 Years of Teaching & Research Experience
Publications:
6 Impact Factor Journal Publications in Last 3 Years
Research Area:
Deep Learning, Computer Vision (Video Analysis), Anomaly Detection
3. Why Operator Overloading Is
Needed ?
• As we know that the only applicable operators on the class
objects are the assignment operator ( = ) , member
selection operator ( . ).
• Therefore, other operators cannot be directly applied to
class objects.
• However, C++ allows the programmer to extend the
definitions of most of the operators so that operators—such
as relational operators, arithmetic operators, the insertion
operator for data output, and the extraction operator for
data input—can be applied to classes.
• In C++ terminology, this is called operator overloading.
5. How to Redefine (Overload) Operators)
• Overloading an operator (Syntax)
– 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
6. Operator Functions as Class Members
vs. as friend Functions
• Member vs non-member
– Operator functions can be member or non-member functions
– When overloading ( ), [ ], -> or any of the assignment
operators, must use a member function
• Operator functions as member functions
– Leftmost operand must be an object (or reference to an object)
of the class
• If left operand of a different type, operator function must be a non-
member function
• Operator functions as non-member functions
– Must be friends if needs to access private or protected
members
– Enable the operator to be commutative
9. Restrictions on Operator Overloading
• C++ operators that can be overloaded
• C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
10. 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
Editor's Notes
I am Dr. Mujtaba Asad. I have been in the teaching profession since 2013 and I have total 10 years of teaching and research experience. I completed my PhD from Shanghai Jiao Tong University, which is one the top ranked universities in world, with a subject QS rank of 23rd in the world. I had been awarded the Scholarship for my PhD Studies from Ministry of Interprovincial Coordination (IPC) for top 50 ranked universities of the world. I did my MS and BS degrees from FAST Lahore. I spent 7 years of life studying here in this institute. I did my intermediate from Govt. College University Lahore. I have obtained first division results throughout my academic career. I have total 13 research publications including 6 impact factor journal publications in last 3 years. My areas of research are Deep Learning, Video Analysis and Video Anomaly detection.
Why we need to overload the operators??
As we know that the only applicable operators for the objects of classes are the assignment operator ( = ) , member selection operator (.)
Therefore, other operators such as arithmetic, relational, insertion and extraction operators cannot be directly applied to class objects. Lets take an example of complex numbers class.
As C++ does not have a built in data type for storing the complex number so we have created our own datatype by writing the class for it. Now If I declare an object of this datatype (class), I can store and display complex numbers in my program. However, I am still unable to do mathematical calculation on the this type of data. As the complex numbers is the object of a class. .
Because for example, if I do this
ComplexType z1, z2(3,5), z3(4,6);
And then z1=z2+z3
The compiler will not able to decide which member of 1st object to be added in the member of 2nd object. Therefore, to resolve this we have to redefine the meaning of the operator + so that it can work with our complex number datatype. This procedure is known as operator overloading.
For overloading operators, we simply have to add a user defined functions in our program where the name of the function is “operator followed by the operator symbol. The returnType and formal parameters for this function are selected on the basis of operator type and their workings.
Lets take an example of overloading Addition operator for ComplexType class.
As the + operator returns the same datatype as of its operands therefore, in the this case the returnType will be ComplexType. Also the + operator is a binary operator so it always will take two operands (one on its left and one on its right). So should have two formal parameters for this overloading function. However, there are two ways writing overloading functions in C++
One is to write the overloading function as the member of class
And the other is to write the overloading function as non-member function of the class.
For some operators, we have the choice to either write the member or non-member overloading function. However, for others operators we do no have the choice.
For square brackets, parenthesis and arrow operator we must have to write member function.
For operators, where the left operand is the same object of the class for which the overloading function is being written, then we have a choice to either write member of non-member function of the class
For operators, where the left operand is not the same object of the class for which the overloading function is being written, then we must have to write a non-member function of the class. And in this case we have to made this function as friend of the class so that it can access the private members of the class through the operator function.
Coming back to the overloading function of operator +, Lets write the overloading function for operator + as member function of the class.
Remember, whenever an overloading function is written as member function the function call with be for the left operand. So for this expression:
z1=z2+z3 , the overloading function call be made through the operand z2 and the call will be like this z1=z2.operator(z3). So instead of two formal parameters this member overloading function will have only one parameter of Type ComplexType.
Now if we don’t want to use the complexType class’s member function “Display” to print the complex numbers on the screen. And we just want to use cout<< to display our complex numbers. Then we also have ti overload the stream insertion operator. Now Notice that the left side operand for operator << is not an object of class ComplexType. Therefore, we must have to write non-member overloading function for the operator <<.
Remember, whenever an overloading function is written as non-member function , then the function call with made be as standalone call. So for this expression:
Cout<<z1; , the stand alone call will be made for the overloading function of operator <<, and the call will be like this operator<<(cout , z1). So instead of one formal parameters this non-member overloading function will have now two parameters of Type ostream and ComplexType. Also, we have to make this function as the friend of class ComplexType so that the stream insertion operator can easily access the private members of complexType object z1.
and the function will be like this