Computer Programming FAST-NU FSB-CH Campus

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Introduction to Programming Lecture 31. Operator Overloading.
Operator overloading. Operatorsand precedence zLeft to right associative y:: scope resolution y. direct member access y-> indirect member access y[] subscripting.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Overloading Operators COSC 1567 C++ Programming Lecture 7.
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 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.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Case Study - Fractions Timothy Budd Oregon State University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator overloading II
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
C++ LANGUAGE MULTIPLE CHOICE QUESTION SET-3
Chapter 7: User-Defined Functions II
Chapter 7: Expressions and Assignment Statements
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object-Oriented Programming (OOP) Lecture No. 21
Chapter 2 Assignment and Interactive Input
Chapter 7: Expressions and Assignment Statements
Objectives Define polymorphism Overload functions
Chapter 14: More About Classes.
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
Operator overloading Dr. Bhargavi Goswami
COMPUTER 2430 Object Oriented Programming and Data Structures I
CISC181 Introduction to Computer Science Dr
Java Programming Language
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
Lecture 14: Problems with Lots of Similar Data
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
CS31 Discussion 1D Winter18: week 6
CS31 Discussion 1H Fall18: week 9
OPERATORS in C Programming
Presentation transcript:

Computer Programming FAST-NU FSB-CH Campus Lecture No. 19 Computer Programming Introduction

Introduction The variables of native data types can perform a number of different operations (functions) using operators ( +, - , / , *) Example: a + b * c Example: if ( a < b ) However, with user defined (classes) objects we can not use operators Example: class obj1, obj2; if ( ob1 < obj2 )

Introduction With the help of operator overloading we can add operator functionality in the class’s objects However, before using any kind of operator we need to implement its functionality in the class

In order to add operator functionality in the class Operator Overloading In order to add operator functionality in the class First create a function for the class Set the name of the function with the operator name operator+ for the addition operator ‘+’ operator> for the comparison operator ‘>’

double operator+ (Employee& emp); Extended Example Employee class and objects class Employee { private: int idNum; double salary; public: Employee(int id, double salary); double addTwo (Employee& emp); double operator+ (Employee& emp); double getSalary() { return salary; } }

The member functions ‘addTwo’ and operator+ double Employee::addTwo(Employee& emp) { double total; total = salary + emp.getSalary(); return(total); } double Employee::operator+(Employee& emp)

Using the Member Functions double sum; Employee Clerk (111, 10000), Driver (222, 6000); // these three statements do the same thing sum = Clerk.addTwo(Driver); sum = Clerk.operator+(Driver); sum = Clerk + Driver; // 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

> operator bool Employee::operator>(Employee& e) { return(seniority > e.seniority); } called from the program like this: if (emp1 > emp2) emp1 accounts for calling object, and emp2 becomes e

Invoking Objects If the operator is binary but there is only one explicit argument, the ‘invoking instance’ is assumed to be the one on the left hand side of the expression. class Date { public: // member functions Date operator=(Date& d); }; int main (void) s1 = s2; // instead of s1.operator=(s2); }

Operator Overloading Syntax Although the syntax of defining prototype of operator overloading is following in most of cases datatype operator+ (datatype) However, for some operators, there is little bit change is above syntax ++, -- operators >> , << operators & and [] operators

Operator ++ and –- are different to other operators of C++ Overloading ++ and -- Operator ++ and –- are different to other operators of C++ We can call them either in the form of prefix (++i) before a object or in the form of postfix (i++) after a object But in both cases, the calling object will be i.

Prefix makes the change, and then it processes the variable. i++ and ++i ? Prefix makes the change, and then it processes the variable. Postfix processes the variable, then it makes the change.  i = 1;  j = ++i;  (i is 2, j is 2)  i = 1;  j = i++;  (i is 2, j is 1)

Overloaded ++ class Inventory { private: int stockNum; int numSold; public: Inventory(int stknum, int sold); void operator++(); }; void Inventory::operator++() numSold++; }

Use of the operator ++ int main ( ) { Inventory someItem(789, 84); // the stockNum is 789 // the numSold is 84 ++someItem; Inventory Item2 = ++someItem; //will this instruction work }

Overloaded ++ class Inventory { private: int stockNum; int numSold; public: Inventory(int stknum, int sold); Inventory& operator++(); }; Inventory& Inventory::operator++() Inventory *object = new Inventory(0,0); numSold++; object->numSold = numSold; return(*object); }

Use of the operator ++ int main ( ) { Inventory someItem(789, 84); // the stockNum is 789 // the numSold is 84 ++someItem; // is this correct? Inventory Item2 = ++someItem; }

Problem The definition of the prefix operator is easy enough. It increments the value before any other operation. But how will C++ be able to tell the difference between a prefix ++ operator and a postfix ++ operator Answer: overloaded postfix operators take a dummy argument (just for differentiation between postfix and prefix).

Postfix operator dummy argument Inventory& Inventory::operator++() // prefix version { Inventory *object = new Inventory(0,0); numSold++; object->numSold = numSold; return(*object);} Inventory& Inventory::operator++(int) // postfix version dummy argument

Use of the operator ++ int main ( ) { Inventory someItem(789, 84); // the stockNum is 789 // the numSold is 84 Inventory Item2 = ++someItem; Inventory Item3 = someItem++; }

Assignment Operator (=) class Employee { private: int idNum; double salary; public: Employee ( ) { idNum = 0, salary = 0.0; } void setValues (int a, int b); void operator= (double ); } void Employee::setValues ( int idN , double sal ) { salary = sal; idNum = idN; } void Employee::operator = (double sal) { salary = sal; }

Assignment Operator (=) int main ( ) { Employee emp1; emp1.setValues(10,33.5); Employee emp2; emp2 = 44.6; // emp2 is calling object }

Assignment Operator (=) class Employee { private: int idNum; double salary; public: Employee ( ) { idNum = 0, salary = 0.0; } void setValues (int a, int b); void operator= (Employee &emp ); } void Employee::setValues ( int idN , double sal ) { salary = sal; idNum = idN; } void Employee::operator = (Employee &emp) { salary = emp.salary; }

Assignment Operator (=) int main ( ) { Employee emp1; emp1.setValues(10,33.5); Employee emp2; emp2 = emp1; // emp2 is calling object }

Comparison Operator (==) class Employee { private: int idNum; double salary; public: Employee ( ) { idNum = 0, salary = 0.0; } void setValues (int a, int b); bool operator== (Employee &emp ); } void Employee::setValues ( int idN , double sal ) { salary = sal; idNum = idN; } bool Employee::operator == (Employee &emp) { return (salary == emp.salary); }

Assignment Operator (=) int main ( ) { Employee emp1; emp1.setValues(10,33.5); Employee emp2; emp2.setValues(10,33.1); if ( emp2 == emp1 ) cout <<“Both objects have equal value”; else cout <<“objects do not have equal value”; }

Examples Subscript operator[] With the help of [ ] operator, we can define array style syntax for accessing or assigning individual elements of classes Examples Student semesterGPA; semesterGPA[0] = 3.5; semesterGPA[1] = 3.3;

Subscript operator[] class Student { private: double gpa[8]; public: { gpa[0] = 3.5; gpa[1] = 3.2; gpa[2] = 4; gpa[3] = 3.3; gpa[4] = 3.8; gpa[5] = 3.6; gpa[6] = 3.5; gpa[7] = 3.8; } double& opeator[] (int Index); double& Student::operator [] (int Index) { return gpa[Index];

Subscript operator[] int main ( ) { Student semesterGPA; semesterGPA[0] = 3.7; double gpa = semesterGPA[4]; }

How the statement executes Subscript operator[] How the statement executes (semesterGPA[0]=3.7;) The[] has highest priority than the assignment operator, therefore semesterGPA[0] is processed first. semesterGPA[0] calls operator [ ], which then return a reference of semesterGPA.gpa[0].

Subscript operator[] The return value is reference to semesterGPA.gpa[0], and the statement semesterGPA[0] = 3.7 is actually integer assignment. int main ( ) { Student semesterGPA; semesterGPA[0] = 3.7; // the above statement is processed like as semesterGPA.gpa[0] = 3.7 }