----------------------------------------------------------------------------------------
// science.cpp
#include <iostream>
#include "science.h" //只要cpp底下include 進這個檔名就可使用
---------------------------------------------------------------------
利用一些簡單的範例幫助理解:
//science.h
#include <iostream>
using std::cout;
void printscience(double a)
{
cout<<a;
}
//science.cpp
#include <iostream>
#include "science.h"
int main( )
{
using std::cout;
double b;
printscience(b);
return 0;
}
只要把上面的cpp檔compile 就可以形成一個執行檔
---------------------------------------------------------------
接下來我要介紹一個方法,我們常常會在main()前面用#define 來定義一個常數或數值但但常常會面臨當重複定義常數的問題,就像上面所提到。如果今天發現沒有定義那也可以用#ifndef的方法來進行標頭檔的定義。
------------------------------------------------------------------
//science.h
#ifndef SCIENCE_H_
#define SCIENCE_H_
struct data
{
char name;
int age;
};
typedef struct data DATA;
#endif
將上述的標頭檔定義完後變能夠充分的使用他了,接下來只要跟前面範例方法一樣#include "science.h"便能講整個檔案作定義出使用struct這個架構,這麼簡單的方法一定要快點學起來才行。
沒有留言:
張貼留言