#include "子函數3.h"
#include <iostream>//嘗試過把這兩個庫函數放在,自定義的函數中,但不行
using namespace std; //因爲只有在調用自定義函數的情況下(就是使用swap),纔去找這個文件,找到之後,
//使用完之後就退出,那麼當使用“cout",就會發現沒有定義,因爲確實沒有declared iostream 這個函數庫
int main()//那麼想說的就是:**先定義後使用**。
{
int m=11,n=29;
swap(m,n);
cout<<m<<' '<<n;
return 0;;
}
庫函數如此,其他也一樣。
#include <iostream>
using namespace std;
class time
{
public:
int hour;
int minute;
int second;
};
int show_time(time *t) //爲什麼會出錯????
{//在使用time來定義t的時候,必須要在該文件下定義time;(這和所有的形式參數一樣,先定義後使用)
int m;
for(m=0;m<2;m++)
{
cin>>t[m].hour>>t[m].minute >>t[m].second ;//在這裏可以添加上hour minute second的範圍
cout<<t[m].hour <<":"<<t[m].minute <<':'<<t[m].second <<endl;
}
return 0;
}
這是主函數
#include <iostream>
#include "例題2.3子函數.cpp"
using namespace std;
int main()
{
int n;
cin>>n;
time *t=new time;
show_time(t);
return 0;
}
time必須在定義之後才能使用,也就是將class time放在子函數的文件夾中。