相关信息


  1. /*
  2. *
  3. * @subject 数据结构
  4. * @author 信管1142班 201411671210 JJ lai
  5. *
  6. * @program 模板与重载
  7. *
  8. */

实验一:

  1. /*
  2. 要求如下:
  3. 1.设计函数来计算“和”和“积”,在主函数中调用,
  4. 2.能考虑重载函数,使整数和小数均能计算。
  5. */
  6. #include<iostream>
  7. using std::cout;
  8. using std::endl;
  9. int sum(int variable_1, int variable_2)
  10. {
  11. return variable_1 + variable_2;
  12. }
  13. double sum(double variable_1, double variable_2)
  14. {
  15. return variable_1 + variable_2;
  16. }
  17. int product(int variable_1, int variable_2)
  18. {
  19. return variable_1 * variable_2;
  20. }
  21. double product(double variable_1, double variable_2)
  22. {
  23. return variable_1 * variable_2;
  24. }
  25. int main()
  26. {
  27. cout << sum(200, 320) << endl;
  28. cout << sum(2.5, 2.7) << endl;
  29. cout << product(260, 2) << endl;
  30. cout << product(9.20, 100.0) << endl;
  31. }

实验二:

  1. /*
  2. 要求如下:
  3. 1.设计函数来计算“和”和“积”,在主函数中调用,
  4. 2.使用模板实现。
  5. */
  6. #include<iostream>
  7. using std::cout;
  8. using std::endl;
  9. template<typename dataType>
  10. dataType sum(const dataType &variable_1, const dataType &variable_2)
  11. {
  12. return variable_1 + variable_2;
  13. }
  14. template<typename dataType>
  15. dataType product(const dataType &variable_1, const dataType &variable_2)
  16. {
  17. return variable_1 * variable_2;
  18. }
  19. int main()
  20. {
  21. cout << sum(200, 320) << endl;
  22. cout << sum(2.5, 2.7) << endl;
  23. cout << product(260, 2) << endl;
  24. cout << product(9.20, 100.0) << endl;
  25. }

实验三:

  1. /*
  2. 要求如下:
  3. 1.设计函数来计算“和”和“积”,在主函数中调用,
  4. 2.使用类模板实现。
  5. 3.使用多文件。
  6. */

  1. //moban.h
  2. template<typename dataType>
  3. class Plate {
  4. public:
  5. dataType sum(const dataType &variable_1, const dataType &variable_2);
  6. dataType product(const dataType &variable_1, const dataType &variable_2);
  7. private:
  8. dataType variable1_;
  9. dataType variable2_;
  10. };
  11. template<typename dataType>
  12. dataType Plate<dataType>::sum(const dataType &variable_1, const dataType &variable_2)
  13. {
  14. return variable_1 + variable_2;
  15. }
  16. template<typename dataType>
  17. dataType Plate<dataType>::product(const dataType &variable_1, const dataType &variable_2)
  18. {
  19. return variable_1 * variable_2;
  20. }

  1. //main.cpp
  2. #include<iostream>
  3. #include"标头.h"
  4. using std::cout;
  5. using std::endl;
  6. int main()
  7. {
  8. Plate<int>I_plate;
  9. Plate<double>D_plate;
  10. cout << I_plate.sum(200, 320) << endl;
  11. cout << I_plate.sum(2.5, 2.7) << endl;
  12. cout << D_plate.product(260, 2) << endl;
  13. cout << D_plate.product(9.20, 100.0) << endl;
  14. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

数据结构———重载与模板(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. compile ffmpeg

    download SDL 1.2.xxx version source code. 1) configure 2) make & make instll download recent ffm ...

  2. Greedy

    在现实世界中,有这样一类问题:它有n个输入,而它的解就由这n个输入的某个子集组成,不过这个子集必须满足某些事先给定的条件.把那些必须满足的条件称为约束条件:而把满足约束条件的子集称为该问题的可行解.问 ...

  3. Swift函数|闭包

    在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...

  4. java和html的区别

    Java 不是一门程序语言,它是一个平台,也可以说是一门技术. Java 包括 1.Java 程式语言 一个类似 C++ 或 Smalltalk 的物件导向程式语言.学习 Java 程式语言类似学人类 ...

  5. Microsoft Visual Studio 2013 Update 1 离线安装程序

    ☆ 微软官网地址:☆http://www.microsoft.com/zh-cn/download/details.aspx?id=41650☆ 离线安装程序 直接下载链接:☆http://downl ...

  6. IIS启用SSL

    安全套接字层 (SSL) 是一套提供身份验证.保密性和数据完整性的加密技术.SSL 最常用来在 Web 浏览器和 Web 服务器之间建立安全通信通道.它也可以在客户端应用程序和 Web 服务之间使用. ...

  7. asp图片化电话号码,避免蜘蛛之类爬走用户隐私

    作用:将页面中的电话号码生成图片格式.挺多的分类信息类网站使用这个功能.不用真正的生成图片.原理类似验证码,挺不错的. <% Call Com_CreatValidCode(Request.Qu ...

  8. ACM——快速排序法

    快速排序 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:653            测试通过:297 描述 给定输入排序元素数目 ...

  9. 第二十篇、自定义UIButton(设置title和image的位置)

    #import "CustomButton.h" #define ImageW 15 #define ImageH 15 #define KRadio 0.75 @implemen ...

  10. post 提交数据

    1 默认:application/x-www-form-urlencoded 在网页表单中可设置 enctype的值,如果不设,默认是 application/x-www-form-urlencode ...