Zhen Jiang West Chester University

Slides:



Advertisements
Similar presentations
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Advertisements

Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
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.
Function Introduction
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
計算機概論實習 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Main Objectives: To understand how to construct programs modularly from.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 Review structures  Program to demonstrate a structure containing a pointer.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
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.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Functions.
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
Command Line Arguments
Function Overloading.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Chapter 3 - Functions Outline 3.1 Introduction
Functions Najah Alsubaie Kingdom of Saudi Arabia
Dr. Shady Yehia Elmashad
Chapter 3 - Functions Outline 3.1 Introduction
Methods Additional Topics
توابع در C++ قسمت اول اصول كامپيوتر 1.
CSC1201: Programming Language 2
Chapter 3 - Functions Outline 3.1 Introduction
CS1201: Programming Language 2
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3 - Functions Outline 3.1 Introduction
5.1 Introduction Pointers Powerful, but difficult to master
Variables have attributes
Chapter 15 - C++ As A "Better C"
CS1201: Programming Language 2
C++ Programming Lecture 17 Pointers – Part I
Arrays Arrays A few types Structures of related data items
CSC1201: Programming Language 2
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Programming Fundamental
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

Zhen Jiang West Chester University Function Overloading Zhen Jiang West Chester University

Overloaded functions distinguished by signature Function overloading Functions with same name and different parameters Should perform similar tasks I.e., function to square ints and function to square floats int square( int x) {return x * x;} float square(float x) { return x * x; } Overloaded functions distinguished by signature Based on name and parameter types (order matters) Name mangling Encodes function identifier with parameters Type-safe linkage Ensures proper overloaded function called

1 // Fig. 3.25: fig03_25.cpp 2 // Using overloaded functions. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function square for int values 9 int square( int x ) 10 { 11 cout << "Called square with int argument: " << x << endl; 12 return x * x; 13 14 } // end int version of function square 15 16 // function square for double values 17 double square( double y ) 18 { 19 cout << "Called square with double argument: " << y << endl; 20 return y * y; 21 22 } // end double version of function square 23 Overloaded functions have the same name, but the different parameters distinguish them.

The proper function is called based upon the argument (int or double). 24 int main() 25 { 26 int intResult = square( 7 ); // calls int version 27 double doubleResult = square( 7.5 ); // calls double version 28 29 cout << "\nThe square of integer 7 is " << intResult 30 << "\nThe square of double 7.5 is " << doubleResult 31 << endl; 32 33 return 0; // indicates successful termination 34 35 } // end main The proper function is called based upon the argument (int or double). Called square with int argument: 7 Called square with double argument: 7.5   The square of integer 7 is 49 The square of double 7.5 is 56.25