diff --git a/array.cc (Case Conflict 1) b/array.cc (Case Conflict 1) deleted file mode 100644 index 95db831..0000000 --- a/array.cc (Case Conflict 1) +++ /dev/null @@ -1,19 +0,0 @@ -class Array { - int *data; - int size; -public: - Array(int s) { - size = s; - data = new int[size]; - } - ~Array() { - delete[] data; - } - -}; - -int main(int argc, char **argv) { - Array a1(10); - Array a2 = a1; - return 0; -} diff --git a/ch22/IntegerMapping b/ch22/IntegerMapping new file mode 100755 index 0000000..97e2805 Binary files /dev/null and b/ch22/IntegerMapping differ diff --git a/ch22/IntegerMapping.cc b/ch22/IntegerMapping.cc index c9bd6e4..1d07ed7 100644 --- a/ch22/IntegerMapping.cc +++ b/ch22/IntegerMapping.cc @@ -27,7 +27,7 @@ class Negate : public IntegerMapping { class Compose : public IntegerMapping { public: - Compose(IntegerMapping &n1, IntegerMapping &n2) : m1(&n1), m2(&n2) {} + Compose(IntegerMapping *n1, IntegerMapping *n2) : m1(n1), m2(n2) {} int maps(int x) const { return m1->maps(m2->maps(x)); } @@ -47,3 +47,4 @@ int main(int argc, char **argv) { cout << weird(&c, 15, 16) << endl; return 0; } + diff --git a/ch23/reference_virtual.cc b/ch23/reference_virtual.cc index 244efb3..57155e6 100644 --- a/ch23/reference_virtual.cc +++ b/ch23/reference_virtual.cc @@ -22,7 +22,7 @@ class SecondClass : public FirstClass { void print1() { cout << "SecondClass::print1()" << endl; } - virtual void print2() { + void print2() { cout << "SecondClass::print2()" << endl; } }; @@ -32,7 +32,7 @@ class ThirdClass : public SecondClass { void print1() { cout << "ThirdClass::print1()" << endl; } - virtual void print2() { + void print2() { cout << "ThirdClass::print2()" << endl; } }; diff --git a/ch25/Internal_vector b/ch25/Internal_vector new file mode 100755 index 0000000..242e9b2 Binary files /dev/null and b/ch25/Internal_vector differ diff --git a/ch25/Internal_vector.cc b/ch25/Internal_vector.cc new file mode 100644 index 0000000..1575203 --- /dev/null +++ b/ch25/Internal_vector.cc @@ -0,0 +1,140 @@ +#include + +using namespace std; + +class InternalVector { + public: + double *data; + int size; + int nbref; + + InternalVector(double *d, int s) : data(new double[s]), + size(s), nbref(0) { + cout << " + Expensive allocation and copy" << endl; + for (int k = 0; k < s; k++) { + data[k] = d[k]; + } + } + + ~InternalVector() { + cout << " + Destruction" << endl; + delete[] data; + } + + void release() { + if (--nbref == 0) { + delete this; + } + } + + InternalVector *grab() { + nbref++; + return this; + } + + InternalVector *own() { + if (nbref == 1) { + return this; + } else { + nbref--; + InternalVector *result = new InternalVector(data, size); + result->nbref++; + return result; + } + } +}; + +class Vector { + public: + Vector(); + Vector(double *d, int s); + Vector(const Vector &v); + ~Vector(); + Vector & operator = (const Vector &v); + inline double get(int k) const; + inline void set(int k, double v); + inline int size() const; + private: + InternalVector *internal; +}; + + +Vector::Vector() { + cout << " * Creating empty Vector" << endl; + internal = 0; +} + +Vector::Vector(double *d, int s) { + cout << " * Creating Vector" << endl; + internal = new InternalVector(d, s); + internal->grab(); +} + +Vector::Vector(const Vector &v) { + cout << " * Coping Vector" << endl; + if (v.internal) { + internal = v.internal->grab(); + } else { + internal = 0; + } +} + +Vector::~Vector() { + cout << " * Destorying Vector" << endl; + if (internal) { + internal->release(); + } +} + +Vector & Vector::operator = (const Vector &v) { + cout << " * Assigning Vector from Vector" << endl; + if (this != &v) { + if (internal) { + internal->release(); + internal = v.internal->grab(); + } + } +} + +inline double Vector::get(int k) const { + return internal->data[k]; +} + +inline void Vector::set(int k, double v) { + if (v != internal->data[k]) { + internal = internal->own(); + internal->data[k] = v; + } +} + +inline int Vector::size() const { + return internal->size; +} + +double sum(Vector v) { + cout << "Entering sum()" << endl; + double s = 0; + for (int i = 0; i < v.size(); i++) { + s += v.get(i); + } + cout << "Leaving sum()" << endl; + return s; +} + +int main(int argc, char **argv) { + cout << "Doing double a[] = {1, 2, 3, 4, 5, 6, 7};" << endl; + double a[] = {1, 2, 3, 4, 5, 6, 7}; + cout << "DOING Vector v(a, sizeof(a)/sizeof(double));" << endl; + Vector v(a, sizeof(a)/sizeof(double)); + cout << "DING Vector w;" << endl; + Vector w; + cout << "DOING w = v;" << endl; + w = v; + cout << "DOING cout << sum(v) << \"\\n\";" << endl; + cout << sum(v) << endl; + cout << "DOING w.set(3, 2.1);" << endl; + w.set(3, 2.1); + cout << "FINISHED" << endl; + + return 0; +} diff --git a/ch26/e1 b/ch26/e1 new file mode 100755 index 0000000..b8f9467 Binary files /dev/null and b/ch26/e1 differ diff --git a/ch26/e1.cc b/ch26/e1.cc new file mode 100644 index 0000000..2394aa8 --- /dev/null +++ b/ch26/e1.cc @@ -0,0 +1,13 @@ +#include +#include + +using namespace std; + +int main(int argc, char **argv) { + string s = "What a beautiful weather!!!"; + string t; + t = s; + std::cout << t << std::endl; + + return 0; +} diff --git a/ch26/e2 b/ch26/e2 new file mode 100755 index 0000000..9ae71e4 Binary files /dev/null and b/ch26/e2 differ diff --git a/ch26/e2.cc b/ch26/e2.cc new file mode 100644 index 0000000..8248ab9 --- /dev/null +++ b/ch26/e2.cc @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +void something(string s) { + cout << "s = [" << s << "]" << endl; + s[0] = 'X'; + cout << "s = [" << s << "]" << endl; +} + +int main(int argc, char **argv) { + string s1 = "University"; + string s2 = " of "; + string s3(" Chicago"); + string s4; + s4 = s1 + s2; + s4 += s3; + cout << s4 << endl; + s1 = s4.substr(11, 2); + cout << s1 << endl; + something(s1); + cout << s1 << endl; + return 0; +} diff --git a/ch26/e3.cc b/ch26/e3.cc new file mode 100644 index 0000000..14e999d --- /dev/null +++ b/ch26/e3.cc @@ -0,0 +1,25 @@ +class IntIterator { + public: + virtual bool hasNext() = 0; + virtual int next() = 0; +}; + +class SmallIterator : public IntIterator { + public: + SmallIterator() : n(0) {} + SmallIterator(int aa) : a(aa), n(1) {} + SmallIterator(int bb, int aa) :a(aa), b(bb), n(2) {} + bool hasNext() { + return n > 0; + } + int next() { + n--; + if (n == 1) { + return b; + } else if (n == 0) { + return a; + } + } + private: + int a, b, n; +};