Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Class and Objects.
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer,
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Operator Overloading.
Overloading C++ supports the concept of overloading Two main types
Pointer to an Object Can define a pointer to an object:
Function Overloading Can enables several function Of same name
Lecture 5: Classes (continued) Feb 1, 2005
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
A First C++ Class – a Circle
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Presentation transcript:

Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create several functions of the same name that perform similar tasks but on different data types

Square function int square(int x) { return x*x; } double square(double y) { return y*y;} float square(float z) { return z*z;}

Use overloaded function Complier will search for the match function prototype int main() { int x=7; double y=7.5; cout<<“square of “<<x<<“ is “ <<square(x)<<endl; cout<<“Square of “<<y<<“is “ <<square(y)<<endl; }

Friend function and friend class A friend function of a class is defined outside that class scope Has right to access private members of the class Using friend functions can enhance performance Often appropriate when a member function cannot be used for certain operations

Declare a function as friend To declare a function as a friend of a class, precede the function prototype in class definition with key word friend To declare a class Two as a friend of One, place friend class Two; in the definition of class One

Friendship Friendship is granted, not taken Friendship is not symmetric If B is friend of A, I.e. A declared B as friend, you cannot infer A is friend of B unless B declare A as friend Friendship is not transitive A is friend of B, and B is friend of C, you cannot say that A is friend of C

Example class Count { friend void setX(Count &, int); public: Count(){x=0;} void print() const {cout<<x<<endl;} private: int x; };

Example continue void setX(Count &c, int val) { c.x = val; } int main() { Count counter; counter.print(); setX(counter, 8); return 0; }

Use this pointer Every object has access to its own address through a pointer called this This pointer is not part of object This pointer is passed into object (by compiler) as implicit first argument on every non-static member function

class Test { int x; public: Test( int =0); void print() const; }; Test::test(int a){ x = a;} void Test::print() const { cout<<“ x = “<<x x = “ x <<“\n(*this).x = “<<(*this).x <<endl; }

int main() { Test t(12); t.print(); return 0; } x = 12 this->x = 12 (*this).x = 12 Output:

Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer, floating These operators are overloaded C++ allow programmer to overload most operators

Can we make Complex class easier to use as follow? int main() { Complex x,y,z; cout<<“Type in two complex numbers\n”; cin>>x >>y; z = x + y; cout<< x <<“+” <<y<<“=“ <<z<<endl; } Type in two complex numbers (2,3) (3,4) (2,3) + (3,4) = (5,7)

Yes, Overload the operators Operator are overloaded by writing a function definition( header and body) Function name become the keyword operator followed by the symbol for the operator being overloaded operator+ would be used to overload the addition operator(+) Precedence and associativity of an operator cannot be changed by overloading

Where to define overloading operator As non member function Must be friend of the class So it is in class definition class Complex { friend ostream& operator<< (ostream&, const Complex &);... } Function name Return type parameters

Implementation ostream & operator<<(ostream & out, const Complex &c) { out<<“(“<<c.real<<“,”<<c.img<<“)”; return out; // enables cout<<x<<y; }

Overload operator >> In class definition class Complex { friend ostream& operator<< (ostream &, const Complex &); friend istream& operator>> (istream&, Complex &);... }

implementation istream & operator>>( istream & input,Complex & c) { input.ignore(); input>>c.real; input.ignore(); input>>c.img; input.ignore(); return input; }