開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅢ部(高度な型とクラス)の13章(シンプルなクラス)、13.9(プログラミング実習)、実習 13-1.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 13-1.
コード(BBEdit, Emacs)
sample233_1.cpp
#include <iostream>
#include <assert.h>
class parity {
private:
int count;
public:
explicit parity(); // parityを初期化(コンストラクタ)
// parity(const parity& old_parity)
// 自動的に生成されるコピーコンストラクタを使用
// ~parity()
// 自動的に生成されるデストラクタを使用
// operator = (const parity& old_parity)
// 自動的に生成される代入演算子を使用
void put();
int test();
};
inline parity::parity()
{
count = 0;
}
inline void parity::put()
{
assert(count >= 0);
++count;
}
inline int parity::test()
{
assert(count >= 0);
if (count % 2 == 0)
return true;
return false;
}
int main()
{
class parity a_parity;
int n = 10;
int i;
for (i = 0; i < n; ++i) {
std::cout << i << ": " << a_parity.test() << '\n';
a_parity.put();
}
return (0);
}
Makefile
#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample233_1
sample233_1: sample233_1.cpp
${CC} ${CFLAGS} -o sample233_1 sample233_1.cpp
clean:
rm sample233_1
入出力結果(Terminal)
$ make && ./sample233_1 g++ -g -Wall -o sample233_1 sample233_1.cpp 0: 1 1: 0 2: 1 3: 0 4: 1 5: 0 6: 1 7: 0 8: 1 9: 0 $
0 コメント:
コメントを投稿