相关信息


/*
*
* @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. Sqlserver的触发器的简单使用

    1,触发器有两种 (1)After触发器(之后触发) 触发器有个好处:就是你之前有过什么操作他会将你的操作的数据信息完整的保存下来,比如你删过什么信息,如果用触发器,那么删除后就会显示两行受影响,那么 ...

  2. Linux shell之数组

    引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...

  3. Linux与windows文件乱码问题

    Linux与windows文件乱码问题 */--> Linux与windows文件乱码问题 Table of Contents 1. 简介 2. iconv详解 2.1. iconv –help ...

  4. quartz定时任务中常用的cron表达式

    一:定时cron的格式,一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为: 1.秒(0~59) 2.分钟(0~59) 3.小时(0~23) 4.天(月(0~31,但是你需 ...

  5. 从lambda到函数式编程

    Object.send(:remove_const,'TRUE') Object.send(:remove_const,'FALSE') def to_integer(pro) pro[-> n ...

  6. dedecms导航

    {dede:global.cfg_cmsurl /} 首页链接 一级导航: {dede:channel type=“top”} [field:typelink]:导航链接 [field:typenam ...

  7. Android Drawable系列(1):自定义背景以及注意事项

    0. Shape自身属性 android:shape=["rectangle" | "oval" | "line" | "ring ...

  8. SQL 树结构统计数据

    create table #Tmp( ID int IDENTITY (1,1) not null, name varchar(50), sl int); DECLARE @ID VARCHAR(36 ...

  9. javascript 事件 第23节

    <html> <head> <title>DOM对象</title> <style type="text/css"> t ...

  10. 鼠标事件(window.onload的自己的错误)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...