MOOC清华《面向对象程序设计》第2章:函数重载实验

来源:互联网 发布:战斧主机 知乎 编辑:程序博客网 时间:2024/06/06 03:02

实验一:(转载自学堂在线《面向对象程序设计(C++)》,注释主要是我加的)

#include <iostream>  using namespace std;    /*void print(char* msg){ //<1>cout << "message: " << msg << endl;}*///当主调函数中print()的实参为字符串时,会出现//deprecated conversion from string constant to 'char*'//(不赞成从字符串常量到字符指针的转换)的报错 void print(char msg){ //<1>,去掉星号*就能得到正确答案,否则实参字符作整数处理 cout << "message: " << msg << endl;}void print(int score){ //<2>cout << "score = " << score << endl;} int main(){      //print("Hello"); //调用<1>处函数,DevCpp不支持print('L'); //调用<1>处函数 print(94); //调用<2>处函数     return 0;  }  



实验二:

#include <iostream>  using namespace std;    void test(int a, int b){cout << "执行函数<1>:" << a + b << endl;}void test(int a, char b){if(b >= 65 && b <= 90)b = b + 32;cout << "执行函数<2>: " << a << b << endl;} int main(){      test(9, 8);    test(10, 'A');    return 0;  }  


阅读全文
0 0
原创粉丝点击