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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JFLEARN

CSDN这么拉会有人打赏?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值