zoukankan      html  css  js  c++  java
  • 第四章函数作业题,函数的重载

    什么是重载?

    就是一个函数名多次使用,通过参数不同实现不同的作用。

    当在主函数中调用重载的函数时,要找参数类型相同的函数。

    例题4.8 求3个数当中的最大的数?

    因为我们刚才求的最大数是整数当中的最大数,如果参数变为双精度获长整型则需要重新编程,功能受限。

    #include <iostream>
    using namespace std;
    
    int max(int x ,int y,int z);
    double max(double x ,double y,double z);
    long max(long x ,long y,long z);
    
    int main(){
        
        int a,b,c,m;
        cout<<"请你输入三个整型的数字:"<<endl;
        cin>>a>>b>>c;
        m=max(a,b,c);
        cout<<"The max of a b and c is:"<<m<<endl;
        
        double ad,bd,cd,md;
        cout<<"请你输入三个小数类型的数字:"<<endl;
        cin>>ad>>bd>>cd;
        md=max(ad,bd,cd);
        cout<<"The max of a b and c is:"<<md<<endl;
        
        long al,bl,cl,ml;
        cout<<"请你输入三个长整型的数字:"<<endl;
        cin>>al>>bl>>cl;
        ml=max(al,bl,cl);
        cout<<"The max of a b and c is:"<<ml<<endl;
        return 0;
    }
    //内联函数 一个关键字inline 
    inline int max(int x ,int y,int z){
        if(z>x)
        x=z;
        if(y>x)
        x=y;
        return x;
    }
    inline double max(double x ,double y,double z){
        if(z>x)
        x=z;
        if(y>x)
        x=y;
        return x;
    }
    inline long max(long x ,long y,long z){
        if(z>x)
        x=z;
        if(y>x)
        x=y;
        return x;
    }
  • 相关阅读:
    Linux之开源软件移植
    数学问题的解题方法(模板)
    图论相关算法理解和总结
    关于ACM,关于CSU
    hdu 4607(树的直径)
    树的直径
    SGU-181 X-Sequence
    1629 B君的圆锥
    1134 最长递增子序列(暴力写的)
    1483 化学变换(暴力)
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7797365.html
Copyright © 2011-2022 走看看