1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.

Slides:



Advertisements
Similar presentations
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Advertisements

Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
1 Overloading Lesson #8 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Object Oriented Programming in C++ Chapter5 Operator Overloading.
1 CSC241: Object Oriented Programming Lecture No 13.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
1 CSC241: Object Oriented Programming Lecture No 12.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
OPERATOR OVERLOADING Customised behaviour of operators.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
CONSTRUCTOR AND DESTRUCTORS
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
1 CSC241: Object Oriented Programming Lecture No 05.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
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.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
1 CSC241: Object Oriented Programming Lecture No 08.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Overloading C++ supports the concept of overloading Two main types
Operator Overloading Ritika Sharma.
Visit for more Learning Resources
CSC241: Object Oriented Programming
Introduction Rules Types Programs
Visit for more Learning Resources
CSC241: Object Oriented Programming
Object-Oriented Programming (OOP) Lecture No. 21
This technique is Called “Divide and Conquer”.
Operator Overloading BCA Sem III K.I.R.A.S.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Operator Overloading; String and Array Objects
Object Oriented Programming Using C++
Operator Overloading.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Class: Special Topics Overloading (methods) Copy Constructors
TOPIC: FUNCTION OVERLOADING
Constructors and Deconstructor
Objects as Function Arguments
Classes Member Qualifiers
Presentation transcript:

1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

2

Function Overloading Multiple definitions for same function in same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. 3

Function Overloading (fo_print.cpp) #include class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; pd.print(5); // print integer pd.print( ); // print float pd.print("Hello C++"); //print character } 4 Program: Write a program of function overloading, which uses one function with different arguments.

Function Overloading (fo_print2.cpp) #include class printData { public: void print(int i, int j) { cout << "Printing int: " << i*j << endl; } void print(double d, double d2) { cout << "Printing float: " << d*d2<< endl; } void print(char* c, char* d) { cout << "Printing character: " << c<<d << endl; } }; int main(void) { printData pd; pd.print(5,10); pd.print(10.01,20.034); pd.print("Hello C++","Good Bye"); } 5 Program: Write a program of function overloading, which uses one function with different arguments, also perform multiplication of arguments.

Function Overloading (fo_data.cpp) #include class printData { public: void print(int i, int j) { cout << "Printing int: " << i*j << endl; } void print(int i, double d) { cout << "Printing int and double" << i*f<< endl; } void print(int i, char* c) { cout << "Printing "<< i<<c << endl; } }; int main(void) { printData pd; pd.print(5,10); pd.print(10,20.034); pd.print(99, " Hello C++"); } 6 Program: Write a program of function overloading, which used different data types.

7

Types of Operators There are two types of operators to overload: Unary Binary Unary operators: Type of Operators works on only one operand. Operand is a variable acted on by an operator. Prefix Unary Operator: Unary operators can appear before operand. i-e: ++a; --a; -3; !(3) Postfix Unary Operator: Unary operator can also appear after operand. i-e: a++; a--; Binary Operators: Type of Operators works on two operands i-e: a+b; a/b; (+, -, *, /, %, =, ) 8

Prefix & Post Fix Example Pre_post.cpp #include int main() { int a=5,b=5; cout<<"Post fix a: "<<a++<<endl; cout<<"After post fix a: "<<a<<endl; cout<<"Pre fix b: "<<++b<<endl; } 9

Operator Overloading  Definition Operator overloading is to allow the same operator for multiple implementations.  Syntax return-type operator operator-symbol (parameter-list) {} //operator is keyword  Example void operator ++ () { // Body of function } 10

Restrictions on Operator Overloading C++ operators that can be overloaded C++ Operators that cannot be overloaded

Restrictions on Operator Overloading Overloading restrictions Precedence of an operator cannot be changed Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created Use only existing operators

13

Overloading Unary Operator (op_count.cpp) #include class Counter { private: unsigned int count; public: Counter(): count(0) {/*emptly body*/} unsigned int get_count() {return count;} void operator++() { ++count; } }; //Counter =constructor. Count=variable int main() { Counter c1,c2; //Define & Initialize cout<<"\n c1= "<<c1.get_count(); //display cout<<"\n c2= "<<c2.get_count(); ++c1;//increment c1 //c1.inc_count(); ++c2;//increment c2 cout<<"\n c1= "<<c1.get_count(); //display again cout<<"\n c2= "<<c2.get_count(); cout<<endl; } 14 Program: Write a program using class and constructor, which uses overloaded operator ++.

The operator Keyword (op_count.cpp) void operator++(); The declaratory syntax tells the compiler to call this member function whenever the ++ operator is encountered. Compiler can distinguish between overloaded operator by looking at the data type of operands. 15

Operator Arguments In main function ++ operator is applied to specific object. Operator ++ takes no arguments Operator ++ only increments the count data in the object of which it is member. 16 (op_count.cpp)

Operator Return Values The ++ operator has a return type void in the operator ++() function, any try to get its values to another variable will generate error. c1=++c2; This is wrong because counter objects must always appear standalone with its operand. 17 (op_count.cpp)

18

Overloading Binary Operators Binary Operators: Type of Operators works on two operands i-e: a+b; a/b; (+, -, *, /, %, =, ) You can overload the Binary operators to compare two objects. 19

Overloading Binary Operator (oo_binary.cpp) #include class Distance { private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Distance() { feet = 0; inches = 0; } Distance(int f, int i) { feet = f; inches = I; } // method to display distance void displayDistance() { cout << "F: " << feet << " I:" << inches <<endl; } // overloaded minus (-) operator Distance operator- () { feet = -feet; inches = -inches; return Distance(feet, inches); } }; int main() { Distance D1(10, 15), D2(-20, 25); -D1; // apply negation D1.displayDistance(); // display D1 -D2; // apply negation D2.displayDistance(); // display D2 } 20 Program: Write a program using class and constructor, which uses overloaded operator negation(-).

Overloading Binary Operator (oo_plus2.cpp) #include class Distance { private: int feet; int inches; public: Distance():feet(0),inches(0.0) { } Distance(int ft, float in): feet(ft),inches(in) { } void getdist() { cout >feet; cout >inches; } void showdist()const { cout<<feet<<"\'-"<<inches<<'\"'; } Distance operator+(Distance)const; }; // Function Distance 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); } 21

Overloading Binary Operator (oo_plus2.cpp) int main() { Distance dist1,dist3,dist4; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1+dist2; dist4 = dist1+dist2+dist3; cout<<"dist1 ="; dist1.showdist();cout<<endl; cout<<"dist2 ="; dist2.showdist();cout<<endl; cout<<"dist3 ="; dist3.showdist();cout<<endl; cout<<"dist4 ="; dist4.showdist();cout<<endl; return 0; } 22

Overloading Binary Operator (oo_plus2.cpp) 23

Overloading Binary Operator (oo_plus2.cpp) Scope resolution operator(::) is used to define a function outside a class or when we want to use a global variable but also has a local variable with same name. () const member function can not modify the data. 24