2014年3月19日水曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅢ部(高度な型とクラス)の14章(クラス - その2)、14.7(プログラミング実習)、実習 14-2.を解いてみる。

その他参考書籍

実習 14-2.

コード(BBEdit, Emacs)

sample244_2.cpp

#include <iostream>

class pig {
private:
  static int count;
public:
  explicit pig() {
    ++count;
  }
  // pig(const pig& old_pig)
  ~pig() {
    --count;
  }
  // operator = (const pig& old_pig)
  static int get_count() {
    return count;
  }
};

int pig::count = 0;

class horse {
private:
  static int count;
public:
  explicit horse() {
    ++count;
  }
  // horse(const horse& old_horse)
  ~horse() {
    --count;
  }
  // operator = (const horse& old_horse)
  static int get_count() {
    return count;
  }
};

int horse::count = 0;

class dog {
private:
  static int count;
public:
  explicit dog() {
    ++count;
  };
  // dog(const dog& old_dog)
  ~dog() {
    --count;
  }
  // operator = (const dog& old_dog)
  static int get_count() {
    return count;
  }
};

int dog::count = 0;

int get_count() {
  return pig::get_count() + horse::get_count() + dog::get_count();
}

int main()
{
  pig a_pig;
  horse a_horse0;
  horse a_horse1;
  dog a_dog0;
  dog a_dog1;
  dog a_dog2;

  std::cout << get_count() << '\n';

  return (0);
}

Makefile

#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample244_2

sample244_2: sample244_2.cpp
 ${CC} ${CFLAGS} -o sample244_2 sample244_2.cpp

clean:
 rm sample244_2

入出力結果(Terminal)

$ make && ./sample244_2
g++ -g -Wall -o sample244_2 sample244_2.cpp
6
$

0 コメント:

コメントを投稿