C++知识点案例 笔记-5
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;
}
C++知识点案例 笔记-5的更多相关文章
- C++知识点案例 笔记-6
1.三种友元函数 -非模板友元函数 -约束模板友元函数 -非约束模板友元函数 2.非类型参数 3.模板特化 1.三种友元函数 =====三种友元函数===== --1---非模板友元函数 #inclu ...
- C++知识点案例 笔记-4
1.纯虚函数 2.抽象类 3.内部类 4.运算符重载 5.类的函数重载 6.友元的函数重载 1.纯虚函数 ==纯虚函数== //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数 ...
- C++知识点案例 笔记-3
1.基类指针等与派生类的兼容 2.构造函数 3.析构函数 4.虚基类 5.虚函数 6.虚析构函数 ==基类指针等与派生类的兼容== #include <iostream> #include ...
- C++知识点案例 笔记-2
1.友元函数 2.友元类 3.继承(公有继承) 4.公有继承的访问权限 5.私有继承的访问权限 6.保护继承的访问权限(两次继承) ==友元函数== #include <iostream> ...
- C++知识点案例 笔记-1
1.重载函数 2.内联函数 3.New.Delete 4.重载与.const形参 5.常数据成员 6.静态成员函数 ==重载函数== #include <iostream> using n ...
- Java后端高频知识点学习笔记1---Java基础
Java后端高频知识点学习笔记1---Java基础 参考地址:牛_客_网 https://www.nowcoder.com/discuss/819297 1.重载和重写的区别 重载:同一类中多个同名方 ...
- [置顶] 单片机C语言易错知识点经验笔记
今天写这一篇文章并不是因为已经想好了一篇文章才写下来,而是我要将这一篇文章作为一个长期的笔记来写,我会一直更新.在进行单片机开发时,经常都会出现一些很不起眼的问题,这些问题其实都是很基础的c语言知识点 ...
- 面试总结:鹅厂Linux后台开发面试笔试C++知识点参考笔记
文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 文章是由自己笔试面试腾讯的笔记整理而来,整理的时候又回顾了一遍,中间工 ...
- php 知识点 --个人笔记
##2015-09-06 为防止用户看到错误信息,而出现的不友好界面.故一般性会在php.ini里设置:display_errors = Off;不过在开发的时候,我们有时候需要打开错误信息.这时候, ...
随机推荐
- mp4视频中插入文字
最近接到一个需求,需要往mp4中动态插入文字,并且mp4中的乌云能在文字上有飘动的效果,一开始想用canvas,但是由于本人经验不足,没什么思路,看到css3有一个属性:mix-blend-mode, ...
- 如何写好一个 Spring 组件
背景 Spring 框架提供了许多接口,可以使用这些接口来定制化 bean ,而非简单的 getter/setter 或者构造器注入.细翻 Spring Cloud Netflix.Spring Cl ...
- 【CTF】WDCTF-finals-2017 3-11 writeup
题目来源:WDCTF-finals-2017 题目链接:https://adworld.xctf.org.cn/task/answer?type=misc&number=1&grade ...
- create-react-app 核心思路分析
原文链接:http://axuebin.com/articles/fe-solution/cli/cra.html,转载请联系 Create React App is an officially su ...
- 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL
1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...
- Cube painting UVA - 253
We have a machine for painting cubes. It is supplied with three different colors: blue, red and gre ...
- Unix ls UVA - 400
The computer company you work for is introducing a brand new computer line and is developing a new ...
- 概A第二章测试
以下判断题全是(√) 问题 1 得 10 分,满分 10 分 问题 2 得 10 分,满分 10 分 0-1分布相当于一个特殊的二项分布b(1,p) ...
- 【软件工程】《构建之法》 & Git+ & CI/CD
<构建之法> & Git+ & CI/CD 个人阅读作业#2 项目 内容 本作业所属课程 2020春季软件工程(罗杰 任健) 本作业要求 个人阅读作业#2 我的课程目标 具 ...
- 关于height:100%
要想高度百分比起作用,一般来说,要满足两个条件:其一,父标签有高度可寻,就是向上遍历父标签要找到一个定值高度(body,html另外讨论),如果中途有个height为auto或是没有设置height属 ...