開発環境
- OS X Mavericks - Apple, ときどき
Windows 8.1 + Cygwin64, MinGW (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の22章(例外)、22.3(プログラミング実習)、実習 22-3.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 22-3.
コード(BBEdit, Emacs)
sample447_3.cpp
#include <iostream>
class minus_err {
public:const std::string what;
minus_err(const std::string& i_what): what(i_what) {}
};
class check {
private:
int total_amount;
public:
explicit check();
// check(const check& old_check)
// ~check()
// operator = (const check& old_check)
void add_item(int amount);
int total();
};
inline check::check()
{
total_amount = 0;
}
inline void check::add_item(int amount)
{
total_amount += amount;
if (total_amount < 0)
throw minus_err("minus");
}
inline int check::total()
{
return total_amount;
}
int main()
{
class check a_check;
int i;
for (i = 0; i < 10; ++i) {
a_check.add_item(i + 1);
}
std::cout << a_check.total() << std::endl;
while (true) {
try {
a_check.add_item(-5);
std::cout << a_check.total() << std::endl;
}
catch (minus_err& err) {
std::cerr << "Error: " << err.what << std::endl;
std::cout << a_check.total() << std::endl;
exit (8);
}
}
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
all: sample447_3
sample447_3: sample447_3.cpp fraction.o fraction.h
${CC} ${CFLAGS} -o sample447_3 sample447_3.cpp fraction.o
fraction.o: fraction.cpp fraction.h
${CC} -c -o fraction.o fraction.cpp
clean:
rm sample447_3
入出力結果(Terminal)
$ make && ./sample447_3 g++ -g -Wall -o sample447_3 sample447_3.cpp fraction.o 55 50 45 40 35 30 25 20 15 10 5 0 Error: minus -5 $
0 コメント:
コメントを投稿