1.关系运算符重载

2.类型转换函数重载

3.转换构造函数

4.函数模板

5.显式实例化

6.类模板外定义模板函数

1.关系运算符重载

==关系运算符重载==

//直接(按分数)比较两个对象
#include<iostream>
using namespace std;
class Student
{ public:
friend ostream& operator<< (ostream& os,const Student& st);
friend istream& operator>> (istream& is, Student& st); friend bool operator == (const Student& st1,const Student& st2);
friend bool operator != (const Student& st1,const Student& st2);
friend bool operator > (const Student& st1,const Student& st2);
friend bool operator < (const Student& st1,const Student& st2);
private:
int id;
double score; };
ostream& operator<< (ostream& os,const Student& st)
{
os <<"id:"<<st.id<<"score"<<st.score;
return os;
}
istream& operator>> (istream& is,Student& st)
{
is >> st.id >> st.score;
return is;
} bool operator == (const Student& st1,const Student& st2)
{
return st1.score == st2.score;
}
bool operator != (const Student& st1,const Student& st2)
{
return !(st1.score == st2.score);
}
bool operator > (const Student& st1,const Student& st2)
{
return (st1.score > st2.score);
}
bool operator < (const Student& st1,const Student& st2)
{
return (st1.score < st1.score);
}
int main()
{
Student st1,st2;
cout<<"请输入两名学生的ID和score:"<<endl;
cin>>st1>>st2;
if (st1 > st2)
{
cout<<"st1分数高于st2"<<endl;
}
else if(st1 < st2)
{
cout<<"st1分数低于st2"<<endl;
}
else
{
cout<<"两名学生分数相同"<<endl;
}
return 0;
}

2.类型转换函数重载

==类型转换函数重载==

#include<iostream>
using namespace std;
class bian
{
private:
int m,n;
public:
bian(int m1,int n1):m(m1),n(n1){}
void show()
{
cout<<"bb:"<<endl;cout<<m<<","<<n<<endl;
}
operator char()
{
return static_cast<char>(m);
}
};
int main()
{
bian bb(69,96);
bb.show();
char ch = bb;
cout<<ch<<endl;
return 0;
}

3.转换构造函数

==转换构造函数==

#include<iostream>
using namespace std;
class jie
{
private:
int m,n;
public:
jie(int a, int b):m(a),n(b){}
jie(double c)
{
cout<<"把m转换成字符型,并给n一个值"<<endl;
m = static_cast<int>(c);
n = 0;
}
void show()
{
cout<<m<<" "<<n<<endl;
} };
int main()
{
jie s1(58,68);
jie s2(2.333);
cout<<"s1"<<endl;
s1.show();
cout<<"s2"<<endl;
s2.show();
return 0; }

4.函数模板

==函数模板==
//目的 函数运行与类型无关
#include<iostream>
using namespace std;
template<typename T>
T add(T x,T y)
{
return x+y;
}
int main()
{
cout<<add(5,516)<<endl;
cout<<add(3.1415,6.011)<<endl; return 0;
}

5.显式实例化

==显式实例化==
//指定参数类型,便于不同类型数据类型相加
#include<iostream>
using namespace std;
template<typename T>
T add(T x,T y)
{
return x+y;
}
template int add<int>(int x,int y);
int main()
{
cout<<add(5,516)<<endl;
cout<<add<int>(3.1415,'a')<<endl; return 0;
}

6.类模板外定义模板函数

==类模板外定义模板函数==
#include<iostream>
using namespace std;
template<typename T>
class Fly
{
private: int size;
T* str;
public:
Fly(T fly[],int s);
void show();
};
//类模板外定义成员函数
template<typename T>
Fly<T>::Fly(T fly[],int s)
{
str = new T[s];
size = s ;
for(int i = 0;i < size; i++)
{
str[i] = fly[i];
}
}
template<typename T>
void Fly<T>::show()
{
for(int k = 0; k <size; k++)
{
cout<<*(str + k)<<endl;
}
}
int main()
{
char ffly[]={'a','d','f','e','t'};
//创建类模板对象
Fly<char> F1(ffly,5);
F1.show(); int jfly[]={5,2,1,1314};
//创建类模板对象
Fly<int> F2(jfly,4);
F2.show(); return 0;
}

最新文章

  1. lamp
  2. python wordcloud 对电影《我不是潘金莲》制作词云
  3. 窗口之间的主从关系与Z-Order
  4. arcgis engine 调用arcgis server服务
  5. yii 的网址收藏
  6. mysql 备份恢复图
  7. Java Hour 46 SLF4J
  8. 四十条测试你是不是合格的PHP程序员
  9. 【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
  10. C语言数组和指针的理解_在取地址运算上的操作_指针加减操作_a 和&amp;a 的区别
  11. 新版703n刷openwrt
  12. pptv web前端面试题
  13. OPENWRT make menuconfig错误之一
  14. css的选择器的优先级
  15. Python爬取腾讯新闻首页所有新闻及评论
  16. Java 8 中的方法引用
  17. bash处理一条命令的步骤
  18. sqlserver 全局事务查询
  19. win10中强制vs2015使用管理员启动
  20. Nginx 线上配置实例

热门文章

  1. 2020 OO 第三单元总结 JML语言
  2. Java代码度量分析工具:DesigniteJava简介
  3. Java异常系列
  4. ISODateTimeFormat 转换2019-08-15T00:36:49.366456463Z 日期格式
  5. adb下载安装
  6. 算法、数据结构、与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design)
  7. ajax--实现异步请求,接受响应及执行回调
  8. 360加固保so动态脱壳
  9. UVA10391复合词
  10. C#-Stmp发邮件