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

feb 2 tem emplates
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Data Structures

  • Feb. 2 – Tem

emplates

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

Polymorphism

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

slide-3
SLIDE 3

Sphere.cpp

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

Planet.cpp

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

slide-4
SLIDE 4

Runtime of Virtual Functions

Sphere obj; Planet obj; Planet r; Sphere &obj = r;

  • bj.print_1();
  • bj.print_2();
  • bj.print_3();
  • bj.print_4();
  • bj.print_5();
slide-5
SLIDE 5

Why Polymorphism?

slide-6
SLIDE 6

MP1 Artwork

slide-7
SLIDE 7

MP1 Artwork

slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

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

slide-14
SLIDE 14

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) …

slide-15
SLIDE 15

class Animal { public: void speak() { } }; class Dog : public Sphere { public: void speak() { } }; class Cat : public Sphere { public: void speak() { } };

animalShelter.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14

slide-16
SLIDE 16

Abstract Class:

[Requirement]: [Syntax]: [As a result]:

slide-17
SLIDE 17

class Sphere { public: ~Sphere(); }; class Planet : public Sphere { public: ~Sphere(); };

virtual-dtor.cpp

15 16 17 18 19 20 21 22 23

slide-18
SLIDE 18

Assignment Operator

slide-19
SLIDE 19

class Sphere { public: Sphere(); Sphere(double r); Sphere(const Sphere & other); ~Sphere(); Sphere & operator=(Sphere & other); double getRadius() const; double getVolume() const; std::string[] getProps() const; void addProp(std::string prop); private: double r_; std::string * props_; unsigned props_max_, props_ct_; void _destroy(); void _copy(Sphere & other); };

#include "Sphere.h" int main() { cs225::Sphere s(10); s = s; return 0; }

assignmentOpSelf.cpp

1 2 3 4 5 6 7

sphere.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

slide-20
SLIDE 20

void Sphere::_destroy() { delete[] props_; } void Sphere::_copy(const Sphere &other) { r_ = other.r; props_max_ = other.props_max_; props_ct_ = other.props_ct_; props_ = new std::string[10]; for (unsigned i = 0; i < props_ct_; i++) { props_[i] = other.props_[i]; } } Sphere& Sphere::operator=(const Sphere &other) { _destroy(); _copy(other); return *this; }

#include "Sphere.h" int main() { cs225::Sphere s(10); s = s; return 0; }

assignmentOpSelf.cpp

1 2 3 4 5 6 7

sphere.cpp

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

slide-21
SLIDE 21

class PNG { public: PNG(); PNG(unsigned int width, unsigned int height); PNG(PNG const & other); ~PNG(); PNG & operator= (PNG const & other); bool operator== (PNG const & other) const; bool readFromFile(string const & fileName); bool writeToFile(string const & fileName); HSLAPixel & getPixel(unsigned int x, unsigned int y) const; unsigned int width() const; // ... private: unsigned int width_; unsigned int height_; HSLAPixel *imageData_; void _copy(PNG const & other); };

cs225/png.h

18 19 23 30 37 43 50 57 73 80 90 96 118 119 120 121 127 132

slide-22
SLIDE 22

Abstract Data Type

slide-23
SLIDE 23

List ADT

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Templates

slide-26
SLIDE 26

T maximum(T a, T b) { T result; result = (a > b) ? a : b; return result; }

template1.cpp

1 2 3 4 5 6 7

slide-27
SLIDE 27

#ifndef LIST_H_ #define LIST_H_ class List { public: private: }; #endif

List.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

List.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22