私の以前の習慣は、定義されたすべての関数をヘッダーファイルに入れることでした。しかし、C ++ Primer 6.1を思い出して、セキュリティ上のリスクがあることを学びました。
しかし、この本(英語版ページ207)は非常にわかりにくく、特に.ccサフィックスはまったく見つかりませんでした。検索した結果、Windowsでは.cppがより一般的に使用されていることがわかりました。
チェーンはそれほど難しくないようです。ヘッダーファイルを一緒に含めるだけです。
以下は、演習6.9のトピックです。(同時に、これは演習6.25および6.26 関数のコマンドラインパラメータの実験でもあります)
タイトル(演習6.9)
ヘッダーファイル:
- Chapter6.h
#ifndef CHAPTER_6
#define CHAPTER_6
int factorial(unsigned n); // a declaration
#endif // !CHAPTER_6
ソースファイル:
- fact.cpp
#include "Chapter6.h"
int factorial(unsigned n) // definition
{
unsigned ret = 1;
if (n)
{
for (unsigned i = n; i != 0; i--)
ret *= i;
}
return ret;
}
- factMain.cpp
#include <iostream>
#include "Chapter6.h"
using namespace std;
int main()
{
unsigned num;
cout << "Please enter a natural number: ";
cin >> num;
cout << num << "! = " << factorial(num) << endl;
return 0;
}
次の手順職務を遂行する:
- Chapter6.h:ステートメント関数
- fact.cpp:定義関数
- factMain.cpp:メインプログラム
関数コマンドラインパラメーターのテスト(演習6.25および6.26)
factMain.cppを変更します。
#include <iostream>
#include "Chapter6.h"
using namespace std;
int main(int argc, char *argv[])
{
unsigned num;
cout << "Please enter a natural number: ";
cin >> num;
cout << num << "! = " << factorial(num) << endl;
// Test for Command-Line Options
string str;
for (int i = 1; i != argc; ++i) {
str += argv[i];
str += " ";
}
cout << str << endl;
for (int i = 0; i < argc; i++)
{
cout << "argc[" << i << "]: " << argv[i] << endl;
}
return 0;
}
出力;
何故なの???
コマンドラインはどれですか?本で負けるのは正しくない!!!
こちらもご覧ください
テディヴァンジェリーのナビゲーションページで
、C ++の個別のコンパイルについて、改行なしで考えている(関数は無効)
[C ++ Primer(5th Edition)演習]演習プログラム-第6章(第6章)