開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅢ部(高度な型とクラス)の14章(クラス - その2)、14.7(プログラミング実習)、実習 14-1.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 14-1.
コード(BBEdit, Emacs)
sample244_1.cpp
#include <iostream>
class class_a {
private:
int used;
public:
explicit class_a()
{
used = 0;
}
// a(const a& old_class_a)
// ~a()
// operator = (const a& old_class_a)
void use_file()
{
used = 1;
}
void unuse_file()
{
used = 0;
}
friend int used_a(const class_a& a);
};
class class_b {
private:
int used;
public:
explicit class_b()
{
used = 0;
}
// class_b(const b& old_class_b)
// ~class_b()
// operator = (const class_b& old_class_b)
void use_file()
{
used = 1;
}
void unuse_file()
{
used = 0;
}
friend int used_b(const class_b& b);
};
int used_a(const class_a& a)
{
return a.used;
}
int used_b(const class_b& b)
{
return b.used;
}
int used(const class_a& a, const class_b& b)
{
if (used_a(a) == 1 || used_b(b) == 1)
return 1;
return 0;
}
int main()
{
class_a a;
class_b b;
std::cout << used(a, b) << '\n';
a.use_file();
std::cout << used(a, b) << '\n';
a.unuse_file();
std::cout << used(a, b) << '\n';
b.use_file();
std::cout << used(a, b) << '\n';
b.unuse_file();
std::cout << used(a, b) << '\n';
return (0);
}
Makefile
#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample244_1
sample244_1: sample244_1.cpp
${CC} ${CFLAGS} -o sample244_1 sample244_1.cpp
clean:
rm sample244_1
入出力結果(Terminal)
$ make && ./sample244_1 g++ -g -Wall -o sample244_1 sample244_1.cpp 0 1 0 1 0 $
0 コメント:
コメントを投稿