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 }

123

时间: 2024-08-25 06:38:15

C++程序设计_第3章_函数和函数模板的相关文章

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

原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 GIS的六大功能是数据获取.存储.查询.分析.表达.输出.在前面的内容里已经介绍了使用ArcGIS进行数据获取.存储.查询.表达和输出的过程,本章将介绍如何在ArcGIS中进行地理分析.分析是GIS的核心和灵魂,是GIS区别于一般的信息系统.CAD或者电子地图系统的主要标志之一. GIS分析,就是研究

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

原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Desktop的组成部分之一,ArcMap用于数据的浏览.编辑.显示.查询.地图排版等.ArcMap和ArcCatalog一起构成了完整的数据处理与管理分析的功能.在前一章中已经介绍了ArcCatalog的使用,本章中将介绍ArcMap的使用.本章的例子依然使用第4章里的小区平面图示例,但是将从原理的角度做更加

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

原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一个全面的了解,接下来在本章中,将通过一个案例来熟悉ArcGIS for Desktop的使用,从解决问题的过程中,逐渐适应ArcGIS桌面的界面和操作方式. 本章的练习数据是一个住宅小区的简单平面示意图,需要在已有的基础上把楼房的轮廓补充完整,并加以整饰,完成一幅地图. 1.1 打开地图文档并浏览

全国计算机等级考试二级教程-C语言程序设计_第11章_对函数的进一步讨论

例11.2 通过给 trans 函数传送不同的函数名,求 tan x 和 cot x 值. 1 #include <stdio.h> 2 #include <math.h> 3 double tran(double(*) (double), double(*) (double), double); /* 函数说明语句 */ 4 main() 5 { 6 double y, v; 7 v = 60 * 3.1416 / 180.0; 8 y = tran(sin, cos, v);

《Pyhton语言程序设计》_第7章_对象和类

#7.2.1_定义类 一个类的功能:数据域.定义方法.初始化程序 初始化程序总是被命名为:_ _init_ _ (两个连续的下划线) #7.2.4_self参数 #self参数是指向对象本身的参数,那么它的作用域就是整个类,self.x可以直接访问实例变量x,self.ml()表示调用类的对象self的示例方法ml. #7.2.5_举例:使用类 原文地址:https://www.cnblogs.com/qiyuanjiejie/p/9736766.html

C++程序设计_第7章_类模板与向量

例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. 1 #include <iostream> 2 3 using namespace std; 4 5 template <class T> 6 7 class Max4 8 { 9 T a, b, c, d; 10 T Max(T a, T b) 11 { 12 return (a > b) ? a : b; 13 } 14 public: 15 Max4(T, T, T, T); 16 T Max(vo

全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

字符串排序有2种: 1长度strlen 2比较strcmp 读入一个3行的二维字符串数组,使用求字符串长度函数strlen,进行从大到小排序,使用冒泡排序. 1 #include <stdio.h> 2 #include <string.h> 3 main() 4 { 5 int i, j; 6 char t[20], a[3][20]; 7 for (i = 0;i < 3;i++) /* 为a表赋值 */ 8 { 9 gets(a[i]); 10 } 11 12 prin

C++程序设计_第6章_继承和派生

例6.1 使用默认内联函数实现单一继承. 1 #include<iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 private: 8 int x, y; 9 public: 10 Point(int a, int b) 11 { 12 x = a; 13 y = b; 14 cout << "Point..." << endl; 15 } 16 void Showxy() 17 { 1

全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型

例14.2 对比向函数传递结构体数组名和向函数传递结构体变量名的区别. 1 #include<stdio.h> 2 3 typedef struct 4 { 5 int num; 6 double mark; 7 }REC; 8 9 void sub1(REC x) 10 { 11 x.num = 23; 12 x.mark = 81.5; 13 } 14 15 void sub2(REC y[]) 16 { 17 y[0].num = 12; 18 y[0].mark = 77.5; 19