04737_C++程序设计_第3章_函数和函数模板
例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章_函数和函数模板的更多相关文章
- ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...
- ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...
- ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...
- 全国计算机等级考试二级教程-C语言程序设计_第7章_函数
函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...
- 全国计算机等级考试二级教程-C语言程序设计_第9章_数组
四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 全国计算机等级考试二级教程-C语言程序设计_第13章_编译预处理和动态存储分配
free(p);//释放内存 p = NULL;//软件工程规范,释放内存以后,指针应该赋值为空 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h ...
- The Way to Go读书笔记_第4章_基本结构和基本数据类型
“_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...
- 04737_C++程序设计_第8章_多态性和虚函数
例8.1 分析下面程序的输出结果. 例8.2 分别使用指针和引用的display函数. #include <iostream> using namespace std; const dou ...
- 《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 ...
随机推荐
- Mongo客户端
http://www.linuxidc.com/Linux/2012-07/64233.htm http://www.oschina.net/p/rockmongo http://www.cnblog ...
- Hadoop--Hadoop的机架感知
Hadoop的机架感知 Hadoop有一个“机架感知”特性.管理员可以手工定义每个slave数据节点的机架号.为什么要做这么麻烦的事情?有两个原因:防止数据丢失和提高网络性能. 为了防止数据丢 ...
- IE 下a标签在 position:absolute 后无法点击的问题
IE 下 a 标签在 position:absolute 后无法点击的问题 条件 : DOCTYPE 为 XHTML. IE 浏览器 现象 : 将 a 的 position 指定为 absolute, ...
- 手工删除oracle的方法
大致方法如下: 1.删除物理文件 1.1.oracle安装文件. 1.2.系统目录下,program files文件下的oracle文件 2.注册表中大概有这么几个地方: hkey ...
- T-SQL视图
视图(view) 用于存储封装一个select语句,使用方法和表一样.也可通过界面对视图进行操作. create view ordersWithNum --创建一个视图ordersWithNum as ...
- UVA 1613 K-Graph Oddity
题意; 在一个n个点的无向连通图中,n是奇数,k是使得所有点的度数不超过k的最小奇数,询问一种染色方案,使得相邻点的颜色不同. 分析: k=所有点中最大的入度.如果最大入度是偶数,则k+1.每个点可选 ...
- ASP.NET怎么防止多次点击提交按钮重复提交
今天做一个系统,由于服务器有点慢,所以常会被点击两次或三次,提交的数据就是多次了.所以要让按钮点击后,不能再次点击. 对于一个按钮,要让变成恢色的,只要this.disabled=true就可以了,可 ...
- Gabor滤波器学习
本文的目的是用C实现生成Gabor模版,并对图像卷积.并简单提一下,Gabor滤波器在纹理特征提取上的应用. 一.什么是Gabor函数(以下内容含部分翻译自维基百科) 在图像处理中,Gabor函数是一 ...
- 如何:控制命名空间前缀 (C#) (LINQ to XML)
Visual Studio 2010 本主题介绍在序列化 XML 树时如何控制命名空间前缀. 在很多情况下,不需要控制命名空间前缀. 但是,某些 XML 编程工具需要命名空间前缀的特定控制. 例如,您 ...
- 表达式求值(河南省第四届ACM试题-C题)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定指定的一个由3种函数组成的表达式,计算其数值. [题目分析] 一开始以为是后缀表达式,后来抽了没想出来,最后用了递归的方法解 ...