Presentation is loading. Please wait.

Presentation is loading. Please wait.

登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。

Similar presentations


Presentation on theme: "登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。"— Presentation transcript:

1 登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。
登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。 總為浮雲能蔽日, 長安不見使人愁。

2 Chapter 6 More about Functions

3 Initializing Function Parameters (P.228)
You may declare the default value of some parameters: void showit(char msg[] = "I know the default!"); When you omit the argument in calling the function, the default value will be supplied automatically. showit("Today is Wednesday."); showit(); Ex6_03.cpp on P.228 Note that in P.229: Only the last argument(s) can be omitted. do_it(30, 30) is legal. do_it(30, , 30, 30) is illegal.

4 Function Overloading (P.237)
#include <iostream> using std::endl; using std::cout; void print(int i) { cout << " Here is int " << i << endl; } void print(double f) { cout << " Here is float " << f << endl; void print(const char* c) { // Use of the const Modifier cout << " Here is char* " << c << endl; int main() { print(10); print(10.10); print("ten");

5 Function Overloading (P.237)
Normally, we need three distinct functions to handle three different data types: int max_int(int array[], int len); long max_long(long array[], int len); double max_double(double array[], int len); Function overloading allows you to use the same function name for defining several functions as long as they each have different parameter lists. When the function is called, the compiler chooses the correct version according to the list of arguments you supply.

6 Ex6_07.cpp on P.238 The following functions share a common name, but have a different parameter list: int max(int array[], int len); long max(long array[], int len); double max(double array[], int len); Three overloaded functions of max() In main(), C++ compiler inspect the argument list to choose different version of functions.

7 Signature Overloaded functions can be differentiated by
having corresponding parameters of different types, or having a different number of parameters. The signature of a function is determined by its name and its parameter list. All functions in a program must have unique signatures. The following example is not valid overloading double max(long array[], int len); long max(long array[], int len); A different return type does not distinguish a function, if the signatures are the same.

8 If Signature Is Not Unique …
#include <iostream> using std::cout; using std::endl; int sum(int a, float b) { return a+ static_cast<int>(b); } float sum(int a, float b) { return static_cast<float>(a) + b; } int main() { cout << sum(1, 2.5) << endl; return 0; } Which version does it invoke?

9 Exercise: Function Overloading
Define two functions display(char []) and display(char [], char[]). The first function will display the argument with a newline character. The second function will display a horizontal separator composed of equal signs ('='), then display an argument in a line, and end with a horizontal separator again. Test your functions with the following main program: int main() { char first[] = "C programming"; char second[] = "is interesting"; display(first); display(first, second); return 0; } The output should be C programming ============== C programming is interesting ==============

10 Example: Playing Cards
Playing cards have four suits: Spade () Heart () Diamond () Club () There are 13 ranks in a suit: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A ex3_suit.cpp

11 Use to Represent 52 Cards T J Q K A ======================================     ex5_cards.cpp Suit ASCII code 3 4 5 6

12 Randomly Get 5 Cards An intuitive method is to randomly choose a number between 0..51, and repeat this action for 5 times for (int i = 0; i < 5; i++ ) cout << rand() % 52 << endl; print_card.cpp print_card_with_seed.cpp Sooner or later, you will see duplicate cards!

13 Shuffle (1) START i=0; i<52; i++ Randomly choose a card j Swap card i with card j Print all cards END The correct way is to “shuffle” your cards and get the first 5 of them.

14 Shuffle (2) ^ @ ^ + + ^ ^ The correct way is to “shuffle” your cards and get the first 5 of them. for (i=0; i<52; i++) { j = rand() % 52; temp=card[i]; card[i]=card[j]; card[j]=temp; } Let’s see a shorter example: rand_swap.cpp card[i] temp card[j]

15 card_shuffle.cpp int main() { int cards[52]; initialize(cards, 52); shuffle(cards, 52); get_five(cards); return 0; } The following exercise will get you familiar with how to pass arrays to a function.

16 Exercise print_a_card() print_a_card_in_chinese() print_cards()
sort_card()

17 Pointers (Chapter 4) A pointer stores an address
which points to a variable of some type A single pointer can point to different variables at different times a p b p = &a; *p = a p = &b; *p = b c p = &c; *p = c

18 Pointers to Functions (P.221)
A pointer to functions also provide you the flexibility. It will call the function whose address was last assigned to the pointer. A pointer to a function must contain The memory address of the function The parameter list The return type

19 Declaring Pointers to Functions (P.222)
double (*pfun) (char*, int); The parentheses around the pointer name, pfun, and the asterisk are necessary. Otherwise, double *pfun (char*, int) would be a function returning a pointer to a double value. long sum(long num1, long num2); long (*pfun)(long, long) = sum; long product(long, long); pfun = product;

20 Ex6_01.cpp on P.223 P.225

21 A Simpler Example As a matter of fact, I think Ex6_01.cpp is too complicated. I prefer the following example: pdo_it = product; cout << pdo_it(3,5) << endl; pdo_it = sum;

22 “A Pointer to a Function” as an Argument
Ex6_02.cpp on P.226 int main(void) { double array[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; int len(sizeof array/sizeof array[0]); cout << endl << "Sum of squares = " << sumarray(array, len, squared); cout << endl << "Sum of cubes = " << sumarray(array, len, cubed); cout << endl; return 0; }

23 sumarray() double sumarry(double array[], int len, double (*pfun) (double)) { double total(0.0); for (int i=0; i<len; i++) total += pfun(array[i]); return total; }

24 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 } ; pfun[1](2.5, 3.5); product(2.5, 3.5) (*pfun)(2.5, 3.5); sum(2.5, 3.5); (*(pfun+2)) (2.5, 3.5); difference(2.5, 3.5)

25 Callback Functions

26 qsort() #include <stdlib.h>
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. This is the “callback function” which you must supply in your program.

27 main() /* * Sort an array of 'int' values and print it to standard output. */ int main() { int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 }; const int array_size = sizeof(int_array) / sizeof(int_array[0]); int k; qsort(&int_array, array_size, sizeof(int_array[0]), cmpFunction); for (k = 0; k < array_size; k++) cout << ' ' << int_array[k]; cout << endl; return 0; }

28 cmpFunction() /* * Custom comparison function that can * compare 'int' values through pointers * passed by qsort(3). */ int cmpFunction(const void *p1, const void *p2) { int left = *(const int *)p1; int right = *(const int *)p2; return ((left > right) - (left < right)); }


Download ppt "登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。"

Similar presentations


Ads by Google