Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ref: Sebesta, Chapter 12; Lafore, Chapter 11

Similar presentations


Presentation on theme: "Ref: Sebesta, Chapter 12; Lafore, Chapter 11"— Presentation transcript:

1 Ref: Sebesta, Chapter 12; Lafore, Chapter 11
COS220 Concepts of PLs AUBG, COS dept Lecture 23m1 OOP Operator Overloading Ref: Sebesta, Chapter 12; Lafore, Chapter 11 12/29/2017 Assoc. Prof. Stoyan Bonev

2 Assoc. Prof. Stoyan Bonev
Lecture Contents: Part 1 Operator Overloading (lecture 23m1) Part 2 Friend functions (lecture 23m2) 12/29/2017 Assoc. Prof. Stoyan Bonev

3 Part 1 Operator Overloading

4 Assoc. Prof. Stoyan Bonev
Introduction Why do we need operator overloading? Examples: Class Counter Class Distance 12/29/2017 Assoc. Prof. Stoyan Bonev

5 Assoc. Prof. Stoyan Bonev
Introduction Why do we need operator overloading? For better readability Examples: Class Counter Class Distance 12/29/2017 Assoc. Prof. Stoyan Bonev

6 Assoc. Prof. Stoyan Bonev
Introduction Example: the Counter class . . . Counter c(100); c.IncCount(); 12/29/2017 Assoc. Prof. Stoyan Bonev

7 Assoc. Prof. Stoyan Bonev
Introduction Example: the Counter class . . . Counter c(100); c.IncCount(); OR c++; 12/29/2017 Assoc. Prof. Stoyan Bonev

8 Assoc. Prof. Stoyan Bonev
Introduction Example: the Counter class . . . Counter c(100); c.IncCount(); OR c+=1; 12/29/2017 Assoc. Prof. Stoyan Bonev

9 Assoc. Prof. Stoyan Bonev
Introduction Example: the Distance class . . . Distance d1(5,3.6), d2(6,4.5), d3; d3.AddDist1(d1,d2); d3 = d1.AddDist2(d2); 12/29/2017 Assoc. Prof. Stoyan Bonev

10 Assoc. Prof. Stoyan Bonev
Introduction Example: the Distance class . . . Distance d1(5,3.6), d2(6,4.5), d3; d3.AddDist1(d1,d2); d3 = d1.AddDist2(d2); OR d3 = d1 + d2; // OK 12/29/2017 Assoc. Prof. Stoyan Bonev

11 Overloaded Operators in C++

12 Overloaded operators as member functions of a class:
The Rule (C++): Overloaded operator always requires one less argument than its number of operands, since one operand is the object of which the operator is a member function. This rule not valid for friend functions. 12/29/2017 Assoc. Prof. Stoyan Bonev

13 Overloading unary operators
Class Counter File OverloadUnaryOprtr.cpp And/Or File OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

14 Counter overloaded operator ++
class Counter { void IncCount() { count++; } void operator++() { count++; } }; Counter c(100); c.IncCount(); ++c; // OK c.operator++(); // OK OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

15 Counter overloaded operator ++
class Counter { void IncCount() { count++; } void operator++() { count++; } }; Counter c(100); c.IncCount(); ++c; c.operator++(); //OK Counter d(300), e; ++d; //OK OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

16 Counter overloaded operator ++
class Counter { void IncCount() { count++; } void operator++() { count++; } }; Counter c(100); ++c; c.operator++(); //OK Counter d(300), e; ++d; //OK e = ++d; // NOT OK OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

17 Counter overloaded operator ++
class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val){ count = val; } unsigned getCount() { return count; } void setCount(unsigned val) {count = val; } void IncCount() { count++; } // overloaded unary operator ++, 1st version Counter operator++() { count++; return Counter(count); } }; 12/29/2017 Assoc. Prof. Stoyan Bonev

18 Counter overloaded operator ++
class Counter { private: unsigned count; public: Counter() { count = 0; } Counter(int val){ count = val; } unsigned getCount() { return count; } void setCount(unsigned val) {count = val; } void IncCount() { count++; } // overloaded unary operator ++, 2nd version Counter operator++() { Counter temp; count++; temp.count = count; return temp; } }; 12/29/2017 Assoc. Prof. Stoyan Bonev

19 Counter overloaded operator ++
void main() { Counter c1; c1.IncCount(); cout << "\n\n" << c1.getCount(); ++c1; Counter c2; c2 = ++c1; cout << "\nCounter c2 =" << c2.getCount(); cout << '\n'; } 12/29/2017 Assoc. Prof. Stoyan Bonev

20 Counter overloaded operator ++
Limitation on Increment Operators To distinguish between c2 = ++c1; c2 = c1++; Stroustrup describes an implementation that requires two overloaded operators operator++() // does prefix (++var) operator++(int) // does postfix (var++) 12/29/2017 Assoc. Prof. Stoyan Bonev

21 Overloading binary operators
Class Distance File OverloadBinaryAddOprtr.cpp And/Or File OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

22 Distance overloaded operator +
One more method: Distance operator+(Distance d2) { int ft = feet + d2.feet; float in = inches + d2.inches; if (in >=12.) { ft++; in-=12.; } return Distance (ft, in); } Distance d1(6, 5.18), d2=3.5, d3, d4, d5; d3 = d1 + d2; d4 = d ; d5 = d1; d3 = d1.operator+(d2); OprtrOvldCPP.cpp 12/29/2017 Assoc. Prof. Stoyan Bonev

23 Distance overloaded operators + , +=
class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } int getFeet() { return feet; } void setFeet(int ft) { feet = ft; } float getInches() { return inches; } void setInches(float in) { inches = in; } Distance operator +(Distance d1) { int ft; float in; ft = feet + d1.feet; in = inches + d1.inches; if (in >= 12.) { in -= 12.; ft++; } return Distance(ft, in); } // place to add method to overload += operator }; 12/29/2017 Assoc. Prof. Stoyan Bonev

24 Distance overloaded operators + , +=
class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } int getFeet() { return feet; } void setFeet(int ft) { feet = ft; } float getInches() { return inches; } void setInches(float in) { inches = in; } Distance operator +(Distance d1) { int ft; float in; ft = feet + d1.feet; in = inches + d1.inches; if (in >= 12.) { in -= 12.; ft++; } return Distance(ft, in); } Distance operator +=(Distance d1) { feet = feet + d1.feet; inches = inches + d1.inches; if (inches >= 12.) { inches -= 12.; feet++; } return Distance(feet, inches); }; 12/29/2017 Assoc. Prof. Stoyan Bonev

25 Distance overloaded operators + , +=
void main () { Distance d1(5, 6.8), d2(3, 4.5), d5, d9(1, 1.0), d10; d5 = d1 + d2; cout << "\n Distance d5 " << d5.getFeet() << " " << d5.getInches() ; d5 += d9; d10 = d1 += d9; cout << "\n Distance d10 " << d10.getFeet() << " " << d10.getInches() ; } 12/29/2017 Assoc. Prof. Stoyan Bonev

26 Distance overloaded comparison operators
class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } int getFeet() { return feet; } void setFeet(int ft) { feet = ft; } float getInches() { return inches; } void setInches(float in) { inches = in; } bool operator < (Distance) const; }; bool Distance::operator < (Distance d2) const { float bf1 = feet + inches/12; float bf2 = d2.feet + d2.inches/12; return (bf1 < bf2) ? true : false; } void main() { Distance dist1(5, 6.8), dist2(3, 4.5); if (dist1<dist2) cout << “\n dist1 object is less than dist2 object”; // OR if (dist1.operator<(dist2)) cout << “\n dist1 is less than dist2”; 12/29/2017 Assoc. Prof. Stoyan Bonev

27 Operator overloading-conclusion
Use similar meanings and similar syntax Use overloaded operators in the same way they are used for basic types. Not all operators can be overloaded Member access or dot (.) operator Scope resolution operator (::) Conditional operator ( ? : ) Pointer-to member operator ( .* ) 12/29/2017 Assoc. Prof. Stoyan Bonev

28 Overload the subscript operator []
The subscript operator, [ ], normally used to access array elements, can be overloaded. For example, C++ does not protect arrays from addressing exception. Let’s try to create “safe” array: such that automatically checks the index numbers used to access the array, to ensure that they are not out of bounds. 12/29/2017 Assoc. Prof. Stoyan Bonev

29 Overload the subscript operator []
File OverloadBinaryBracketOperator.cpp. 12/29/2017 Assoc. Prof. Stoyan Bonev

30 Overload the subscript operator []
Three example programs will illustrate the implementation of a “safe” array, each one using a different approach to inserting and reading the array elements Separate put() and get() functions A single access() function using return by reference The overloaded [ ] operator using return by reference 12/29/2017 Assoc. Prof. Stoyan Bonev

31 Separate put() and get() functions
class SafeArray { private: int arr[100]; public: void putel(int n, int elvalue) { if (n<0 || n>=100) {cout<<“Index out of bounds”; exit(1); } arr[n] = elvalue; } int getel(int n) const { return arr[n]; } }; void main() { SafeArray sa1; for (int j=0; j<100; j++) sa1.putel(j, j*10); // insert array elements for (int j=0; j<100; j++) // display elements cout << “Element “<<j<<“ is =“<<sa1.getel(j)<< endl; } 12/29/2017 Assoc. Prof. Stoyan Bonev

32 A single access() function using return by reference
Instead of using separate get and put function, it is possible to use the same member function both to insert and read “safe” array elements. The secret is to return the value from the function by reference. This means the function may be placed on the left side and on the right side of the assignment operator. Reminder: functions returning values by reference 12/29/2017 Assoc. Prof. Stoyan Bonev

33 Functions returning reference
They are named pseudo functions as well. Pseudo functions may appear as left operand and/or as right operand of the assignment operator: left operand: setx() = <expression> right operand: y = …setx()… - expression operand both left and right operand: setx() = …setx()… 12/29/2017 Assoc. Prof. Stoyan Bonev

34 Functions returning reference
int x; int& setx(); void main() {setx()=218; cout<<x; } int& setx() { return x; } 12/29/2017 Assoc. Prof. Stoyan Bonev

35 Functions returning reference
#include <iostream> using namespace std; int x; int& setx(); void main() { int y; y = setx(); cout << '\n' << y; setx() = 218; cout << '\n' << x; setx() = setx() + 12; cout<<‘\n’x<<y; } int& setx() { return x; } 12/29/2017 Assoc. Prof. Stoyan Bonev

36 A single access() function using return by reference
class SafeArray { private: int arr[100]; public: int& access(int n) { if (n<0 || n>=100) {cout<<“Index out of bounds”; exit(1); } return arr[n]; } }; void main() { SafeArray sa1; for (int j=0; j<100; j++) sa1.access(j) = j*10; // insert array elements for (int j=0; j<100; j++) // display elements { int temp = sa1.access(j); cout << “Element “<<j<<“ is =“<<temp<< endl; } 12/29/2017 Assoc. Prof. Stoyan Bonev

37 The overloaded [] operator using return by reference
The same approach is applied in overloading subscript [ ] operator. The subscript operator appears to the left side and to the right side of an assignment operator. 12/29/2017 Assoc. Prof. Stoyan Bonev

38 The overloaded [] operator using return by reference
class SafeArray { private: int arr[100]; public: int& operator [](int n) { if (n<0 || n>=100) {cout<<“Index out of bounds”; exit(1); } return arr[n]; } }; void main() { SafeArray sa1; for (int j=0; j<100; j++) sa1[j] = j*10; // insert array elements for (int j=0; j<100; j++) // display elements { int temp = sa1[j]; cout << “Element “<<j<<“ is =“<<temp<< endl; } 12/29/2017 Assoc. Prof. Stoyan Bonev

39 Overloaded Operators in VBasic

40 Overloaded Operators in VB
???? Open file OprtrovldVb.vb Operator overloading not supported in VB 12/29/2017 Assoc. Prof. Stoyan Bonev

41 Overloaded Operators in C#

42 Overloaded Operators in C#
Open file OprtrovldCs.cs 12/29/2017 Assoc. Prof. Stoyan Bonev

43 Overloaded Operators in C#
class Counter { private int count; public Counter() { count = 0; } public Counter(int val ) { count = val; } public int getCount() { return count; } public void setCount(int val ) { count = val; } public void IncCount() { count++; } // overloaded unary operator ++, first version public static Counter operator++(Counter p) return new Counter(p.getCount()+1); } }; // end of class Counter 12/29/2017 Assoc. Prof. Stoyan Bonev

44 Overloaded Operators in C#
class Program { static void Main(string[] args) Counter c1=new Counter(50); c1.IncCount(); Console.WriteLine("\n Counter c1 {0}",c1.getCount()); c1++; Counter c2 = new Counter(); c2 = ++c1; Console.WriteLine("\n Counter c2 {0}",c2.getCount()); } } // end of class Program 12/29/2017 Assoc. Prof. Stoyan Bonev

45 Overloaded Operators in C#
class Distance { private int feet; float inches; public Distance() { feet = 0; inches = 0.0F; } public Distance(int ft, float inch) { feet=ft;inches=inch;} public int getFeet() { return feet; } public void setFeet(int ft) { feet = ft; } public float getInches() { return inches; } public void setInches(float inch) { inches = inch; } public static Distance operator +(Distance d1, Distance d2) int ft; float im; ft = d1.getFeet() + d2.getFeet(); im = d1.getInches() + d2.getInches(); if (im >= 12.0F){ im -= 12.0F; ft++; } return new Distance(ft, im); }// end of operator+ }; // end of class Distance 12/29/2017 Assoc. Prof. Stoyan Bonev

46 Overloaded Operators in C#
class Program { static void Main(string[] args) Distance d1 = new Distance(5, 6.8F), d2 = new Distance(3, 4.5F), d5 = new Distance(); d5 = d1 + d2; Console.WriteLine("\n Distance d5 {0} {1}", d5.getFeet(), d5.getInches() ) ; } } // end of class Program 12/29/2017 Assoc. Prof. Stoyan Bonev

47 Overloaded Operators in Java

48 Overloaded Operators in Java
Operator Overloading not supported in Java 12/29/2017 Assoc. Prof. Stoyan Bonev

49 Part 2 Friend Functions (separate lecture 23m2)_

50 Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts Source: Longman dictionary 1987 50

51 Thank You For Your Attention


Download ppt "Ref: Sebesta, Chapter 12; Lafore, Chapter 11"

Similar presentations


Ads by Google