Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Functions Overloaded Functions Use pointers and references.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
The University of Adelaide, School of Computer Science
Programming and Data Structure
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Computer Programming 1 More on functions. Computer Programming 2 Objectives Function overloading Scope rules and namespace Inline Templates Pass by value.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
(continue) © by Pearson Education, Inc. All Rights Reserved.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Overview of C++ Templates
Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
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.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
A First Book of ANSI C Fourth Edition
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers Declare a pointer Address of a variable Pointers and.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
 2006 Pearson Education, Inc. All rights reserved Templates.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Programming with ANSI C ++
C++ Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
FUNCTIONS In C++.
CS212: Object Oriented Analysis and Design
Lecture 6 C++ Programming
Templates.
Name: Rubaisha Rajpoot
CS250 Introduction to Computer Science II
登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。
Corresponds with Chapter 5
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Functions Overloaded Functions Use pointers and references as parameters Template functions Function pointers Recursive functions

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Function overloading With function overloading, you can have several functions in a program with the same name Two functions with the same name are different if one of the following is true –The number of parameters for each function is different –The number of parameters is the same, but at least one pair of corresponding parameters has different types

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The Signature of a Function The combination of the name of a function, together with its parameter types defines unique characteristic called the function signature The return type is not part of the function signature Overloaded functions with pointer or reference parameters are different Program 9.1

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading and Pointer Parameters Different int larger(int*pV1, int* pV2); int larger(float*pV1, float*pV2); Identical int largest(int values[], int count); int largest(int* values, int count);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading and Reference Parameters It is not allowed to overload a function with a parameter of a given type data_type, with a function that has a parameter of type reference to data_type Be careful that it may cause unpredictable results int doIt(int number); // These are not int& doIt(int &number); // distinguishable dolt(value);// calling Program 9.2 The compiler is not prepared to use a temporary address to initialize a reference

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading and const Parameters Functions with constant parameters and non- constant parameters are the same signature –long& larger(long a, long b); –long& larger(const long a, const long b); The compiler ignores the const aspect of the parameters Functions with pointer to constant parameter and pointer to non-constant parameter are different –long* larger(long* a, long* b); –long* larger(const long* a, const long* b);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading with const Pointer Parameters Different

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading with const Pointer Parameters Identical

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading and const Reference Parameters Different Type int& is always different from type const int& The return value has no effect on the overloading

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointer to const and non- const Parameters long* larger(long*, long*); // prototype long num1=1, num2=2; // in main() long num3 = *larger(&num1, &num2); // calling long* larger(const long*, const long*); //prototype const long num10=1, num20=2; // in main() long num30=*larger(&num10,&num20); //calling

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointer to const Type & Constant Pointer to non-const Type Reference to “ 補充 Pointer.doc” Identical signature long* larger(long* a, long* b); long* larger(long* const a, long* const b);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. const Reference Parameter It is much more easier to recognize a constant reference parameter than a constant pointer parameter Different long& larger(long& a, long& b); const long& larger(const long& a, const long& b); Program ft_sig.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloading and Default Argument Values const char* const string& You can’t define a default argument for both functions due to ambiguity

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Template Functions In some situations, we were writing overloaded functions that contain exactly the same code (not necessary) A function template is a blueprint or a recipe for a function Program 9.3

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Defining a Template Specialization Explicitly specifying a template parameter Program 9.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloaded function Function Templates and Overloading

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Overloaded template

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Templates with Multiple Parameters

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Non-Type Template Parameters

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers to Functions A pointer can point to the address of a function. Such a pointer can point to different functions at different times during execution of your program. A pointer point to different functions with the same data types and the return type

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Declaring Pointers to Functions Program 9.5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Passing a Function as an Argument A function passed to another function as an argument is sometimes referred to as a callback function Program 9.6

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays of Pointers to Functions double sum(double, double); double product(double, double); double difference(double, double); double(*pfun[3])(double,double) = { sum, product, difference);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Recursion When a function contains a call to itself, it is referred to as a recursive function Indirect recursive function call –Function1 calls function2, and function2 calls function1, alternatively. A prerequisite for avoiding a loop of unlimited duration is that the function must contain some means of stopping the process Program 9.7

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loop and Recursion double power(double x, int n) { return 0==n ? 1.0 : (0>n ? 1.0/power(x,-n) : x*power(x,n-1)); } // same as followings double power(doublex, int n) { if (0==n) return 1.0; if (0<n) return x*power(x,n-1); return 1.0/power(x, -n); }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

More Efficient Loop double power(double x, int n) { if (n==0) return 1.0; if (n<0) { x = 1.0/x; n = -n; } double result = x; for (int i =1; i < n; i++) result *=x; return result; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Quicksort Using Recursion

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Swapping addresses in the Quicksort algorithm 變數 current 記錄比 chosen 小的個數做為 Index

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Swapping Addresses void swap(string* pStr[], int first, int second) { string* temp = pStr[first]; pStr[first] = pStr[second]; pStr[second] = temp; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Counting Words int count_words(const string&text, const string& separators) { size_t start = text.find_first_not_of(separators); size_t end = 0; int word_count = 0; while (start != string::npos) { end = text.find_first-of(separators, start+1); if (end == string::npos) end = text.length(); word_count++; start = text.find_first_not_of(separators, end+1); } return word_count; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Recursive function void sort(string*pStr[], int start, int end) { if (!(start<end)) return; swap(pStr, start, (start+end)/2); int current = start; for (int i = start+1; i<= end; i++) if (*(pStr[i]) < *(pStr[start])) swap(pStr, ++current, i); swap(pStr, start, current); sort(pStr, start, current-1); sort(pStr, current+1, end); }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Extracting Words void extract_words(string* pStr[], const string& text, const string& separators) { size_t start = text.find_first_not_of(separators); size_t end = 0, index = 0; while(start != string::npos) { end = text.find_first_of(separators, start+1); if (end == string::npos) end = text.length(); pStr[index++] = new string(text.substr(start, end-start)); start = text.find_first_not_of(separators,end+1); }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Quick Sort Program 9.8