OPERATOR OVERLOADING Customised behaviour of operators.
-
Upload
sheryl-walsh -
Category
Documents
-
view
223 -
download
0
Transcript of OPERATOR OVERLOADING Customised behaviour of operators.
![Page 1: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/1.jpg)
OPERATOR OVERLOADING
Customised behaviour of operators
![Page 2: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/2.jpg)
Introduction
Operator overloading Use traditional operators with user-defined
objects Straightforward and natural way to extend
C++ Requires great care
When overloading misused, program difficult to understand
![Page 3: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/3.jpg)
Fundamentals of Operator Overloading Use operator overloading to improve
readability Avoid excessive or inconsistent usage
Format Write function definition as normal Function name is keyword operator
followed by the symbol for the operator being overloaded.
operator+ would be used to overload the addition operator (+)
![Page 4: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/4.jpg)
Restrictions on Operator Overloading
Most of C++’s operators can be overloaded
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
![Page 5: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/5.jpg)
Restrictions on Operator Overloading (II)
Arity (number of operands) cannot be changed Urnary operators remain urnary, and binary
operators remain binary Operators *, + and - each have unary and
binary versions Unary and binary versions can be overloaded
separately
![Page 6: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/6.jpg)
Restrictions on Operator Overloading (III)
No new operators can be created Use only existing operators
Built-in types Cannot overload operators You cannot change how two integers are
added
![Page 7: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/7.jpg)
Coding Practices
![Page 8: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/8.jpg)
Example: Operator Overloadingclass OverloadingExample
{ private:
int m_LocalInt;
public:
OverloadingExample(int j) // default constructor
{
m_LocalInt = j;
}
int operator+ (int j) // overloaded + operator
{
return (m_LocalInt + j);
}
};
![Page 9: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/9.jpg)
Example: Operator Overloading (contd.)
void main()
{
OverloadingExample object1(10);
cout << object1 + 10;
}
![Page 10: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/10.jpg)
Types of Operator
Unary operator Binary operator
![Page 11: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/11.jpg)
Unary Operators
Operators attached to a single operand (-a, +a, --a, a--, ++a, a++)
![Page 12: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/12.jpg)
Example: Unary Operators
class UnaryExample
{
private:
int m_LocalInt;
public:
UnaryExample(int j)
{
m_LocalInt = j;
}
int operator++ ()
{
return (m_LocalInt++);
}
};
![Page 13: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/13.jpg)
Example: Unary Operators (contd.)
void main()
{
UnaryExample object1(10);
cout << object1++;
}
![Page 14: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/14.jpg)
Prefix and Postfix Notation
int operator++ () // Prefix{ return (++m_LocalInt);}
int operator++ (int) // postfix{ return (m_LocalInt++);}Only difference is the int in the parenthesis
![Page 15: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/15.jpg)
Binary Operators
Operators attached to two operands a+b, a-b, a*b etc.)
![Page 16: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/16.jpg)
Example: Unary Operators
class UnaryExample
{
private:
int m_LocalInt;
public:
UnaryExample(int j)
{
m_LocalInt = j;
}
int operator++ ()
{
return (m_LocalInt++);
}
};
![Page 17: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/17.jpg)
Binary Operators
Operators attached to two operands (a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b)
![Page 18: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/18.jpg)
Example: Binary Operators
class BinaryExample
{
private:
int m_LocalInt;
public:
BinaryExample(int j)
{
m_LocalInt = j;
}
int operator+ (BinaryExample rhsObj)
{
return (m_LocalInt + rhsObj.m_LocalInt);
}
};
![Page 19: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/19.jpg)
Example: Binary Operators (contd.)
void main()
{
BinaryExample object1(10), object2(20);
cout << object1 + object2;
}
![Page 20: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/20.jpg)
Another Way of Doing it!
How to solve object1 + object2 +object3+……?
![Page 21: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/21.jpg)
class BinaryExampleU{ private:
int m_LocalInt; public: BinaryExampleU(int j)
{ m_LocalInt = j;
}BinaryExampleU operator+ (BinaryExampleU rhsObj){ return BinaryExampleU(m_LocalInt +
rhsObj.m_LocalInt);}
void print() {
cout<<m_LocalInt; }};
![Page 22: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/22.jpg)
void main(){ BinaryExampleU object1(10),
object2(20), object3 (30), object4(0); object4 = object1 + object2 +object3 ;
object4.print();}
![Page 23: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/23.jpg)
Case Study: An Array class
Implement an Array class with Range checking Array assignment Arrays that know their size Outputting/inputting entire arrays with <<
and >> Array comparisons with == and !=
![Page 24: OPERATOR OVERLOADING Customised behaviour of operators.](https://reader033.fdocuments.us/reader033/viewer/2022051401/5697bfee1a28abf838cb93d6/html5/thumbnails/24.jpg)
Thankyou