Download presentation
Presentation is loading. Please wait.
Published byClaud Griffith Modified over 9 years ago
1
Operatorsand Operators Overloading
2
Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers a programmer a more elegant and powerful mathematical shorthand for performing operations on object. Example: overloading the arithmetic binary operators Matrix m3; m3.Add(m1, m2); Matrix m3 = m1 + m2 Without operator overloading With operator overloading
3
class Point{ private: double x, y; // constructors……….. public: void Add ( const Point &p1, const Point &p2){ x = p1.x + p2.x; y = p1.y + p2.y; } void Subtract ( const Point &p1, const Point &p2){ x = p1.x - p2.x; y = p1.y - p2.y; } }; void main( ){ Point p1 (3.0, 4.0); Point p2 (1.0, 2.0); Point p3, p4; p3.Add(p1, p2); p4.Subtract(p1. p2); } Note: Without Overloaded Operator
4
class Point{ private: double x, y; // constructors……….. public: //overloaded operator Point operator + (const Point &p); Point operator - (const Point &p); }; Point Point::operator + (const Point &p) { return Point(x + p.x, y + p.y) } Point Point::operator - (const Point &p) { return Point(x - p.x, y - p.y) } void main( ){ Point p1 (3.0, 4.0); Point p2 (1.0, 2.0); Point p3, p4; p3 = p1 + p2; p4 = p1 – p2; } Note: With Overloaded Operator
5
The Unary Increment (++) and Decrement (--) Operator Overloaded prefix (++p) operator increments each data member by the value of 1 and then returns the incremented object. Overloaded the postfix (p++) increment operator C++ adopts a similar style to the prefix operator, but uses a dummy member function argument of type int. For p++, each data member is reduced by the constant 1 before it is returned, thus returning the original object before incrementing occurred. A similar procedure is exercised for the prefix and postfix decrement operators.
6
class Point{ //….. public: Point operator ++ ( ); Point operator ++ (int); Point operator -- ( ); Point operator -- (int); }; Point Point::operator ++ ( ){ x += 1.0; y += 1.0; return Point(x, y); } Point Point::operator ++ (int){ x += 1.0; y += 1.0; return Point(x -1.0, y- 1.0); } Point Point::operator -- ( ){ x -= 1.0; y -= 1.0; return Point(x, y); } Point Point::operator -- (int); { x -= 1.0; y -= 1.0; return Point(x +1.0, y +1.0); } void main( ){ Point p1 (1.0, 2.0); Point p2 (1.0, 2.0); Point r; r = ++p1; r = p2++; } Output: 2, 3 1, 2 (1) (2) (1) (2)
7
The Relational Operators Relational operators (, >=, == and !=). Relational operator involving either logical-true or logical-false. Provided the Boolean FALSE is denoted by zero and Boolean TRUE by any non-zero value. Boolean data type is most conveniently implemented as an enum. enum Boolean { FALSE, TRUE };
8
Thus, making use of the Boolean type, the overloaded equality operators are implemented as: Boolean Point::operator == (const Point &p){ return (x==p.x && y==p.y && z==p.z) ? TRUE : FALSE; } Boolean Point::operator != (const Point &p){ return (x!=p.x && y!=p.y && z!=p.z) ? TRUE : FALSE; } or, without Boolean type: int Point::operator == (const Point &p){ return (x==p.x && y==p.y && z==p.z); } int Point::operator != (const Point &p){ return (x!=p.x && y!=p.y && z!=p.z); }
9
class Point{ private: double x, y; public: Point operator = (const Point &p ); }; Point Point::operator = (const Point &p ){ x = p.x; y = p.y; cout<<“Overloaded = operator called”; return Point(x, y); } main( ){ Point p1(3.0, 4.0), p2; p2 = p1; cout << “p2” << p2.x << p2.y << endl; } Output: Overloaded = operator called P2 3 4 Overloaded = operator Overloaded Assignment Operator The operation of the overloaded assignment operator = ( ) function is similar to the operation of the copy constructor.
10
The overloaded assignment operator member function definition is: Which indicates that the two data members of a Point object on the right-hand side of the operator = are directly assigned to the data members of the object on the left- hand side. The overloaded = operator member function is passed a const object by reference rather than by value, and a Point object is returned. This pointer enables a function to return the object which invokes the overloaded operator member function. Point Point::operator = (const Point &p ){ x = p.x; y = p.y; cout<<“Overloaded = operator called”; return Point(x, y);}
11
Non-Member Overloaded Operator Functions Overloaded operator functions can be non- member functions.
12
class Point{ //……… public: double x, y; }; inline Point operator + (const Point &p1, const Point &p2){ return Point ( p1.x + p2.x, p1.y + p2.y ); } inline Point operator - (const Point &p1, const Point &p2){ return Point ( p1.x - p2.x, p1.y - p2.y ); } main( ){ Point p1(3.0, 4.0); Point p2(1.0, 2.0); Point p3, p4, p5; p3 = p1 + p2; p4 = p1 – p2; } p3 (4, 6) p4 (2, 2)
13
Overloading Input Extraction and Output Insertion Operators cout is an object of class ostream, which provides standard output. The insertion (<<) operator is sufficiently overloaded to output all of C++’s integral types so that a user of the ostream class does not have to specify the type of identifier or object when performing output. Overloaded insertion and extraction functions must be non-member functions of a user-defined class. cout << “integer 1 = ” << x << endl;
14
Typical IOSTREAM.H Header If the << operator is not overloaded for a given class, it is the responsibility of the designer to overload the << operator. class ostream : public ios { //… public: // Character ostream & operator << (char); ostream & operator << (signed char); ostream & operator << (unsigned char); // Integer ostream & operator << (int); ostream & operator << (long); ostream & operator << (double); ostream & operator << (float); };
15
class Point{ public: double x, y; //…. }; ostream& operator << (ostream &s, const Point &p){ s << “(” << p.x << “,” << p.y << “)” << endl; return s;} istream& operator >> (istream &s, const Point &p){ cout << “Enter x and y coordinates of a point: ”; s >> p.x >> p.y; return s;} main( ){ Point p(1.0, 2.0), q; cout << p; cin >> q; cout << q; } Output: (1, 2) Enter x and y coordinates of a Point: 4 5 (4, 5)
16
Two objects are passed as arguments to the function. The first argument is a reference to the output stream which occurs on the left-hand side of the << operator. The second argument is a const reference to the object on the right-hand side of the overloaded operator to be chained. The function returns a reference to an object of class ostream, which allows the overloaded operator to be chained. ostream& operator << (ostream &s, const Point &p){ s << “(” << p.x << “,” << p.y << “)” << endl; return s;}
17
Operators Which Cannot be Overloaded C++ is very generous as to which of the available operators can be overloaded. However, there are a few exceptions. Operators that cannot be overloaded are: :: scope resolution. Direct member access sizeof size, in bytes, of an object.*direct member pointer access ?:conditional
18
Friend and Overloaded Operators class Point{ private: double x, y, z; public: Point(double x_arg, double y_arg, double z_arg = 0.0) : x(x_arg), y(y_arg), z(z_arg){} friend Point operator + (const Point &p1, const Point &p2); }; inline Point operator + (const Point &p1, const Point &p2){ return Point(p1.x + p2.x, p1.y+p2.y, p1.z+p2.z); } void main( ){ Point p (1.0,1.0); Point q (-1.0,-1.0); Point r = p + q; } Friend also can defined as inline
19
class Complex{ private: double re, im; public: Complex (double r, double i); friend Complex operator + (const Complex &c1, const Complex &c2); friend Complex operator - (const Complex &c1, const Complex &c2); friend ostream &operator << (ostream &s, const Complex &c); friend istream &operator << (istream &s, const Complex &c); }; Complex::Complex(double r, double i){ re = r; im = i; } inline Complex operator + (const Complex &c1, const Complex &c2){ return Complex (c1.re + c2.re, c1.im + c2.im); } inline Complex operator - (const Complex &c1, const Complex &c2){ return Complex (c1.re - c2.re, c1.im - c2.im); } inline ostream &operator << (ostream &s, const Complex &c){ return s << "(" << c.re << "," << c.im << ")" << endl; }
20
void main( ){ Complex c1(1.0, 2.0); Complex c2(3.0, 4.0); Complex c3 = c1 + c2; Complex c4 = c1 - c2; cout << "c3 " << c3; cout << "c4 " << c4; } Output: c3 (4,6) c4 (-2,-2)
21
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator + (addition) ClassName operator+(const ClassName &op1, const ClassName &op2) { // your calculations here return ClassName(...); // return the related constructor } Definition: friend ClassName operator+(const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a+c;
22
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator (subtraction) ClassName operator (const ClassName &op1, const ClassName &op2) { // your calculations here return ClassName(...); // return the related constructor } Definition: friend ClassName operator (const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a-c;
23
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator (multiplication) ClassName operator (const ClassName &op1, const ClassName &op2) { // your calculations here return ClassName(...); // return the related constructor } Definition: friend ClassName operator (const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a*c;
24
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator / (division) ClassName operator/(const ClassName &op1, const ClassName &op2) { // your calculations here return ClassName(...); // return the related constructor } Definition: friend ClassName operator / (const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a/c;
25
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator + (addition) ClassName ClassName::operator+(const ClassName &op) { // your calculations here return ClassName(...); // return the related constructor } Definition: ClassName operator+(const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a+c; // error
26
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator (subtraction) ClassName ClassName::operator (const ClassName &op) { // your calculations here return ClassName(...); // return the related constructor } Definition: ClassName operator (const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a-c; // error
27
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator (multiplication) ClassName ClassName::operator (const ClassName &op) { // your calculations here return ClassName(...); // return the related constructor } Definition: ClassName operator (const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a*c; // error
28
Summary of Standard Operator Overloading Implementation Binary Arithmetic Operators Operator / (division) ClassName ClassName::operator/(const ClassName &op) { // your calculations here return ClassName(...); // return the related constructor } Definition: ClassName operator / (const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3; ClassName b, c(4); b = a/c; // error
29
Summary of Standard Operator Overloading Implementation Unary Operators Operator ++ (postfix increment) ClassName Classname::operator++(int) { ClassName cn(...) // save original // your calculations here return cn; // return the original } Definition: ClassName operator ++ (int); Declaration:
30
Summary of Standard Operator Overloading Implementation Unary Operators Operator (postfix decrement) ClassName Classname::operator (int) { ClassName cn(...) // save original // your calculations here return cn; // return the original } Definition: ClassName operator (int); Declaration:
31
Summary of Standard Operator Overloading Implementation Unary Operators Operator ++ (prefix increment) ClassName &Classname::operator++() { // your calculations here return *this; // return the same object } Definition: ClassName &operator ++ (); Declaration:
32
Summary of Standard Operator Overloading Implementation Unary Operators Operator (prefix decrement) ClassName &Classname::operator () { // your calculations here return *this; // return the same object } Definition: ClassName &operator (); Declaration:
33
Summary of Standard Operator Overloading Implementation Unary Operators Operator (negation) ClassName Classname::operator () { ClassName cn(...); // initialize an object // your calculations here return cn; // return the same object } Definition: ClassName operator (); Declaration:
34
Summary of Standard Operator Overloading Implementation Unary Operators Operator + (affirmation) ClassName &Classname::operator+() { return *this; } Definition: ClassName &operator + (); Declaration:
35
Summary of Standard Operator Overloading Implementation Assignment Operators Operator = (normal assignment) ClassName &ClassName::operator=(const ClassName &op) { // your calculations here return *this; // return the same object } Definition: ClassName &operator=(const ClassName &op); Declaration:
36
Summary of Standard Operator Overloading Implementation Assignment Operators Operator + (additive assignment) ClassName &ClassName::operator+ (const ClassName &op) { // your calculations here return *this; // return the same object } Definition: ClassName &operator+ (const ClassName &op); Declaration:
37
Summary of Standard Operator Overloading Implementation Assignment Operators Operator (subtractive assignment) ClassName &ClassName::operator (const ClassName &op) { // your calculations here return *this; // return the same object } Definition: ClassName &operator (const ClassName &op); Declaration:
38
Summary of Standard Operator Overloading Implementation Assignment Operators Operator (multiplicative assignment) ClassName &ClassName::operator (const ClassName &op) { // your calculations here return *this; // return the same object } Definition: ClassName &operator (const ClassName &op); Declaration:
39
Summary of Standard Operator Overloading Implementation Assignment Operators Operator / (divisive assignment) ClassName &ClassName::operator/ (const ClassName &op) { // your calculations here return *this; // return the same object } Definition: ClassName &operator/ (const ClassName &op); Declaration:
40
Summary of Standard Operator Overloading Implementation Relational Operators Operator == (equal to) ClassName operator==(const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator==(const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a == c);
41
Summary of Standard Operator Overloading Implementation Relational Operators Operator != (not equal to) ClassName operator!=(const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator!=(const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a != c);
42
Summary of Standard Operator Overloading Implementation Relational Operators Operator = (less than or equal to) ClassName operator =(const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator =(const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a <= c);
43
Summary of Standard Operator Overloading Implementation Relational Operators Operator = (greater than or equal to) ClassName operator =(const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator =(const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a >= c);
44
Summary of Standard Operator Overloading Implementation Relational Operators Operator (less than) ClassName operator (const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator (const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a < c);
45
Summary of Standard Operator Overloading Implementation Relational Operators Operator (greater than) ClassName operator (const ClassName &op1, const ClassName &op2) { return (...); // return a value of a comparison calculation } Definition: friend int operator (const ClassName &op1, const ClassName &op2); Declaration: Non-Member : to allow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a > c);
46
Summary of Standard Operator Overloading Implementation Relational Operators Operator == (equal to) int ClassName::operator==(const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator==(const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a == c); // error
47
Summary of Standard Operator Overloading Implementation Relational Operators Operator != (not equal to) int ClassName::operator!=(const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator!=(const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a != c); // error
48
Summary of Standard Operator Overloading Implementation Relational Operators Operator = (less than or equal to) int ClassName::operator =(const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator =(const ClassName &op; Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a <= c); // error
49
Summary of Standard Operator Overloading Implementation Relational Operators Operator = (greater than or equal to) int ClassName::operator =(const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator =(const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a >= c); // error
50
Summary of Standard Operator Overloading Implementation Relational Operators Operator (less than) int ClassName::operator (const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator (const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a < c); // error
51
Summary of Standard Operator Overloading Implementation Relational Operators Operator (greater than) int ClassName::operator (const ClassName &op) { return (...); // return a value of a comparison calculation } Definition: int operator (const ClassName &op); Declaration: Member : to disallow operation from other class, e.g.: int a = 3, b; ClassName c(4); b = (a > c); // error
52
Summary of Standard Operator Overloading Implementation Shift Operators (for streaming) Operator (shift right / input) istream &operator (istream &is, ClassName &obj) { is >>... // your calculations here obj = ClassName(...); // reset the class object return is; // return the istream } Definition: friend istream &operator (istream &is, ClassName &obj); Declaration:
53
Summary of Standard Operator Overloading Implementation Shift Operators (for streaming) Operator (shift left / output) ostream &operator (ostream &os, const ClassName &obj) { return (os <<...); // return the final ostream } Definition: friend ostream &operator (ostream &os, const ClassName &obj); Declaration:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.