04737_C++程序设计_第3章_函数和函数模板

例3.1

传对象不会改变原来对象数据成员值的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void swap(string, string);//使用string类的对象作为函数参数
 9 
10 void main()
11 {
12     string str1("现在"), str2("过去");//定义对象str1和str2
13     
14     swap(str1, str2);//使用传值方式传递str1和str2的数据成员值
15 
16     cout << "返回后:str1=" << str1 << "str2=" << str2 << endl;
17 }
18 
19 void swap(string s1, string s2)//string类的对象s1和s2作为函数参数
20 {
21     string temp = s1;
22     s1 = s2;
23     s2 = temp;
24     
25     cout << "交换为:str1=" << s1 << "str2=" << s2 << endl;
26 }

例3.2

使用对象指针作为函数参数的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void swap(string *, string *);//使用string类的对象作为函数参数
 9 
10 void main()
11 {
12     string str1("现在"), str2("过去");//定义对象str1和str2
13     
14     swap(&str1, &str2);//使用传地址值方式传递str1和str2的地址值
15 
16     cout << "返回后:str1=" << str1 << "str2=" << str2 << endl;
17 }
18 
19 void swap(string *s1, string *s2)//string类的对象指针s1和s2作为函数参数
20 {
21     string temp = *s1;
22     *s1 = *s2;
23     *s2 = temp;
24     
25     cout << "交换为:str1=" << *s1 << "str2=" << *s2 << endl;
26 }

例3.3

传递数组名实例。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 void swap(int[]);//数组形参使用“类型[]”的形式
 8 
 9 void main()
10 {
11     int a[] = { 3,8 };//定义数组a
12 
13     swap(a);//传递数组名a,也就是指针名
14 
15     cout << "返回后:a=" << a[0] << " b=" << a[1] << endl;
16 }
17 
18 void swap(int a[])//数组名a,也就是指针名作为函数参数
19 {
20     int temp = a[0];
21     a[0] = a[1];
22     a[1] = temp;
23 
24     cout << "交换为:a=" << a[0] << " b=" << a[1] << endl;
25 }

例3.4

使用“引用形参”作为函数参数的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void swap(string&, string&);//使用string类的引用对象作为参数
 9 
10 void main()
11 {
12     string str1("现在"), str2("过去");//定义对象str1和str2
13 
14     swap(str1, str2);//传递对象的名字:str1和str2
15 
16     cout << "返回后:str1=" << str1 << " str2=" << str2 << endl;
17 }
18 
19 void swap(string &s1, string &s2)//string类的引用对象s1和s2作为函数参数
20 {
21     string temp = s1;
22     s1 = s2;
23     s2 = temp;
24 
25     cout << "交换为:str1=" << s1 << " str2=" << s2 << endl;
26 }

例3.6

设计一个根据参数数量输出信息的函数。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void Display(string s1, string s2 = "", string s3 = "");
 9 
10 void main()
11 {
12     string str1("现在"), str2("过去"), str3("将来");
13 
14     Display(str1);
15     Display(str1, str2, str3);
16     Display(str3, str1);
17     Display(str2, str3);    
18 }
19 
20 void Display(string s1, string s2, string s3)
21 {
22     if (s2 == "" && s3 == "")
23     {
24         cout << s1 << endl;
25     }
26     else if (s3 == "" && s2 != "")
27     {
28         cout << s1 << "" << s2 << endl;
29     }
30     else
31     {
32         cout << s1 << "" << s2 << "" << s3 << endl;
33     }
34 }

例3.7

不允许改变作为参数传递的字符串内容的实例。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void change(const string&);
 9 
10 void main()
11 {
12     string str("Can you change it?");
13 
14     change(str);
15 
16     cout << str << endl;
17 }
18 
19 void change(const string&s)
20 {
21     string s2 = s + "No!";
22 
23     cout << s2 << endl;
24 }

例3.8

返回引用的函数。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int a[] = { 2,4,6,8,10,12 };//全局数组
 8 
 9 int& index(int i);//返回引用的函数原型声明
10 
11 void main()
12 {
13     index(3) = 16;//将a[3]改为16
14 
15     cout << index(3) << endl;//输出16
16 }
17 
18 int& index(int i)//函数定义
19 {
20     return a[i];//返回指定下标的整数数组内容
21 }

例3.9

使用函数input输入一组数并返回一个指针,然后由主函数main将这组数显示出来的例子。

  

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 float *input(int&);//声明返回指针的函数
 8 
 9 void main()
10 {
11     int num;
12     float *data;//声明与input类型一致的指针
13 
14     data = input(num);//调用函数,返回指针赋给data
15 
16     if (data)//data不空,输出所指内容
17     {
18         for (int i = 0; i < num; i++)//使用指针的下标形式
19         {
20             cout << data[i] << " ";//循环输出
21         }
22 
23         delete data;//释放内存空间
24     }
25 }
26 
27 float *input(int& n)//定义返回指针的函数
28 {
29     cout << "Input number:";//询问输入数据数量
30     cin >> n;
31 
32     if (n <= 0)//输入个数不合理则退出
33     {
34         return NULL;
35     }
36 
37     float *buf = new float[n];//根据输入数据数量申请空间
38 
39     if (buf == 0)//没申请到则退出
40     {
41         return NULL;
42     }
43 
44     for (int i = 0; i < n; i++)//接收输入数据
45     {
46         cin >> buf[i];
47     }
48 
49     return buf;//返回指针
50 }

例3.10

函数返回对象的例子。 

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 string input(const int);//声明返回string类型的函数
 9 
10 void main()
11 {
12     int n;
13 
14     cout << "Input n=";
15     cin >> n;//接收要处理的字符串数量
16 
17     string str = input(n);//将函数返回的对象赋给对象str
18 
19     cout << str << endl;
20 }
21 
22 string input(const int n)
23 {
24     string s1, s2;//建立两个string类的对象(均为空串)
25 
26     for (int i = 0; i < n; i++)//接收n个字符串
27     {
28         cin >> s1;
29         s2 = s2 + s1 + " ";//将接收的字符串相加
30     }
31 
32     return s2;//返回string对象
33 }

例3.11

函数返回值作为函数的参数。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int max(int, int);//2个整型参数的函数原型
 8 
 9 void main()
10 {
11     cout << max(55, max(25, 39)) << endl;
12 }
13 
14 int max(int m1, int m2)
15 {
16     return (m1 > m2) ? m1 : m2;
17 }

例3.12

函数重载产生多态性的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 double max(double, double);//2个实型参数的函数原型
 8 int max(int, int);//2个整型参数的函数原型
 9 char max(char, char);//2个字符型参数的函数原型
10 int max(int, int, int);//3个整型参数的函数原型
11 
12 void main()
13 {
14     cout << max(2.5, 17.54) << " " << max(56, 8) << " " << max('w', 'p') << endl;
15     cout << "max(5, 9, 4)=" << max(5, 9, 4) << " max(5, 4, 9)=" << max(5, 4, 9) << endl;
16 }
17 
18 double max(double m1, double m2)
19 {
20     return (m1 > m2) ? m1 : m2;
21 }
22 
23 int max(int m1, int m2)
24 {
25     return (m1 > m2) ? m1 : m2;
26 }
27 
28 char max(char m1, char m2)
29 {
30     return (m1 > m2) ? m1 : m2;
31 }
32 
33 int max(int m1, int m2, int m3)
34 {
35     int t = max(m1, m2);
36     return max(t, m3);
37 }

例3.13

编写一个具有默认参数的函数。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int add(int m1 = 0, int m2 = 0, int m3 = 0, int m4 = 0)
 8 {
 9     return m1 + m2 + m3 + m4;
10 }
11 
12 void main()
13 {
14     cout << add(1, 3) << "," << add(1, 3, 5) << "," << add(1, 3, 5, 7) << endl;
15 }

例3.14

编制求两个数据中的最大值的函数模板程序。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 template <class T>
 8 
 9 T max(T m1, T m2)
10 {
11     return (m1 > m2) ? m1 : m2;
12 }
13 
14 void main()
15 {
16     cout << max(2, 5) << "	" << max(2.0, 5.) << "	"
17         << max('w', 'a') << "	" << max("ABC", "ABD") << endl;
18 }

例3.15

编写具有复数complex模板类参数的重载函数实例。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <complex>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 void printer(complex<int>);
10 void printer(complex<double>);
11 
12 void main()
13 {
14     int i(0);
15 
16     complex<int>num1(2, 3);
17     complex<double>num2(3.5, 4.5);
18 
19     printer(num1);
20     printer(num2);
21 }
22 
23 void printer(complex<int>a)
24 {
25     string str1("real is "), str2 = "image is ";
26     cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
27 }
28 
29 void printer(complex<double>a)
30 {
31     string str1("real is "), str2 = "image is ";
32     cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
33 }

例3.16

使用类模板作为函数模板参数的程序。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 #include <complex>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 template <class T>
10 
11 void printer(complex<T>a)
12 {
13     string str1("real is "), str2 = "image is ";
14     cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
15 }
16 
17 void main()
18 {
19     int i(0);
20 
21     complex<int>num1(2, 3);
22     complex<double>num2(3.5, 4.5);
23 
24     printer(num1);
25     printer(num2);
26 }

例3.17

使用显式规则和关键字typename编制函数模板的例子。 

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 template <typename T>//使用typename替代class
 8 T max(T m1, T m2)//求最大值
 9 {
10     return (m1 > m2) ? m1 : m2;
11 }
12 
13 template <typename T>//必须重写
14 T min(T m1, T m2)//求最小值
15 {
16     return (m1 < m2) ? m1 : m2;
17 }
18 
19 void main()
20 {
21     cout << max("ABC", "ABD") << "," << min("ABC", "ABD") << ","
22         << min('W', 'T') << "," << min(2.0, 5.);
23     
24     cout << "	" << min<double>(8.5, 6) << "," << min(8.5, (double)6) << "," << max((int)8.5, 6);
25 
26     cout << "	" << min<int>(2.3, 5.8) << "," << max<int>('a', 'y') << "," << max<char>(95, 121) << endl;
27 }
原文地址:https://www.cnblogs.com/denggelin/p/5549044.html