cs 225
play

CS 225 Data Structures Fe February 3 - Li Lifecycle G G Carl - PowerPoint PPT Presentation

CS 225 Data Structures Fe February 3 - Li Lifecycle G G Carl Evans Cop Copy Con Constru ructor or Cop Copy Con Constru ructor or Automatic Copy Constructor Custom Copy Constructor Cube.h Cube.cpp 1 #pragma once 7 namespace


  1. CS 225 Data Structures Fe February 3 - Li Lifecycle G G Carl Evans

  2. Cop Copy Con Constru ructor or

  3. Cop Copy Con Constru ructor or Automatic Copy Constructor Custom Copy Constructor

  4. Cube.h Cube.cpp 1 #pragma once 7 namespace cs225 { 2 8 Cube::Cube() { 3 namespace cs225 { 9 length_ = 1; 4 class Cube { 10 cout << "Default ctor" 5 public: << endl; 6 Cube(); 11 } 7 Cube(double length); 12 8 13 Cube::Cube(double length) { 9 14 length_ = length; 10 double getVolume() const; 15 cout << "1-arg ctor" 11 double getSurfaceArea() const; << endl; 12 16 } 13 private: 17 14 double length_; 18 15 }; 19 16 } 20 17 21 18 22 19 23 20 24 25 … // ...

  5. joinCubes-byValue.cpp 11 /* 12 * Creates a new Cube that contains the exact volume 13 * of the volume of the two input Cubes. 14 */ 15 Cube joinCubes(Cube c1, Cube c2) { 16 double totalVolume = c1.getVolume() + c2.getVolume(); 17 18 double newLength = std::pow( totalVolume, 1.0/3.0 ); 19 20 Cube result(newLength); 21 return result; 22 } 23 28 int main() { 24 29 Cube *c1 = new Cube(4); 25 30 Cube *c2 = new Cube(5); 26 31 32 Cube c3 = joinCubes(*c1, *c2); 33 34 return 0; 35 }

  6. Ca Calls t to c o con onstru ructor ors By Value By Pointer By Reference void foo(Cube a) { … } void foo(Cube *a) { … } void foo(Cube &a) { … } Cube::Cube() Cube::Cube(double) Cube::Cube(const Cube&)

  7. joinCubes-byPointer.cpp 11 /* 12 * Creates a new Cube that contains the exact volume 13 * of the volume of the two input Cubes. 14 */ 15 Cube joinCubes(Cube * c1, Cube * c2) { 16 double totalVolume = c1->getVolume() + c2->getVolume(); 17 18 double newLength = std::pow( totalVolume, 1.0/3.0 ); 19 20 Cube result(newLength); 21 return result; 22 } 23 28 int main() { 24 29 Cube *c1 = new Cube(4); 25 30 Cube *c2 = new Cube(5); 26 31 32 Cube c3 = joinCubes(c1, c2); 33 34 return 0; 35 }

  8. joinCubes-byRef.cpp 11 /* 12 * Creates a new Cube that contains the exact volume 13 * of the volume of the two input Cubes. 14 */ 15 Cube joinCubes(Cube & c1, Cube & c2) { 16 double totalVolume = c1.getVolume() + c2.getVolume(); 17 18 double newLength = std::pow( totalVolume, 1.0/3.0 ); 19 20 Cube result(newLength); 21 return result; 22 } 23 28 int main() { 24 29 Cube *c1 = new Cube(4); 25 30 Cube *c2 = new Cube(5); 26 31 32 Cube c3 = joinCubes(*c1, *c2); 33 34 return 0; 35 }

  9. Tower.h 1 #pragma once 2 3 #include "cs225/Cube.h" 4 using cs225::Cube; 5 6 class Tower { 7 public: 8 Tower(Cube c, Cube *ptr, const Cube &ref); 9 Tower(const Tower & other); 10 11 private: 12 Cube cube_; 13 Cube *ptr_; 14 const Cube &ref; 15 }; 16 17

  10. Tower.cpp 10 Tower::Tower(const Tower & other) { 11 cube_ = other.cube_; 12 ptr_ = other.ptr_; 13 ref_ = other.ref_; 14 }

  11. Tower.cpp 10 Tower::Tower(const Tower & other) { 11 cube_ = other.cube_; 12 ptr_ = other.ptr_; 13 ref_ = other.ref_; 14 }

  12. Tower.cpp 10 Tower::Tower(const Tower & other) { 11 cube_ = other.cube_; 12 ptr_ = other.ptr_; 13 ref_ = other.ref_; 14 } Tower.cpp 10 Tower::Tower(const Tower & other) : cube_(other.cube_), 11 ptr_(other.ptr_), ref_(other.ref_) { } 12 13 Constructor Initializer List 14

  13. Tower.cpp Tower::Tower(const Tower & other) { // Deep copy cube_: // Deep copy ptr_: // Deep copy ref_: }

  14. Des Destr tructor

  15. Cube.h Cube.cpp 1 #pragma once 7 namespace cs225 { 2 8 Cube::Cube() { 3 namespace cs225 { 9 length_ = 1; 4 class Cube { 10 cout << "Default ctor" 5 public: << endl; 6 Cube(); 11 } 7 Cube(double length); 12 8 Cube(const Cube & other); 13 Cube::Cube(double length) { 9 ~Cube(); 14 length_ = length; 10 15 cout << "1-arg ctor" 11 double getVolume() const; << endl; 12 double getSurfaceArea() const; 16 } 13 17 14 private: 18 15 double length_; 19 16 }; 20 17 } 21 18 22 19 23 20 24 25 … // ...

  16. Operators that can be overloaded in C++ + - * / % ++ -- Arithmetic Bitwise & | ^ ~ << >> = Assignment == != > < >= <= Comparison ! && || Logical [] () -> Other

  17. Cube.h Cube.cpp 1 #pragma once 7 namespace cs225 { 2 8 Cube::~Cube() { 3 namespace cs225 { 9 cout << "dtor called"; 4 class Cube { << endl; 5 public: 10 } 6 Cube(); 11 7 Cube(double length); 12 8 Cube(const Cube & other); 13 9 ~Cube(); 14 10 15 11 16 12 17 13 18 14 19 15 double getVolume() const; 20 16 double getSurfaceArea() const; 21 17 22 18 private: 23 19 double length_; 24 20 }; 25 } … // ...

Recommend


More recommend