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
Operator overloading redefine the operations of operators
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Class and Objects.
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.
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.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Operator overloading Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Chapter -6 Polymorphism
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 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
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.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
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.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Friend functions.
Function Overloading Can enables several function Of same name
Lecture 5: Classes (continued) Feb 1, 2005
Templates.
Operator Overloading CSCE 121 J. Michael Moore
More on Classes Programming in C++ Fall 2008
Zhen Jiang West Chester University
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
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.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS150 Introduction to Computer Science 1
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
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: