用C++编写程序:利用函数重载编写三个分别求短整数,整数和双精度数绝对值的函数,要求有输入和输出

如题所述

#include <iostream>
/******/
short abs(short n) { return n > 0 ? n : -n;  }
int abs(int n) { return n > 0 ? n : -n;  }
double abs(double n) { return n > 0 ? n : -n;  }
/******/
int main()
{
    short a;
    int b;
    double c;
    std::cin >> a >> b >> c;
    std::cout << abs(a) << std::endl << abs(b) << std::endl << abs(c) << std::endl;
}

其实/*****/下里面可以这样写:

template <typename T> T abs(const T &n) { return n > 0 ? n : -n; }

温馨提示:答案为网友推荐,仅供参考
相似回答