Accelerated C++ Chapter 09

どもです。
恒例の Accelerated C++ です。Chapter09 です。
いってみよ!

メモ

struct foo {
  int x;
  int get() const { return x; } // <-- メンバ関数
}
int foo::func() const {
  return ::func(); // <-- 名前空間のトップレベルで? 定義されている func。
}
    • デフォルトプロテクション (default protection)
      • ラベルがない場合は,メンバは,デフォルトで public。
        • クラスでは,private。
  • class (クラス)
    • デフォルトで private。
    • accessor function
    • コンストラク
    • デフォルトコンストラク
      • 引数なしのコンストラク
    • コンストラクタ初期化子リスト (constructor initializer list)
      • Foo::Foo() : m1_(0), m2_(0) {} とか。
    • オブジェクトの生成ステップ
      • 1. メモリ割り当て。
      • 2. コンストラクタ初期化子で初期化。
        • メンバに対して何もしない場合は,そのメンバはその型のデフォルトコンストラクタで初期化される。
      • 3. コンストラクタの中身を実行。

Exercises

全部やったけど,一部のみアップ。面倒臭いとかそういうことです。

Exercise 9-5
  • grader.hh
#ifndef GUARD_GRADER_H_
#define GUARD_GRADER_H_

#include "Student_info.hh"

class Grader {
public:
  Grader(double border_score) : border_score_(border_score) {}
  bool judge(const Student_info&) const;
  void report(std::ostream&, const std::vector<Student_info>&) const;
  void report_passed(std::ostream&, const std::vector<Student_info>&) const;
  void report_failed(std::ostream&, const std::vector<Student_info>&) const;
  bool is_passed(const Student_info&) const;
  bool is_failed(const Student_info&) const;
  void report_patitioned(std::ostream& os, const std::vector<Student_info>& students) const;

private:
  double border_score_;
};

#endif // GUARD_GRADER_H_
  • grader.cc
#include <vector>
#include <algorithm>
#include <iomanip>
#include <functional>
#include <iterator>

#include "Student_info.hh"
#include "Grader.hh"

using namespace std;

bool Grader::judge(const Student_info& s) const
{
  return s.grade() > 60;
}

// Report the final grades of all the students in STUDENTS, sorted by alphabetically.
void Grader::report(ostream& out, const vector<Student_info>& students) const
{
  vector<Student_info> s = students;
  sort(s.begin(), s.end(), compare);

  size_t maxlen = 0;
  vector<Student_info>::const_iterator iter;
  for (iter = s.begin(); iter != s.end(); ++iter) {
    maxlen = max(iter->name().size(), maxlen);
  }

  for (iter = s.begin(); iter != s.end(); ++iter) {
    double final_grade = iter->grade();
    out << iter->name() << string(maxlen - iter->name().size(), ' ') << "\t";
    streamsize prec = out.precision();
    out << setw(5) << setprecision(3) << final_grade << setprecision(prec) << "\t"
        << (final_grade > 60 ? "P" : "F") << endl;
  }
}

bool Grader::is_passed(const Student_info& s) const
{
  return s.grade() > border_score_;
}

void Grader::report_passed(ostream& os, const vector<Student_info>& students) const
{
  vector<Student_info> s_passed;
  for (vector<Student_info>::const_iterator iter = students.begin(); iter != students.end(); ++iter) {
    if (is_passed(*iter)) {
      s_passed.push_back(*iter);
    }
  }
  report(os, s_passed);
}

void Grader::report_failed(ostream& os, const vector<Student_info>& students) const
{
  vector<Student_info> s_failed;
  for (vector<Student_info>::const_iterator iter = students.begin(); iter != students.end(); ++iter) {
    if (!is_passed(*iter)) {
      s_failed.push_back(*iter);
    }
  }
  report(os, s_failed);
}

あんまり覚えてないけど,Exercise 9-6 の方もカバーしていた気がする。
テストを実行したら通ったので,大丈夫だと思う。
けど,すごい冗長。
思い出してきたぞ。その冗長さを取り除こうとしたけど,
std::remove_copy_if() とかに渡す pred がどうのこうの。
渡そうとしても grader がいないからどうのこうの。
ちょっと考えてみるか。も。