数据结构———重载与模板(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++语言作为算法描述语言,应用包含规范 ...
随机推荐
- compile ffmpeg
download SDL 1.2.xxx version source code. 1) configure 2) make & make instll download recent ffm ...
- Greedy
在现实世界中,有这样一类问题:它有n个输入,而它的解就由这n个输入的某个子集组成,不过这个子集必须满足某些事先给定的条件.把那些必须满足的条件称为约束条件:而把满足约束条件的子集称为该问题的可行解.问 ...
- Swift函数|闭包
在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...
- java和html的区别
Java 不是一门程序语言,它是一个平台,也可以说是一门技术. Java 包括 1.Java 程式语言 一个类似 C++ 或 Smalltalk 的物件导向程式语言.学习 Java 程式语言类似学人类 ...
- Microsoft Visual Studio 2013 Update 1 离线安装程序
☆ 微软官网地址:☆http://www.microsoft.com/zh-cn/download/details.aspx?id=41650☆ 离线安装程序 直接下载链接:☆http://downl ...
- IIS启用SSL
安全套接字层 (SSL) 是一套提供身份验证.保密性和数据完整性的加密技术.SSL 最常用来在 Web 浏览器和 Web 服务器之间建立安全通信通道.它也可以在客户端应用程序和 Web 服务之间使用. ...
- asp图片化电话号码,避免蜘蛛之类爬走用户隐私
作用:将页面中的电话号码生成图片格式.挺多的分类信息类网站使用这个功能.不用真正的生成图片.原理类似验证码,挺不错的. <% Call Com_CreatValidCode(Request.Qu ...
- ACM——快速排序法
快速排序 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:653 测试通过:297 描述 给定输入排序元素数目 ...
- 第二十篇、自定义UIButton(设置title和image的位置)
#import "CustomButton.h" #define ImageW 15 #define ImageH 15 #define KRadio 0.75 @implemen ...
- post 提交数据
1 默认:application/x-www-form-urlencoded 在网页表单中可设置 enctype的值,如果不设,默认是 application/x-www-form-urlencode ...