例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. 博客终于开通了happy

    HelloWorld! 在我不懈的申请下,我的博客终于在第4次申请后成功开通了! 作为一个毕业两年的码农,现在才开始想要记录一些东西,似乎有点晚 -_-! 希望多年以后可以在这看到我长长的足迹!

  2. SQL Server 2008 对XML 数据类型操作

    原文 http://www.cnblogs.com/qinjian123/p/3240702.html 一.前言 从 SQL Server 2005 开始,就增加了 xml 字段类型,也就是说可以直接 ...

  3. session_cache_limiter 及 session 常见问题

    我点击后退按钮,为什么之前填写的东西不见 这是因为你使用了session. 解决办法: PHP代码:-------------------------------------------------- ...

  4. wikioi1450 xth的旅行

    题目描述 Description 毕业了,Xth很高兴,因为他要和他的 rabbit 去双人旅行了.他们来到了水城威尼 斯.众所周知(⊙﹏⊙b汗),这里的水路交通很发达,所以 xth 和 rabbit ...

  5. hibernate连接时指定编码方式 hibernate中文乱码问题

    <property name="connection.url">jdbc:mysql://localhost:3306/cms?useUnicode=true& ...

  6. linux之SQL语句简明教程---GROUP BY

    我们现在回到函数上.记得我们用 SUM 这个指令来算出所有的 Sales (营业额)吧!如果我们的需求变成是要算出每一间店 (Store_Name) 的营业额 (Sales),那怎么办呢?在这个情况下 ...

  7. Windows查看电脑上安装的.Net Framework版本的五种方法(转)

    1.查看安装文件判断Framwork版本号 打开资源管理器,比如我的电脑,再地址栏输入%systemroot%\Microsoft.NET\Framework后单击“转到”或者按回车. 在新文件夹中查 ...

  8. Linux dirname、basename 指令

    http://blog.sina.com.cn/s/blog_9d074aae01013ctk.html 一.dirname指令 1.功能:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然 ...

  9. Knots(找规律)

    Knots Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. Lotto(dfs)

    Lotto Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submis ...