c++学习笔记之类模板
类是对象的抽象,类模板是类的抽象。
比较两个数(不同类型)的大小
在类模板内定义成员函数
#include<iostream>
using namespace std;
template<class numtype>
class compare
{public:compare(numtype a,numtype b){x=a;y=b;}numtype max(){return(x>y)?x:y;}numtype min(){return(x<y)?x:y;}private:numtype x,y;} ;int main(){compare<int> cmp1(3,7);cout<<cmp1.max()<<"is the maximum of two integer numbers"<<endl;cout<<cmp1.min()<<"is the minimum of two integer numbers"<<endl<<endl;compare<float>cmp2(45.78,93.6);cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl<<endl;compare<char>cmp3('a','A');cout<<cmp3.max()<<"is the maximum of two characters"<<endl;cout<<cmp3.min()<<"is the minimum of two characters"<<endl<<endl;}
在类模板外定义成员函数
#include<iostream>
using namespace std;
template<class numtype>
class compare
{public:compare(numtype a,numtype b);numtype max();numtype min();private:numtype x,y;} ;template <class numtype>compare<numtype>::compare(numtype a,numtype b){x=a;y=b;}template<class numtype>numtype compare<numtype>::max(){return (x>y)?x:y;}template<class numtype>numtype compare<numtype>::min(){return(x<y)?x:y;}int main(){compare<int> cmp1(3,7);cout<<cmp1.max()<<"is the maximum of two integer numbers"<<endl;cout<<cmp1.min()<<"is the minimum of two integer numbers"<<endl<<endl;compare<float>cmp2(45.78,93.6);cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl<<endl;compare<char>cmp3('a','A');cout<<cmp3.max()<<" is the maximum of two characters"<<endl;cout<<cmp3.min()<<" is the minimum of two characters"<<endl<<endl;}
c++学习笔记之类模板相关推荐
- OpenCV 学习笔记(模板匹配)
OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...
- D3D9学习笔记之模板
学习目标: 理解模板缓存的工作原理,如何创建模板缓存以及如何对模板缓存进行控制 了解如何实现镜面效果,以及如何使用模板缓存阻止物体映像在非镜面区域中的绘制 掌握如何绘制阴影,以及如何借助模板缓存阻止& ...
- C++学习笔记【模板】
主要内容来源:https://www.bilibili.com/video/BV1et411b73Z?spm_id_from=333.999.0.0 一.函数模板 1.函数模板的概念 template ...
- Python学习笔记——Flask模板
目录 3.1 模板 3.2 过滤器: 3.3 Web表单: 3.4 控制语句 3.5 宏.继承.包含 3.6 Flask中的特殊变量和方法: 3.1 模板 在前面的示例中,视图函数的主要作用是生成请求 ...
- 黑马ajax学习笔记02--art-template模板,自动提示,防抖,三级联动,fromData传参及传文件,同源,jsonp,天气预报,CROS,服务器桥接,withCredential跨域登录
1.模板引擎概述 作用:使用模板引擎提供的模板语法,可以将数据和HTML拼接起来 实际上是实现在客户端做数据拼接 art-template模板引擎 官网:http://aui.github.io/ar ...
- C++学习笔记:模板参数
本章节主要学习一下模板形参的基本知识. 模板参数有三种类型:类型模板参数.模板的模板参数(以模板作为模板的参数).非类型模板参数. 类型模板参数 类型模板参数是我们使用模板的主要目的.也就是普通的类型 ...
- Django学习笔记之模板渲染、模板语言、simple_tag、母版子版、静态配置文件
一.首先我们用PyCharm来创建一个Django项目 终端命令:django-admin startproject sitename 图形创建: 这样一个Django项目就创建完成了,上面可以看到项 ...
- Symfony2 学习笔记之模板使用
我们知道,controller负责处理每一个进入Symfony2应用程序的请求.实际上,controller把大部分的繁重工作都委托给了其它地方,以使代码能够被测试和重用.当一个controller需 ...
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义
模板与泛型编程 --模板定义 引言: 所谓泛型程序就是以独立于不论什么特定类型的方式编写代码.使用泛型程序时,我们须要提供详细程序实例所操作的类型或值. 模板是泛型编程的基础.使用模板时能够无须了解模 ...
最新文章
- IOS开发中多线程的使用
- 中国牡蛎碳酸钙市场需求现状调研及十四五投资风险评估报告2022-2028年版
- 为什么在释放锁的时候是从 tail 进行扫描
- Java int -1无符号右移_java中的无符号右移
- Magento布局layout.xml文件详解
- 郭明錤对明年新款iPhone卖点进行预测:支持5G 摄像头改进
- [ACM_数据结构] Color the ball [线段树水题][数组开大]
- svn添加提交备注限制和自动发布web项目
- std::function
- linux设置开机自启动的方式总结
- Hibernate基础及配置
- 【预测模型】基于matlab GUI BP神经网络预测【含Matlab源码 934期】
- android ssh软件,优秀的 Android SSH 工具推荐
- BFS算法解题套路框架(附习题)
- 《程序员》2012年7期精彩内容:智能算法
- cebemax hal库 stm32 OLED移植 解析
- Unity3D官方案例1-星际航行游戏Space Shooter
- plsr matlab,matlab中的偏最小二乘回归(PLSR)和主成分回归(PCR)
- duplicate column name
- C语言比较两个字符串相等为什么不是用“==”