数据结构———重载与模板(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++语言作为算法描述语言,应用包含规范 ...
随机推荐
- 使用Mou 编写Markdown编辑器博客
Mou Overview Mou, the missing Markdown editor for web developers. Syntax Strong and Emphasize strong ...
- nginx性能配置参数说明:
nginx的配置:main配置段说明一.正常运行的必备配置: 1.user username [groupname]; 指定运行worker进程的用户和组 2.pid /path/to/pidfile ...
- plot函数功能总结
基本形式 >> y=[1 2 3 4 5 6]; >> plot(y) 生成的图形是以序号为横坐标.数组y的数值为纵坐标画出的折线. >> x=linspace(0 ...
- Office365 InfoPath 表单的设计和应用(原创)
表单的应用:我想到的有2种. 1 做为自定义表单库的模版. 通过发放url(模版链接)给用户来填写表单. 最后将在表单库中得到所有填写的信息列表. 如 2 上传表单做为ContentType 也就是自 ...
- 搭建高可用的MongoDB集群
http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/1 在大数据的时代 ...
- xxx is not in the sudoers file.This incident will be reported.的解决方法
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是:chmod u+w /etc/sudoers 3.编辑sudoers文件vi /etc/sud ...
- HW-找7(测试ok满分注意小于等于30000的条件)
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数 知识点 循环 运行时间限制 0M 内存限制 0 输入 一个正整数N.(N不大于300 ...
- jQuery 源码分析5: jQuery 基本静态方法(一)
jQuery在初始化过程中会为自己扩展一些基本的静态方法和属性,以下是jQuery 1.11.3版本 239 ~ 564行间所扩展的静态属性和方法 jQuery.extend({ // 为每个jQ ...
- CSS3的几个标签速记3
transition:CSS3过渡 css3里很好的一个标签,可以非常方便的完成需要很多JS才能完成的动态效果 例语法:transition:width 2S,height 2S,transf ...
- OpenJudge 2766 最大子矩阵
1.链接: http://bailian.openjudge.cn/practice/2766 2.题目: 总Time Limit: 1000ms Memory Limit: 65536kB Desc ...