Download Operator Overloading-Advanced Programming for Engineers-Lecture Slides and more Slides Programming for Engineers in PDF only on Docsity!
Operator Overloading
Operator overloading
- Operator overloading is to allow the same operator to be bound to more than one implementation, depending on the types of the operands.
- As you know that there are standard arithmetic operators in C/C++ for addition ( + ), subtraction ( - ), multiplication ( * ) and division ( / ).
- We should only use these operators for their specific purposes.
- If we want to add two int s, say i and j , the addition will take place in the following manner i.e. i + j.
Operators to Overload
- There are two types of operators to overload:
- Unary
- Binary
- Unary operators are the ones that require only one operator to work.
- Unary operators are applied to the left of the operand. For example, ^ , & , ~ and!.
- Binary operators require two operands on both
sides of the operator.
- + , - , ***** , / , % , = , < and > are examples of binary
operators.
Fundamentals of Operator Overloading
- 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
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[]
Restrictions on Operator Overloading
- 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
Operator Functions as Class Members vs.
as friend Functions
- Member vs non-member
- Operator functions can be member or non-member functions
- Operator functions as member functions
- Leftmost operand must be an object (or reference to an object) of the class
- Operator functions as non-member functions
- Must be friends if needs to access private or protected members
Example: Complex.h
class Complex {
public: int a,b;
Complex(int ,int); Complex(); ~Complex(); int getA(); void setA(int); int getB(); void setB(int); void printNum(); Complex operator+(Complex&); };
Complex.cpp
Complex::Complex(int i,int j) { a=i;b=j; } Complex::Complex() {} Complex::~Complex(){} int Complex::getA() { return a; }
void Complex::printNum() { cout<<"The number is:"<<a<<"+"<<b<<"i"<<endl; } Complex Complex::operator+(Complex& compNo) { cout<<"Operator + being Called"<<endl; Complex x(a + compNo.a , b + compNo.b); return (x); }
#include #include "Complex.h" using namespace std; void main() { Complex c1(3,4); c1.printNum(); Complex c2(5,6); c2.printNum(); Complex c3; c3=c1+c2; c3.printNum(); }
The member functions ‘addTwo’ and operator+
double Employee::addTwo(const Employee& emp) { double total; total = salary + emp.getSalary(); return total; }
double Employee::operator+(const Employee& emp) { double total; total = salary + emp.getSalary(); return(total); }
Using the Member Functions
void main(void) { double sum Employee Clerk (111, 10000), Driver (222, 6000);
// these three statements do the same thing
sum = Clerk.addTwo(Driver);
sum = Clerk.operator+(Driver); cout<<“sum :”<<sum<<endl; sum = Clerk + Driver; cout<<“sum :”<<sum<<endl; // the syntax for the last one is the most natural // and is easy to remember because it is consistent // with how the + operator works for everything else }