註冊
登錄
論壇
搜索
幫助
導航
私人消息 (0)
公共消息 (0)
系統消息 (0)
好友消息 (0)
帖子消息 (0)
種子論壇 | 高雄市資訊培育協會學員討論區
»
結訓課程 (青少年程式設計班)
»
C++
» 多載函式 (一)
返回列表
發帖
發短消息
加為好友
tonyh
當前離線
UID
94
帖子
7024
精華
21
積分
210
閱讀權限
150
在線時間
2581 小時
註冊時間
2011-1-8
最後登錄
2024-9-19
超級版主
1
#
跳轉到
»
倒序看帖
打印
字體大小:
t
T
tonyh
發表於 2014-8-30 17:14
|
只看該作者
多載函式 (一)
本帖最後由 tonyh 於 2014-8-30 17:41 編輯
多載函式的定義:
相同的函式名稱,卻擁有不同功能運算。
條件是引入參數的數量不同或是型態不同。
練習:
輸入 三個數字 ,分別利用兩個相同名稱的函式來計算
1. 前兩個數相加
2. 三個數相加
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x, y, z;
cout<<"請依序輸入三個數字: ";
cin>>x>>y>>z;
cout<<"前兩數相加: "<<cal(x,y)<<endl;
cout<<"三個數相加: "<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
附件:
您需要
登錄
才可以下載或查看附件。沒有帳號?
註冊
收藏
分享
發短消息
加為好友
林宇翔
當前離線
UID
67612
帖子
512
精華
0
積分
0
閱讀權限
10
在線時間
104 小時
註冊時間
2014-1-21
最後登錄
2024-5-18
新手上路
2
#
林宇翔
發表於 2014-8-30 17:27
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x,y,z;
cout <<"請輸入3個數字: ";
cin>>x>>y>>z;
cout<<"前兩數相加:"<<cal(x,y) << endl;
cout<<"3個數相加: "<<cal(x,y,z);
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
張峻瑋
當前離線
UID
67589
帖子
368
精華
0
積分
0
閱讀權限
10
在線時間
98 小時
註冊時間
2013-8-12
最後登錄
2016-10-1
新手上路
3
#
張峻瑋
發表於 2014-8-30 17:30
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x,y,z;
cout<<"請依序輸入三個數字: ";
cin>>x>>y>>z;
cout<<"前兩個數相加: "<<cal(x,y)<<endl;
cout<<"三個數相加: "<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
劉得恩
當前離線
UID
67617
帖子
453
精華
0
積分
0
閱讀權限
100
在線時間
109 小時
註冊時間
2014-1-21
最後登錄
2024-2-6
版主
4
#
劉得恩
發表於 2014-8-30 17:31
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x,y,z;
cout<<"請依序輸入三個數字 ";
cin>>x>>y>>z;
cout<<"前兩個數相加:"<<cal(x,y)<<endl;
cout<<"三個數相加:"<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x,int y){
return x+y;
}
int cal(int x ,int y,int z){
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
張彥承
當前離線
UID
67585
帖子
417
精華
0
積分
0
閱讀權限
10
在線時間
74 小時
註冊時間
2013-8-12
最後登錄
2020-7-4
新手上路
5
#
張彥承
發表於 2014-8-30 17:31
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x,y,z;
cout<<"請依序輸入三個字:";
cin>>x>>y>>z;
cout<<"前兩數相加:"<<cal(x,y);
cout<<"三個數相加:"<<cal(x,y,z);
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
周雍程
當前離線
UID
67588
帖子
278
精華
0
積分
0
閱讀權限
10
在線時間
66 小時
註冊時間
2013-8-12
最後登錄
2015-10-24
新手上路
6
#
周雍程
發表於 2014-8-30 17:32
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x,y,z;
cout<<"請輸入依序三個數: ";
cin>>x>>y>>z;
cout<<"前兩個數相加"<<cal(x,y)<<endl;
cout<<"三個數相加"<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x, int y)
{
return x+y;
}
int cal(int x, int y, int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
李允軒
當前離線
UID
67613
帖子
434
精華
0
積分
0
閱讀權限
10
在線時間
115 小時
註冊時間
2014-1-21
最後登錄
2021-4-6
新手上路
7
#
李允軒
發表於 2014-8-30 17:32
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int a(int,int);
int a(int,int,int);
int main()
{
int x,y,z;
cout << "請數入三個數字:" << endl;
cout << "第一個數(x):";
cin >> x;
cout << "第一個數(y):";
cin >> y;
cout << "第一個數(z):";
cin >> z;
cout << "前兩數相加:" << a(x,y) << endl;
cout << "三個數相加" << a(x,y,z) << endl;
system("pause");
return 0;
}
int a(int x,int y)
{
return x + y;
}
int a(int x,int y,int z)
{
return x + y + z;
}
複製代碼
TOP
發短消息
加為好友
劉得旗
當前離線
UID
67543
帖子
184
精華
0
積分
0
閱讀權限
10
在線時間
30 小時
註冊時間
2013-1-21
最後登錄
2014-9-27
新手上路
8
#
劉得旗
發表於 2014-8-30 17:33
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal (int,int);
int cal (int,int,int);
int main()
{
int x,y,z;
cout<<"依序輸入3個數字";
cin>>x>>y>>z;
cout<<"前2數相加"<<cal(x,y)<<endl;
cout<<"3個數相加"<<cal(x,y,z);
system("pause");
return 0;
}
int cal (int x, int y)
{
return x+y;
}
int cal (int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
張郁庭
當前離線
UID
67586
帖子
284
精華
0
積分
0
閱讀權限
10
在線時間
63 小時
註冊時間
2013-8-12
最後登錄
2023-5-17
新手上路
9
#
張郁庭
發表於 2014-9-5 20:06
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x, y, z;
cout<<"請依序輸入三個數字: ";
cin>>x>>y>>z;
cout<<"前兩數相加: "<<cal(x,y)<<endl;
cout<<"三個數相加: "<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
發短消息
加為好友
劉得旗
當前離線
UID
67543
帖子
184
精華
0
積分
0
閱讀權限
10
在線時間
30 小時
註冊時間
2013-1-21
最後登錄
2014-9-27
新手上路
10
#
劉得旗
發表於 2014-9-13 16:26
|
只看該作者
#include<iostream>
#include<cstdlib>
using namespace std;
int cal(int,int);
int cal(int,int,int);
int main()
{
int x, y, z;
cout<<"依序輸入三個數字: ";
cin>>x>>y>>z;
cout<<"前兩數相加: "<<cal(x,y)<<endl;
cout<<"三個數相加: "<<cal(x,y,z)<<endl;
system("pause");
return 0;
}
int cal(int x,int y)
{
return x+y;
}
int cal(int x,int y,int z)
{
return x+y+z;
}
複製代碼
TOP
返回列表
谷哥人營隊
113Scratch夏令營
113年APCS冬令營
113年Scratch冬令營
113年Python研習冬令營
113年Python證照特訓營(一)
113年谷哥人程式體驗營
113年國三專班
113年Python研習夏令營
113年Python證照特訓營(二)
113年程式夏令營(一)(二)
113年APCS夏令營(一)(二)
112年國三專班
112APCS夏令營(一)(二)
112App快速開發夏令營
C語言 / C++ (特別輔導)
C++證照
C#
家教特輔
C語言特輔/證照(家教)
C++證照
C#
HTML5+CSS+JavaScript+PHP+MySQL
Java 家教 (王捷恩)
113新版塊名稱
快樂學 Scratch
Python 家教 (王捷恩 康恒睿)
Python 特別輔導 (家教)
快樂 C++ (家教)
我愛 Java (家教)
程式解題我最行 (家教)
程式常態班
C++ 新生挑戰區
考照心得分享
快樂 C++11307週五19:00
快樂 C++11303 (週六15:40-17:40) 3F
程式解題我最行 (週六15:30-17:30) 3F
快樂 C++ (週六13:30-15:30) 3F
快樂 C++ (週六19:00-21:00) 3F
程式解題我最行 (週六10:00-12:00) 3F
快樂學 Scratch
程式解題我最行(週五19:00-21:00)
快樂 C++ (週六13:30-15:30) 3F
程式解題我最行(週三19:15-21:15)
快樂 C++11207週六10
快樂 C++11208週六19:00
程式解題我最行 (週五19:00-21:00) 3F
快樂 C++ (週六15:30-17:30) 3F
程式解題我最行 (週六19:00-21:00) 3F
程式解題我最行 (週四19:10-21:10)
產投職訓
結訓課程 (產投職訓)
Php & MySQL old
Illustrator old
Dreamweaver old
Android手機程式開發班
PHP & MySQL電子商務互動式網站實作班 (102下)
PHP & MySQL (102上)
PHP & MySQL電子商務互動式網站實作班
Photoshop數位影像設計初階
Flash創意廣告動畫初階
行銷短片視訊剪輯
數位商業攝影實務班
PHP & MySQL電子商務系統開發實務初階班
電子商務系統開發實務中階班
Server基礎架設&動態網頁設計初階班
Java視窗應用程式設計與遊戲開發班
Illustrator時尚插畫創作設計初階班
102上Php & MySQL 初階班
電子商務互動式網站實作中階
Dreamweaver多媒體網頁設計
Android手機程式開發班(2012年10月)
PHP & MySQL (2012年10月)
創意塗鴉
yahoo橫幅
google橫幅
市民學苑
第二屆樂活部落格
第一屆電腦設備簡易維護和故障排除班
專案訓練
電子商務創業班
TQC PHP認證
投資理財班
領隊導遊班
電腦基礎及網路應用身心障礙專班
應用軟體網頁化開發
[收藏此主題]
[關注此主題的新回復]
[通過 QQ、MSN 分享給朋友]