例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. 阅读express的感悟

    在github上看了半天的源码,也是云里雾里,勉强也算看完了,通过查看很多人的讲解也方便了我的理解,今天记录下来,也算是做个笔记. 进入express的源码文件里我们可以看到8个文件:middlewa ...

  2. ios中strong, weak, assign, copy

    copy 和 strong(retain) 区别 1. http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是 ...

  3. linux内核--进程地址空间(一)

    引言:现代操作系统提供了一种对内存的抽象概念,叫做虚拟存储器,它为每个进程提供了一个大的,一致的,和私有的地址空间.通过一个很清晰的机制,虚拟存储器提供了3个重要的能力: 1)它将主存看成是一个存储在 ...

  4. PHP 多input file文件上传

    前台html jquery代码 后台PHP处理 前台html <form id="form" method="post" enctype="mu ...

  5. 呵呵!手把手带你在 IIS 上执行 Python

    公司的站点让我头痛死了.在众多前辈高手的带领下.一大堆的 CMD 在站点里执行得好好地,黑客攻击也好好地.仅仅有站点和我不好好地,我快累死了,站点快挂了.. . 为了解决问题.我想到了 Python ...

  6. Windows下搭建Eclipse+Android4.0开发环境

    官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...

  7. UIScrollView 与 UIPageView 的联合使用

       @interface ViewController : UIViewController<UIScrollViewDelegate> { UIScrollView * scrollV ...

  8. 每个人应该知道的NVelocity用法

    NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象.从而使得界面设 ...

  9. css绝对定位、相对定位和文档流的那些事

    前言 接触html.和css时间也不短了,但每次用div+css布局的时候心里还是有点儿虚,有时候干脆就直接用table算了,很多时候用div会出现些不可预料的问题,虽然花费一定时间能够解决,但总不是 ...

  10. RDLC报表系列(五) 简单的图表-柱状图

    继续接上一篇的内容,本文主要是讲图标的内容,本文就是简单的图标,复杂的柱状图和折线图在下一文章中介绍. 数据源还是上文RDLC报表系列(四) 矩阵中的相同 1.还是继续使用demo2的文件