相关信息


/*
*
* @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++)的更多相关文章

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

    用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...

  2. C++ Templates (1.5 重载函数模板 Overloading Function Templates)

    返回完整目录 目录 1.5 重载函数模板 Overloading Function Templates 1.5 重载函数模板 Overloading Function Templates 和普通函数一 ...

  3. sizeof 感知重载,模板具现, 转换规则

    问题:如何侦知任意型别 T 是否可以自动转换为型别 U? 方案:侦测转换能力的想法:合并运用 sizeof 和重载函数. 1 依赖 sizeof,sizeof 有着惊人的能力,你可以把 sizeof  ...

  4. c++学习笔记之函数重载和模板理解

    1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...

  5. 从0开始 数据结构 AC自动机 模板(from kkke)

    AC自动机模板 2.4.1 头文件&宏&全局变量 #include <queue> #define MAXN 666666 #define MAXK 26//字符数量 st ...

  6. [ACM_数据结构] 线段树模板

    #include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ ...

  7. c/c++ 模板函数的重载

    模板函数的重载 普通函数可以重载,模板函数也可以重载,但规则复杂 有下面2个函数,名字相同,返回值相同就,参数不同,符合重载. template<typename T> std::stri ...

  8. C++模板专门化与重载

    最近在复习C++有关知识,又重新看<<Effective C++>>,收获颇丰.原来以前看这边书,好多地方都是浅尝辄止.<<Effective C++>> ...

  9. c++学习书籍推荐《数据结构C++语言描述:应用标准模板库STL(第2版)》下载

    本书是Ford和Topp两位教授于1996看出版的名著Data Structures with C++的第2版,在全球范围内已经有数以万计的学生从中受益.作者将C++语言作为算法描述语言,应用包含规范 ...

随机推荐

  1. Forwarding a Range of Ports in VirtualBox

    STAN SCHWERTLY MAY 9, 2012 ARTICLES 3 COMMENTS Doesn't allow forwarding a range of ports through the ...

  2. oracle 不转义 &

    在为表加注释,遇到有些注释包含'&' 但又不想写eacape, 在sqlplus下,set define off   即可将&输入到数据库中.

  3. C语言进行CGI程序设计

    一.CGI概述 CGI(公用网关接口)规定了Web服务器调用其他可执行程序(CGI程序)的接口协议标准.Web服务器通过调用CGI程序实现和Web浏览器的交互, 也就是CGI程序接受Web浏览器发送给 ...

  4. linux下安装软件后的环境变量设置

    /home/username/.bashrc文件中加入如下内容 export LM_LICENSE_FILE=/home/program_files/modeltech_10.2c/modeltech ...

  5. SSRS中加入书签功能及数据集窗口

    SSRS在使用过程中,是比较好用的,但如果报表过长,则会比较难看到想看的内容,在SSRS中有书签功能,可以将报表直接直接跳转至书签位置,这样用户可以比较精准的定位到关注位置.使用一个文本框或是图片,再 ...

  6. 使用 ICharpCode.SharpZipLib 压缩指定目录结构

    今天做项目中遇见一个压缩问题,我的目录结构是树形菜单,文件在服务器存储是平面存储,没有目录结构,所以在下载指定目录的时候要构建目录结构,如下: 当我右键点击下载b目录文件夹的时候要Download   ...

  7. smarty半小时快速上手入门教程

    http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...

  8. while循环语句

    while(循环条件,返回布尔类型)            {                代码执行的操作,或者打印输出. } do  whilw循环 do            {         ...

  9. Merge Into example

    merge into users a using temp_users b on (a.userid = b.user_id) when matched then update set a.passw ...

  10. PHP JQuery Json

    PHP: echo Json_encode($result); Jquery: $.get( 'process.php', {'p1':'aa','p2':'dd', 'data_type':'jso ...