[C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板

此节包括函数重载,隐式函数重载,函数模板,带参数函数模板
函数的重载
#include <iostream>
using namespace std; int operate (int a, int b) {
return (a * b);
} double operate (double a, double b) {
return (a / b);
} int main ()
{
int x = ;
int y = ;
double n = 5.0 ;
double m = 2.0; cout << operate (x,y) << '\n';
cout << operate (n,m) << '\n'; return ;
}
打印结果
2.5
Program ended with exit code:
函数模板
#include <iostream>
using namespace std; // 模板
template <class T>
T sum (T a, T b) {
T result;
result = a + b;
return result;
} int main () {
// 值初始化
int i = ;
int j = ;
int k = ;
double f = 2.0, g = 0.5, h; // 使用模板函数
k = sum<int>(i, j);
h = sum<double>(f, g); // 打印输出
cout << k << '\n';
cout << h << '\n'; return ;
}
打印结果
2.5
Program ended with exit code:
模板自动匹配
#include <iostream>
using namespace std; template <class T, class U>
bool are_equal (T a, U b) {
return (a == b);
} int main () { // 自动模板识别
if (are_equal(,10.0))
cout << "x and y are equal\n";
else
cout << "x and y are not equal\n";
return ;
}
打印结果
x and y are equal
Program ended with exit code:
带参数的模板
#include <iostream>
using namespace std; template <class T, int N>
T fixed_multiply (T val) {
return val * N;
} int main() {
std::cout << fixed_multiply<int, >() << '\n';
std::cout << fixed_multiply<int, >() << '\n';
}
打印结果
Program ended with exit code:
[C++] 用Xcode来写C++程序[5] 函数的重载与模板的更多相关文章
- [C++] 用Xcode来写C++程序[4] 函数
用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成 ...
- [C++] 用Xcode来写C++程序[7] Class
用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- [C++] 用Xcode来写C++程序[3] Constants
用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...
- [C++] 用Xcode来写C++程序[2] 操作变量
用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables # ...
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...
- 使用Xcode IDE写node.js
最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- Ubuntu-16.04 R 安装及Jupyter notebook 配置
1. R 安装 通常在Terminal下直接apt-get 即可,在16.10下可以get到R-3.3.1,目前最新好像是 R-3.4.2,可以去官方网站下载源码编译 (https://www.r-p ...
- Windows 8.1 硬盘安装 Ubuntu14.04 双系统参考教程及注意事项
硬盘安装,无需光盘.U盘:Win8.1为主,Ubuntu14.04为辅,可将Windows或Ubuntu设置为开机默认启动项:在Ubuntu下可查看.操作Windows系统下的文件:适用于安装和14. ...
- redis-redisTemplate模糊匹配删除
前几天需要一个模糊删除redis中key的功能, 没有多想, 直接 String key = "noteUserListenedPoi:*"; redisTemplate.del ...
- printf()的转换说明的修饰符中的标记、数字、和.数字
先记下代码和运行结果 再解释 #include <stdio.h> #include <stdlib.h> #include <limits.h> #define ...
- jquery ajax abort()方法
如果用户频繁点击ajax请求,除最后一个外都是无效的,趁早结束节省资源.也可能出现更严重的问题,最后一个发送的请求,响应未必是最后一个,有可能造成混乱.用jquery的abort方法,可以中途中止aj ...
- haml参考大全
原文来自: http://blackanger.blog.51cto.com/140924/47642 Haml是一种用来描述任何XHTML web document的标记语言,它是干净,简单的. ...
- [Codeforces Round#488]Div.2
总结 这是我无聊透顶肝到三点半的一场 cf ,结果还真够无聊的 这套题涵盖了英语题,语文题,模拟题.注重考查了选手的英语素养能力,语文阅读能力和精湛的模拟和枚举能力.是不可多得的一套好题. 没什么单独 ...
- [CQOI 2018]破解D-H协议
Description 题库链接 给出 \(A,B,P,g\) ,\(g\) 是 \(P\) 的原根,求出 \(A\equiv g^a\pmod{P}\) , \(B\equiv g^b\pmod{P ...
- MVC页面缓存
1.OutputCache 属性 contact.cshtml [OutputCache(Duration=10)] public ActionResult Contact() { ...
- CSS Sprite 精灵图
.bg_sprite{background-image:url(/整图地址); background-repeat:no-repeat} 引用该类 .. 然后在元素中逐一定义背景坐标 .. 以下为关键 ...