//001 IntCell类的完整声明 class IntCell { private: int storedValue; public: IntCell(){storedValue = 0;} IntCell(int initialValue){storedValue = initialValue;} int read(){return storedValue;} void write(int x){storedValue = x;} };
//002 改进的IntCell类 class IntCell { private: int storedValue; public: explicit IntCell(int initialValue = 0):storedValue(initialValue){} int read() const {return storedValue;} int write(int x){storedValue = x;} };
//003 接口与实现的分离 three files //IntCell.h #ifndef IntCell_H #define IntCell_H class IntCell { private: int storedValue; public: explicit IntCell(int initialValue = 0); int Cellread() const; void write(int x); }; #endif //IntCell.cpp #include "IntCell.h" IntCell::IntCell(int initialValue):storedValue(initialValue){} int IntCell::read() const {return storedValue;} void IntCell:: write(int x){storedValue = x;} //TestIntCell.cpp #include <iostream> #include "IntCell.h" using namespace std; int main() { IntCell m; m.write(5); cout << "Cell contents: " << m.read() << endl; return 0; }
//004 vector类的例子 #include <iostream> #include <vector> using namespace std; int main() { vector<int> squares(100); for (int i = 0; i < squares.size(); i++) squares[i] = i*i; for (int i = 0; i < squares.size(); i++) cout << i << " " << squares[i] << endl; return 0; }
//005 003中IntCell类的指针用法 int main() { IntCell *m; m = new IntCell; m->write(5); cout << "Cell Contents: " << m->read() << endl; delete m; return 0; }
//006 数据成员是指针的IntCell类实现 //IntCell.h #ifndef IntCell_H #define IntCell_H class IntCell { private: int * storedValue; public: explicit IntCell(int initialValue = 0); IntCell(const IntCell & rhs); ~IntCell(); const IntCell& operator=(const IntCell & rhs); int read() const; void write(); }; #endif //IntCell.cpp #include "IntCell.h" IntCell::IntCell(int initialValue = 0) { storedValue = new int(initialValue); } IntCell::IntCell(const IntCell& rhs) { storedValue = new int(*rhs.storedValue); } IntCell::~IntCell() { delete storedValue; } const IntCell& IntCell::operator=(const IntCell& rhs) { if (this != &rhs) *storedValue = *rhs.storedValue; return *this; } int IntCell::read() const { return *storedValue; } void IntCell::write(int x) { *storedValue = x; } //IntCellTest.cpp #include <iostream> #include "IntCell.h" int main() { IntCell a(2); IntCell b = a; IntCell c; c = b; a.write(4); cout << a.read() << endl << b.read() << endl << c.read() << endl; return 0; }
//007 未分离的MemoryCell类模板 template <typename Object> class MemoryCell { private: Object storedValue; public: explicit MemoryCell(const Object& initialValue = Object()):storedValue(initialValue){} const Object& read() const {return storedValue;} void write(const Object& x) {storedValue = x;} };
//008 使用MemoryCell类模板的程序 int main() { MemoryCell<int> m1; MemoryCell<string> m2("hello"); m1.write(37); m2.write(m2.read() + " world"); cout << m1.read() << endl << m2.read() << endl; return 0; }
//009 泛型函数findMax的例子 class Employee { private: string name; double salary; public: void setValue(const string& n, double s){name = n; salary = s;} const string& getName() const {return name;} void print(ostream& out) const {out << name << " (" << salary << ")" ;} bool operator<(const Employee& rhs) const {return salary < rhs.salary;} }; ostream& operator<<(ostream& out, const Employee& rhs) { print(out); return out; } int main() { vector<Employee> v(3); v[0].setValue("George Bush", 400000.00); v[1].setValue("Bill Gates", 2000000000.00); v[2].setValue("Dr. Phil", 13000000.00); cout << findMax(v) << endl; return 0; }
//010 findMax函数模板 /** * Return the maximum item in array a. * Assumes a.size() > 0. * Comparable objects must provide operator< and operator= */ template <typename Comparable> const Comparable& findMax(const vector<Comparable> & a) { int maxIndex = 0; for (int i = 1; i < a.size(); i++) if (a[maxIndex] < a[i]) maxIndex = i; return a[maxIndex]; }
//011 函数对象作为findMax的第二个参数 template <typename Object, typename Comparator> const Object& findMax(const vector<Object>& arr, Comparator cmp) { int maxIndex = 0; for (int i = 1; i < arr.size(); i++) if (cmp.isLessThan(arr[maxIndex]), arr[i]) maxIndex = i; return arr[maxIndex]; } class CaseInsensitiveCompare { public: bool isLessThan(const string& lhs, const string& rhs) const { return stricmp(lhs.c_str(), rhs.c_str()) < 0; } }; int main() { vector<string> arr[3]; arr[0] = "ZEBAR"; arr[1] = "alligator"; arr[2] = "crocodile"; cout << findMax(arr, CaseInsensitiveCompare()) << endl; return 0; }
//012 第二个版本findMax template <typename Object, typename Comparator> const Object& findMax(const vectror<Object>& arr, Comparator isLessThan) { int maxIndex = 0; for (int i = 1; i < arr.size(); i++) if (isLessThan(arr[maxIndex], arr[i])) maxIndex = i; return arr[maxIndex]; } #include <functional> template <typename Object> const Object& findMax(const vector<Object>& arr) { findMax(arr, less<Object>()); } class CaseInsensitiveCompare { public: bool operator()(const Object& lhs, const Object& rhs) const { return stricmp(lhs.c_str(), rhs.c_str()) < 0; } } int main() { vector<string> arr[3]; arr[0] = "ZEBRA"; arr[1] = "alligator"; arr[2] = "crocodile"; cout << findMax(arr, CaseInsensitiveCompare()) << endl; cout << findMax(arr) << endl; return 0; }
//013 矩阵 #ifndef MATRIX_H #define MATRIX_H #include <vector> using namespace std; template <typename Object> class matrix { private: vector <vector<Object>> array; public: matrix(int rows, int cols):array(rows) { for (int i = 0; i < array.size(); i++) array[i].resize(cols); } const vector<Object>& operator[](int row) const { return array[row]; } vector<Object>& operator[](int row) { return array[row]; } int numrows() const { return array.size(); } int numcols() const { return numrows() ? array[0].size() : 0; } };