例3.1

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void swap(string, string);//使用string类的对象作为函数参数 void main()
{
string str1("现在"), str2("过去");//定义对象str1和str2 swap(str1, str2);//使用传值方式传递str1和str2的数据成员值 cout << "返回后:str1=" << str1 << "str2=" << str2 << endl;
} void swap(string s1, string s2)//string类的对象s1和s2作为函数参数
{
string temp = s1;
s1 = s2;
s2 = temp; cout << "交换为:str1=" << s1 << "str2=" << s2 << endl;
}

例3.2

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void swap(string *, string *);//使用string类的对象作为函数参数 void main()
{
string str1("现在"), str2("过去");//定义对象str1和str2 swap(&str1, &str2);//使用传地址值方式传递str1和str2的地址值 cout << "返回后:str1=" << str1 << "str2=" << str2 << endl;
} void swap(string *s1, string *s2)//string类的对象指针s1和s2作为函数参数
{
string temp = *s1;
*s1 = *s2;
*s2 = temp; cout << "交换为:str1=" << *s1 << "str2=" << *s2 << endl;
}

例3.3

传递数组名实例。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 void swap(int[]);//数组形参使用“类型[]”的形式

 void main()
{
int a[] = { , };//定义数组a swap(a);//传递数组名a,也就是指针名 cout << "返回后:a=" << a[] << " b=" << a[] << endl;
} void swap(int a[])//数组名a,也就是指针名作为函数参数
{
int temp = a[];
a[] = a[];
a[] = temp; cout << "交换为:a=" << a[] << " b=" << a[] << endl;
}

例3.4

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void swap(string&, string&);//使用string类的引用对象作为参数 void main()
{
string str1("现在"), str2("过去");//定义对象str1和str2 swap(str1, str2);//传递对象的名字:str1和str2 cout << "返回后:str1=" << str1 << " str2=" << str2 << endl;
} void swap(string &s1, string &s2)//string类的引用对象s1和s2作为函数参数
{
string temp = s1;
s1 = s2;
s2 = temp; cout << "交换为:str1=" << s1 << " str2=" << s2 << endl;
}

例3.6

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void Display(string s1, string s2 = "", string s3 = ""); void main()
{
string str1("现在"), str2("过去"), str3("将来"); Display(str1);
Display(str1, str2, str3);
Display(str3, str1);
Display(str2, str3);
} void Display(string s1, string s2, string s3)
{
if (s2 == "" && s3 == "")
{
cout << s1 << endl;
}
else if (s3 == "" && s2 != "")
{
cout << s1 << "、" << s2 << endl;
}
else
{
cout << s1 << "、" << s2 << "、" << s3 << endl;
}
}

例3.7

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void change(const string&); void main()
{
string str("Can you change it?"); change(str); cout << str << endl;
} void change(const string&s)
{
string s2 = s + "No!"; cout << s2 << endl;
}

例3.8

返回引用的函数。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 int a[] = { ,,,,, };//全局数组

 int& index(int i);//返回引用的函数原型声明

 void main()
{
index() = ;//将a[3]改为16 cout << index() << endl;//输出16
} int& index(int i)//函数定义
{
return a[i];//返回指定下标的整数数组内容
}

例3.9

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 float *input(int&);//声明返回指针的函数

 void main()
{
int num;
float *data;//声明与input类型一致的指针 data = input(num);//调用函数,返回指针赋给data if (data)//data不空,输出所指内容
{
for (int i = ; i < num; i++)//使用指针的下标形式
{
cout << data[i] << " ";//循环输出
} delete data;//释放内存空间
}
} float *input(int& n)//定义返回指针的函数
{
cout << "Input number:";//询问输入数据数量
cin >> n; if (n <= )//输入个数不合理则退出
{
return NULL;
} float *buf = new float[n];//根据输入数据数量申请空间 if (buf == )//没申请到则退出
{
return NULL;
} for (int i = ; i < n; i++)//接收输入数据
{
cin >> buf[i];
} return buf;//返回指针
}

例3.10

函数返回对象的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; string input(const int);//声明返回string类型的函数 void main()
{
int n; cout << "Input n=";
cin >> n;//接收要处理的字符串数量 string str = input(n);//将函数返回的对象赋给对象str cout << str << endl;
} string input(const int n)
{
string s1, s2;//建立两个string类的对象(均为空串) for (int i = ; i < n; i++)//接收n个字符串
{
cin >> s1;
s2 = s2 + s1 + " ";//将接收的字符串相加
} return s2;//返回string对象
}

例3.11

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 int max(int, int);//2个整型参数的函数原型

 void main()
{
cout << max(, max(, )) << endl;
} int max(int m1, int m2)
{
return (m1 > m2) ? m1 : m2;
}

例3.12

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 double max(double, double);//2个实型参数的函数原型
int max(int, int);//2个整型参数的函数原型
char max(char, char);//2个字符型参数的函数原型
int max(int, int, int);//3个整型参数的函数原型 void main()
{
cout << max(2.5, 17.54) << " " << max(, ) << " " << max('w', 'p') << endl;
cout << "max(5, 9, 4)=" << max(, , ) << " max(5, 4, 9)=" << max(, , ) << endl;
} double max(double m1, double m2)
{
return (m1 > m2) ? m1 : m2;
} int max(int m1, int m2)
{
return (m1 > m2) ? m1 : m2;
} char max(char m1, char m2)
{
return (m1 > m2) ? m1 : m2;
} int max(int m1, int m2, int m3)
{
int t = max(m1, m2);
return max(t, m3);
}

例3.13

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 int add(int m1 = , int m2 = , int m3 = , int m4 = )
{
return m1 + m2 + m3 + m4;
} void main()
{
cout << add(, ) << "," << add(, , ) << "," << add(, , , ) << endl;
}

例3.14

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 template <class T>

 T max(T m1, T m2)
{
return (m1 > m2) ? m1 : m2;
} void main()
{
cout << max(, ) << "\t" << max(2.0, .) << "\t"
<< max('w', 'a') << "\t" << max("ABC", "ABD") << endl;
}

例3.15

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <complex>
#include <string> using namespace std; void printer(complex<int>);
void printer(complex<double>); void main()
{
int i(); complex<int>num1(, );
complex<double>num2(3.5, 4.5); printer(num1);
printer(num2);
} void printer(complex<int>a)
{
string str1("real is "), str2 = "image is ";
cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
} void printer(complex<double>a)
{
string str1("real is "), str2 = "image is ";
cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
}

例3.16

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <complex>
#include <string> using namespace std; template <class T> void printer(complex<T>a)
{
string str1("real is "), str2 = "image is ";
cout << str1 << a.real() << ',' << str2 << a.imag() << endl;
} void main()
{
int i(); complex<int>num1(, );
complex<double>num2(3.5, 4.5); printer(num1);
printer(num2);
}

例3.17

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 template <typename T>//使用typename替代class
T max(T m1, T m2)//求最大值
{
return (m1 > m2) ? m1 : m2;
} template <typename T>//必须重写
T min(T m1, T m2)//求最小值
{
return (m1 < m2) ? m1 : m2;
} void main()
{
cout << max("ABC", "ABD") << "," << min("ABC", "ABD") << ","
<< min('W', 'T') << "," << min(2.0, .); cout << "\t" << min<double>(8.5, ) << "," << min(8.5, (double)) << "," << max((int)8.5, ); cout << "\t" << min<int>(2.3, 5.8) << "," << max<int>('a', 'y') << "," << max<char>(, ) << endl;
}

04737_C++程序设计_第3章_函数和函数模板的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. 全国计算机等级考试二级教程-C语言程序设计_第7章_函数

    函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...

  5. 全国计算机等级考试二级教程-C语言程序设计_第9章_数组

    四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第13章_编译预处理和动态存储分配

    free(p);//释放内存 p = NULL;//软件工程规范,释放内存以后,指针应该赋值为空 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h ...

  7. The Way to Go读书笔记_第4章_基本结构和基本数据类型

    “_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...

  8. 04737_C++程序设计_第8章_多态性和虚函数

    例8.1 分析下面程序的输出结果. 例8.2 分别使用指针和引用的display函数. #include <iostream> using namespace std; const dou ...

  9. 《python语言程序设计》_第6章_函数

    # 6.1_引言 程序1: 结果: Sum from 1 to 10 is 55Sum from 20 to 38 is 513Sum from 35 to 50 is 630 程序2: #程序1和2 ...

随机推荐

  1. Activity和View的区别:

    Activity和View的区别: activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示.  viewGroup继承自view,实现了ViewM ...

  2. MarkDown基础使用教程-by sixleaves

    以下是个人浏览文档,结合自己平时使用所总结, 和引用国外关于如何使用markdown的教程.如有不足,还请海涵,期待于您的交流.我觉得使用markdown书写挺好的! 工具下载,可以去下载gitboo ...

  3. yum cannot retrieve metalink for repository

    Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

  4. Windows.Server.2003.R2 简体中文企业版 x86 x64(转)

    两张盘,第二张是 R2安装盘. Windows.Server.2003.R2.With.Sp2 中文企业版[MSDN官方版本][32bit] Windows 2003.R2.With.Sp2 简体中文 ...

  5. HDU 1852 Beijing 2008 数论

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1852 这道题和HDU1452类似. 题意:给你一个n.k,让你求2008^n所有因子的和(包括1和本 ...

  6. #include <boost/regex.hpp>

    boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...

  7. IOS(swift)-数据存储 · 用NSUserDefaults存储配置信息

    1.用NSUserDefaults存储配置信息 注:本次使用NSUserDefaults存储信息是在不考虑安全问题的前提下.分两种情况:1.如果是密码用户名等敏感信息,请使用Keychain存储用户敏 ...

  8. Top k问题(线性时间选择算法)

    问题描述:给定n个整数,求其中第k小的数. 分析:显然,对所有的数据进行排序,即很容易找到第k小的数.但是排序的时间复杂度较高,很难达到线性时间,哈希排序可以实现,但是需要另外的辅助空间. 这里我提供 ...

  9. IIS中如何建立FTP服务

    文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机.这些文件存储在运行 FTP 服务器软件的服务器计算机上.然后,远程计算机可以使用 FTP 建 ...

  10. Oracle触发器Trigger2行级

    create table trigger_t2( id int, name ), age int ); /* --创建一个before update的触发器-控制每一行,行级 --只有行级的才会有:n ...