重载double、int、char

public class F_OverLoad {
 
 public static void load(int i) {
  System.out.print("int"+i);
 }
 
 public static void load(double i) {
  System.out.print("double"+i);
 }
 
 public static void load(char i) {
  System.out.print("char"+i);
 }
  
 public static void main(String[] args){
     double d = 2;
     load(d);
 }
}

 

输出:double2.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题我会尽力回答。以下是函数模板bubble和字符串类型的特化版本bubble的定义,以及STUDENT类的定义和重载"<"运算符的代码: ```c++ #include<iostream> #include<string> using namespace std; // STUDENT类的定义 class STUDENT{ public: string id; // 学号 string name; // 姓名 int age; // 年龄 string department; // 系别 // 重载<运算符,用来比较两个对象的大小 bool operator<(const STUDENT& s){ return id < s.id; } }; // 声明一个函数模板bubble,用来处理基本数据类型int、float、doublechar、STUDENT template<class TYPE> void bubble(TYPE A[], int len); // 定义bubble的特殊版本,用来处理字符串类型 template<> void bubble(char* Strs[], int len); // bubble函数模板的定义 template<class TYPE> void bubble(TYPE A[], int len){ for(int i=0; i<len-1; i++){ for(int j=0; j<len-i-1; j++){ if(A[j] > A[j+1]){ swap(A[j], A[j+1]); } } } // 打印排序后的数组 for(int i=0; i<len; i++){ cout << A[i] << " "; } cout << endl; } // bubble函数模板的字符串类型特化版本的定义 template<> void bubble(char* Strs[], int len){ for(int i=0; i<len-1; i++){ for(int j=0; j<len-i-1; j++){ if(strcmp(Strs[j], Strs[j+1]) > 0){ swap(Strs[j], Strs[j+1]); } } } // 打印排序后的字符串数组 for(int i=0; i<len; i++){ cout << Strs[i] << " "; } cout << endl; } int main(){ // 测试bubble函数模板 int intArr[] = {4, 2, 1, 5, 3}; bubble(intArr, 5); float floatArr[] = {3.2, 1.5, 4.6, 2.0, 5.1}; bubble(floatArr, 5); double doubleArr[] = {5.6, 2.3, 4.1, 1.7, 3.9}; bubble(doubleArr, 5); char charArr[] = {'d', 'f', 'a', 'c', 'e', 'b'}; bubble(charArr, 6); STUDENT stuArr[] = {{"20210001", "张三", 20, "计算机科学与技术"}, {"20210003", "李四", 22, "数学与应用数学"}, {"20210002", "王五", 21, "软件工程"}}; bubble(stuArr, 3); // 测试bubble函数模板的特化版本 char* strArr[] = {"hello", "world", "C++", "programming", "language"}; bubble(strArr, 5); return 0; } ``` 注意:在这里,我们使用了std::string类型来定义STUDENT类中的学号、姓名和系别等信息。同时,需要在STUDENT类定义中重载"<"运算符,才能够使用STUDENT类类型作为bubble函数模板的参数类型。 希望我的回答能够帮助到你,如果你还有任何问题,欢迎随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值