CSC241: Object Oriented Programming

Slides:



Advertisements
Similar presentations
1 CSC241: Object Oriented Programming Lecture No 21.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
1 CSC241: Object Oriented Programming Lecture No 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
1 CSC241: Object Oriented Programming Lecture No 11.
CS Object Oriented Programming Using C++
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 13 Operator Overloading 1. Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely.
1 CSC241: Object Oriented Programming Lecture No 08.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Operator Overloading Ritika Sharma.
Visit for more Learning Resources
Chapter 7: Expressions and Assignment Statements
Operator Overloading; String and Array Objects
CSC241: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
CSC241: Object Oriented Programming
Visit for more Learning Resources
Object-Oriented Programming (OOP) Lecture No. 21
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Objectives Define polymorphism Overload functions
Object-Oriented Programming (OOP) Lecture No. 16
ITM 352 Expressions, Precedence, Working with Strings Class #5
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Introduction to C++ Programming
Operator Overloading; String and Array Objects
Chapter 2: Basic Elements of Java
Operator Overloading; String and Array Objects
Chapter 15 Pointers, Dynamic Data, and Reference Types
C Operators, Operands, Expressions & Statements
Object Oriented Programming Using C++
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 20
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
CS150 Introduction to Computer Science 1
Operator Overloading; String and Array Objects
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Objects as Function Arguments
Presentation transcript:

CSC241: Object Oriented Programming Lecture No 09

Previous Lecture - - Operator overloading Overloading unary operator ++ - - Argument of overloaded function Post increment Counter operator ++ () { return Counter(++count); } Counter operator ++ (int) { return Counter(count++);

Today’s Lecture Example : Distance class Overloading binary operator overloading ++ and – ++ add 1 in feet and 1 in inches, -- subtract 1 from feet and 1 from inches Prefix and postfix Overloading binary operator

Example – Distance class Overload pre-increment and pre-decrement unary operator for distance class such that Increment: add 1 in feet and 1 in inches Decrement: subtract 1 from feet and 1 from inches Pre increment – prefix Post increment – post fix Write to program

Overloading Binary Operators Binary operator can be overloaded as easy as unary operator by using a non static fucntion Distance objects could be added using a member function add_dist(): dist3.add_dist(dist1, dist2); By overloading the + operator we can reduce this dense-looking expression to dist3 = dist1 + dist2;

Distance class 3 1.5 2 2.25 5 10 dist1 feet inches dist2 feet inches class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “ : ” << inches ; } Distance operator + ( Distance d2) const{ int f = feet + d2.feet; float i = inches + d2.inches; if(i >= 12.0) { i -= 12.0; f++; } return Distance(f,i); }; Distance class dist1 feet inches 1.5 3 dist2 feet inches 2.25 2 dist3 feet inches 5 3.75 dist4 feet inches d2 feet inches 10 2 7.5 Distance dist1(3, 4.5); Distance dist2(11, 6.25); Distance dist3; dist3 = dist1 + dist2; 2.25 feet inches 5 Distance dist4; dist4 =dist3 + dist1 + dist2; 3.75

Cont. dist3 = dist1 + dist2 The argument on the left side of the operator (dist1 in this case) is the object of which the operator is a member The object on the right side of the operator (dist2) is as an argument to the operator + function The operator returns a object by value, which is assigned to dist3

Overloaded binary operator: one argument

Cont. In the operator+() function, the left operand is accessed directly – since this is the object of which the operator is a member The right operand is accessed as the function’s argument – as d2.feet and d2.inches An 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 That’s why unary operators require no arguments Go to program

Concatenating Strings int main() { char str1[12] = "hello"; char str2[ ] = "world"; strcat(str1, str2); cout << "str1 = " << str1 << endl; cout << "str2 = " << str2 << endl; return 0; } h e l o \0 str1[12] w o r l d \0 str2[6] h e l o \0 str1[12] w r d Program Output str1 = helloworld str2 = world The + operator cannot be used to concatenate C-strings. That is, you can’t say str3 = str1 + str2; we can overload the + operator to perform such concatenation

String class Program Output Hello world ! Hello world ! main() { class String { private: char str[80]; public: String(){ strcpy(str, “"); } String( char s[ ] ) { strcpy(str, s); void display() const { cout << str; String operator + (String ss) const { String temp; if( strlen(str) + strlen(ss.str) < 80 ) { strcpy(temp.str, str); strcat(temp.str, ss.str); else { cout << “\nString overflow”; exit(1); } return temp; }; String class main() { String s1 = “Hello ”; //String s1("\Hello "); String s2 = “world !”; //String s2(“ world !"); String s3; s1.display(); s2.display(); s3 = s1 + s2; s3.display(); } Program Output Hello world ! Hello world !

Cont. Hello \0 s1 world !\0 s2 s3 Hello world!\0 ss world! \0 temp String operator + (String ss) const { String temp; if( strlen(str) + strlen(ss.str) < 80 ) { strcpy(temp.str, str); strcat(temp.str, ss.str); } else { cout << “\nString overflow”; exit(1); } return temp; String s1 = “Hello ”; String s2 = “world !”; String s3; s3 = s1 + s2; Hello \0 s1 world !\0 s2 s3 Hello world!\0 ss world! \0 temp Hello \0 World! \0 Go to program

Overloading -, + and = binary Operators class ThreeD {    int x, y, z;  public:    ThreeD() { x = y = z = 0; }    ThreeD(int i, int j, int k)  { x = i; y = j; z = k; }    ThreeD operator+(ThreeD op2);    ThreeD operator=(ThreeD op2);    ThreeD operator-(ThreeD op2);     void show() ;  };  // Overload subtraction.  ThreeD ThreeD::operator-(ThreeD op2)  {     ThreeD temp;     temp.x = x - op2.x;     temp.y = y - op2.y;     temp.z = z - op2.z;     return temp;  } // Overload +.  ThreeD ThreeD::operator+(ThreeD op2) {     ThreeD temp;     temp.x = x + op2.x;      temp.y = y + op2.x;      temp.z = z + op2.z;      return temp;  } 

Cont. // Overload assignment.  ThreeD ThreeD::operator=(ThreeD op2){    x = op2.x;     y = op2.y;     z = op2.z;     return *this;  }  void ThreeD::show() {   cout << x << ", ”<< y << ", ”<< z << "\n";  }  main() {    ThreeD a(1, 2, 3), b(10, 10, 10), c;     cout << "Original value of a: ";     a.show();     cout << "Original value of b: ";     b.show();     c = b - a;     cout << “b - a: ";     c.show(); } PROGRAM OUTPUT: Original value of a: 1, 2, 3 Original value of b: 10, 10, 10 b - a: 9, 8, 7

Multiple Overloading We’ve seen different uses of the + operator: to add distances and to concatenate strings. You could put both these classes together in the same program, and C++ would still know how to interpret the + operator: It selects the correct function to carry out the “addition” based on the type of operand d1 = d2 + d3; s1 = s2 + s3;

Comparison Operator < main() function Distance dist1(3, 4.5); Distance dist2(6, 2.5); if( dist1 < dist2 ) cout << “\ndist1 is less than dist2”; else cout << “\ndist1 is greater than (or equal to) dist2”; bool Distance::operator < (Distance d2) const { float bf1 = feet + inches/12; float bf2 = d2.feet + d2.inches/12; return (bf1 < bf2) ? true : false; } Go to program

Arithmetic assignment: Operator += main() function Distance dist1(3, 2.5); Distance dist2(5 3.25); dist1 += dist2; void Distance::operator += (Distance d2) { feet += d2.feet; inches += d2.inches; if(inches >= 12.0) { inches -= 12.0; feet++; } Go to program

The Subscript Operator [ ] The subscript operator, [], which is normally used to access array elements, can be overloaded. This is useful, for example if we want to make a “safe” array: One that automatically checks the index numbers you use to access the array, to ensure that they are not out of bounds. To be useful, the overloaded subscript operator must return by reference.

Cont. To see why this is true, lets see three example programs that implement 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 All programs create a class called safearray, only member data is an array of 100 int values, and all three check to ensure that all array accesses are within bounds.

Separate get() and put() Functions const int LIMIT = 100; class safearray { private: int arr[LIMIT]; public: void putel(int n, int elvalue) { if( n< 0 || n>=LIMIT ) { cout << “\nIndex out of bounds”; exit(1); } arr[n] = elvalue; int getel(int n) const { if( n< 0 || n>=LIMIT ){ return arr[n]; }; int main() { int temp; safearay sa1; for(int j=0; j<LIMIT; j++) sa1.putel(j, j*10); for(j=0; j<LIMIT; j++) { temp = sa1.getel(j); cout << “Element “ << j << “ is “ << temp ; }

Single access() Function Returning by Reference const int LIMIT = 100; class safearray { private: int arr[LIMIT]; public: int& access(int n) { if( n< 0 || n>=LIMIT ) { cout << “\nIndex out”; exit(1); } return arr[n]; main() { int temp; safearray sa1; for(int j=0; j<LIMIT; j++) sa1.access(j) = j*10; for(j=0; j<LIMIT; j++) { temp = sa1.access(j); cout << “Element “ << j << “ is “ << temp ; } The statement sa1.access(j) = j*10; causes the value j*10 to be placed in arr[j], the return value of the function

Overloaded [] Operator Returning by Reference const int LIMIT = 100; class safearray { private: int arr[LIMIT]; public: int& operator [ ](int n) { if( n< 0 || n>=LIMIT ) { cout << “\nIndex out of bounds”; exit(1); } return arr[n]; main() { int temp; safearray sa1; for(int j=0; j<LIMIT; j++) sa1[ j ] = j*10; for(j=0; j<LIMIT; j++) { temp = sa1[ j ]; cout << “Element “ << j << “ is “ << temp ; }

Overloading Restrictions Can not overload the operators as they apply to builit-in data types.    Example:   y = 5;                      x = y + 5; // overloading + operator so that x becomes 15 is illegal. Must respect the original "Functional" template of the operators. You can not convert a unary operator to a binary operator. Example:  y = x ++ z; //Illegal You can not change the operator's precedence. Example:   Can not make the + operator higher precedence than the * operator.