数据结构———重载与模板(C++)
相关信息
/*
*
* @subject 数据结构
* @author 信管1142班 201411671210 JJ lai
*
* @program 模板与重载
*
*/
实验一:
/*
要求如下:
1.设计函数来计算“和”和“积”,在主函数中调用,
2.能考虑重载函数,使整数和小数均能计算。
*/
#include<iostream>
using std::cout;
using std::endl;
int sum(int variable_1, int variable_2)
{
return variable_1 + variable_2;
}
double sum(double variable_1, double variable_2)
{
return variable_1 + variable_2;
}
int product(int variable_1, int variable_2)
{
return variable_1 * variable_2;
}
double product(double variable_1, double variable_2)
{
return variable_1 * variable_2;
}
int main()
{
cout << sum(200, 320) << endl;
cout << sum(2.5, 2.7) << endl;
cout << product(260, 2) << endl;
cout << product(9.20, 100.0) << endl;
}
实验二:
/*
要求如下:
1.设计函数来计算“和”和“积”,在主函数中调用,
2.使用模板实现。
*/
#include<iostream>
using std::cout;
using std::endl;
template<typename dataType>
dataType sum(const dataType &variable_1, const dataType &variable_2)
{
return variable_1 + variable_2;
}
template<typename dataType>
dataType product(const dataType &variable_1, const dataType &variable_2)
{
return variable_1 * variable_2;
}
int main()
{
cout << sum(200, 320) << endl;
cout << sum(2.5, 2.7) << endl;
cout << product(260, 2) << endl;
cout << product(9.20, 100.0) << endl;
}
实验三:
/*
要求如下:
1.设计函数来计算“和”和“积”,在主函数中调用,
2.使用类模板实现。
3.使用多文件。
*/
//moban.h
template<typename dataType>
class Plate {
public:
dataType sum(const dataType &variable_1, const dataType &variable_2);
dataType product(const dataType &variable_1, const dataType &variable_2);
private:
dataType variable1_;
dataType variable2_;
};
template<typename dataType>
dataType Plate<dataType>::sum(const dataType &variable_1, const dataType &variable_2)
{
return variable_1 + variable_2;
}
template<typename dataType>
dataType Plate<dataType>::product(const dataType &variable_1, const dataType &variable_2)
{
return variable_1 * variable_2;
}
//main.cpp
#include<iostream>
#include"标头.h"
using std::cout;
using std::endl;
int main()
{
Plate<int>I_plate;
Plate<double>D_plate;
cout << I_plate.sum(200, 320) << endl;
cout << I_plate.sum(2.5, 2.7) << endl;
cout << D_plate.product(260, 2) << endl;
cout << D_plate.product(9.20, 100.0) << endl;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
数据结构———重载与模板(C++)的更多相关文章
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...
- C++ Templates (1.5 重载函数模板 Overloading Function Templates)
返回完整目录 目录 1.5 重载函数模板 Overloading Function Templates 1.5 重载函数模板 Overloading Function Templates 和普通函数一 ...
- sizeof 感知重载,模板具现, 转换规则
问题:如何侦知任意型别 T 是否可以自动转换为型别 U? 方案:侦测转换能力的想法:合并运用 sizeof 和重载函数. 1 依赖 sizeof,sizeof 有着惊人的能力,你可以把 sizeof ...
- c++学习笔记之函数重载和模板理解
1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...
- 从0开始 数据结构 AC自动机 模板(from kkke)
AC自动机模板 2.4.1 头文件&宏&全局变量 #include <queue> #define MAXN 666666 #define MAXK 26//字符数量 st ...
- [ACM_数据结构] 线段树模板
#include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ ...
- c/c++ 模板函数的重载
模板函数的重载 普通函数可以重载,模板函数也可以重载,但规则复杂 有下面2个函数,名字相同,返回值相同就,参数不同,符合重载. template<typename T> std::stri ...
- C++模板专门化与重载
最近在复习C++有关知识,又重新看<<Effective C++>>,收获颇丰.原来以前看这边书,好多地方都是浅尝辄止.<<Effective C++>> ...
- c++学习书籍推荐《数据结构C++语言描述:应用标准模板库STL(第2版)》下载
本书是Ford和Topp两位教授于1996看出版的名著Data Structures with C++的第2版,在全球范围内已经有数以万计的学生从中受益.作者将C++语言作为算法描述语言,应用包含规范 ...
随机推荐
- 在安卓下使用python连接蓝牙串口模块(HC-06)
在安卓上安装Python: 请参考:https://github.com/kuri65536/python-for-android/blob/master/README.md下载程序文件需要访问 ht ...
- iis7 php urlrewrite 并隐藏index.php
<rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> ...
- SQL-学习使用FOR XML PATH
前言:本人SQL技术很烂,然后工作时间也不久,许多东西都还在学习中,说的不好的地方尽请谅解. 首先跟大家说一下我今天遇到的问题吧. 查出的数据有三列,第一列存放的是32位的GUID,Res_Name存 ...
- Android——获取网络图片
布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- SecureCRT自动断开连接的问题
直接在虚拟机上ssh道实验室的服务器时并没有发现过上一段时间不操作就会断开,可能是我没有注意,也能是操作时间间隔比较短. 但是在secureCRT上登录时,发现经常的断开,很是郁闷,所以baidu了一 ...
- awk与cut在以空格为分割域时的区别
awk默认以空格为分割域,比如我想获得某进程pid:[root@SHCTC-GAME12-44 ~]# ps -ef|grep "sshd -f"|grep -v greproot ...
- 《JAVA核心技术卷 卷1 基础知识》
第一卷 关键字:体系结构中立,可移植性,高性能,多线程 体系机构中立:通过解释字节码实现,优点是,让JAVA能在很多机器上运行.缺点是运行速度很慢. 可移植性:因为JAVA的基本数据类型有固定的大小. ...
- C# lazy加载
转自http://www.cnblogs.com/yunfeifei/p/3907726.html 在.NET4.0中,可以使用Lazy<T> 来实现对象的延迟初始化,从而优化系统的性能. ...
- netbeans环境中使用maven搭建jsf、primefaces工程
新建maven工程,选择web工程 增加jsf框架,加入primefaces 部署到服务器,在浏览器中打开页面,这是primefaces框架的页面. 此时工程web目录下有如下文件 web.xml文件 ...
- ###Maintainable C++
点击查看Evernote原文. #@author: gr #@date: 2014-08-15 #@email: forgerui@gmail.com 记录一些标准规范.让自己的编码更可读,更可维护. ...