feb 2 tem emplates
play

Feb. 2 Tem emplates Wad ade Fag agen-Ulm lmschneid ider - PowerPoint PPT Presentation

Data Structures Feb. 2 Tem emplates Wad ade Fag agen-Ulm lmschneid ider Polymorphism Object-Orientated Programming (OOP) concept that a single object may take on the type of any of its base types. Sphere.cpp Planet.cpp


  1. Data Structures Feb. 2 – Tem emplates Wad ade Fag agen-Ulm lmschneid ider

  2. Polymorphism Object-Orientated Programming (OOP) concept that a single object may take on the type of any of its base types.

  3. Sphere.cpp Planet.cpp Sphere::print_1() { // No print_1() in RedBall.cpp 1 1 2 cout << "Sphere" << endl; 2 3 } 3 4 4 5 Sphere::print_2() { 5 Planet::print_2() { 6 cout << "Sphere" << endl; 6 cout << “Earth" << endl; 7 } 7 } 8 8 9 virtual Sphere::print_3() { 9 // No print_3() in RedBall.cpp 10 cout << "Sphere" << endl; 10 11 } 11 12 12 13 virtual Sphere::print_4() { 13 Planet::print_4() { 14 cout << "Sphere" << endl; 14 cout << “Earth" << endl; 15 } 15 } 16 16 17 // In .h file: 17 Planet::print_5() { 18 virtual Sphere::print_5() = 0; 18 cout << “Earth" << endl; 19 19 } 20 20 21 21 22 22

  4. Runtime of Virtual Functions Planet r; Sphere obj; Planet obj; Sphere &obj = r; obj.print_1(); obj.print_2(); obj.print_3(); obj.print_4(); obj.print_5();

  5. Why Polymorphism?

  6. MP1 Artwork

  7. MP1 Artwork

  8. MP: Ext xtra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will award +1 extra credit point per day for completing Part 1 before the due date (up to +7 points) : Example for MP2: +7 points: Complete by Monday, Feb. 5 (11:59pm) +6 points: Complete by Tuesday, Feb. 6 (11:59pm) +5 points: Complete by Wednesday, Feb. 7 (11:59pm) +4 points: Complete by Thursday, Feb. 8 (11:59pm) +3 points: Complete by Friday, Feb. 9 (11:59pm) +2 points: Complete by Saturday, Feb. 10 (11:59pm) +1 points: Complete by Sunday, Feb. 11 (11:59pm) MP2 Due Date: Monday, Feb 12

  9. MP: Ext xtra Credit The most successful MP is an MP done early! We will give partial credit and maximize the value of your extra credit: You made a submission and missed a few edge cases in Part 1: Monday: +7 * 80% = +5.6 earned You fixed your code and got a perfect score on Part 1: Tuesday: +6 * 100% = +6 earned (maximum benefit) You began working on Part 2, but added a seg fault to Part 1: Wednesday: +5 * 0% = +0 earned (okay to score lower later) …

  10. animalShelter.cpp 1 class Animal { 2 public: 3 void speak() { } 4 }; 5 6 class Dog : public Sphere { 7 public: 8 void speak() { } 9 }; 10 11 class Cat : public Sphere { 12 public: 13 void speak() { } 14 };

  11. Abstract Class: [Requirement]: [Syntax]: [As a result]:

  12. virtual-dtor.cpp 15 class Sphere { 16 public: 17 ~Sphere(); 18 }; 19 20 class Planet : public Sphere { 21 public: 22 ~Sphere(); 23 };

  13. Assignment Operator

  14. sphere.h class Sphere { 1 2 public: 3 Sphere(); 4 Sphere(double r); 5 Sphere(const Sphere & other); 6 ~Sphere(); 7 8 Sphere & operator=(Sphere & other); 9 10 double getRadius() const; 11 double getVolume() const; 12 13 std::string[] getProps() const; assignmentOpSelf.cpp 14 void addProp(std::string prop); 15 1 #include "Sphere.h" 16 private: 2 17 double r_; 3 int main() { 18 std::string * props_; 4 cs225::Sphere s(10); 19 unsigned props_max_, props_ct_; 5 s = s; 20 void _destroy(); 6 return 0; 21 void _copy(Sphere & other); 7 } 22 };

  15. sphere.cpp void Sphere::_destroy() { delete[] props_; } 10 11 12 void Sphere::_copy(const Sphere &other) { 13 r_ = other.r; 14 props_max_ = other.props_max_; 15 props_ct_ = other.props_ct_; 16 props_ = new std::string[10]; 17 for (unsigned i = 0; i < props_ct_; i++) { 18 props_[i] = other.props_[i]; 19 } 20 } 21 22 Sphere& Sphere::operator=(const Sphere &other) { assignmentOpSelf.cpp 23 24 1 #include "Sphere.h" 25 2 26 _destroy(); 3 int main() { 27 _copy(other); 4 cs225::Sphere s(10); 28 return *this; 5 s = s; 29 } 6 return 0; 7 }

  16. cs225/png.h 18 class PNG { 19 public: 23 PNG(); 30 PNG(unsigned int width, unsigned int height); 37 PNG(PNG const & other); 43 ~PNG(); 50 PNG & operator= (PNG const & other); 57 bool operator== (PNG const & other) const; 73 bool readFromFile(string const & fileName); 80 bool writeToFile(string const & fileName); 90 HSLAPixel & getPixel(unsigned int x, unsigned int y) const; 96 unsigned int width() const; // ... 118 private: 119 unsigned int width_; 120 unsigned int height_; 121 HSLAPixel *imageData_; 127 void _copy(PNG const & other); 132 };

  17. Abstract Data Type

  18. List ADT

  19. What types of “stuff” do we want in our list?

  20. Templates

  21. template1.cpp 1 2 3 T maximum(T a, T b) { 4 T result; 5 result = (a > b) ? a : b; 6 return result; 7 }

  22. List.h List.cpp #ifndef LIST_H_ 1 1 2 #define LIST_H_ 2 3 3 4 4 5 5 6 class List { 6 7 public: 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 private: 16 17 17 18 18 19 19 20 }; 20 21 21 22 #endif 22

Recommend


More recommend