Fe February 1 Te Templates Wa Wade Fa Fagen-Ul Ulmsch schnei - - PowerPoint PPT Presentation

fe february 1 te templates
SMART_READER_LITE
LIVE PREVIEW

Fe February 1 Te Templates Wa Wade Fa Fagen-Ul Ulmsch schnei - - PowerPoint PPT Presentation

Data Structures Fe February 1 Te Templates Wa Wade Fa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles Example: e: assignmentOpSelf.cpp 1 #include "Cube.h" 40 Cube& Cube::operator=(const Cube


slide-1
SLIDE 1

Data Structures

Fe February 1 – Te Templates

Wa Wade Fa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles

slide-2
SLIDE 2

Example: e:

#include "Cube.h" Cube& Cube::operator=(const Cube &other) { _destroy(); _copy(other); return *this; } 1 … 40 41 42 43 44 45 46

assignmentOpSelf.cpp

slide-3
SLIDE 3

Example: e:

#include "Cube.h" int main() { cs225::Cube c(10); c = c; return 0; } 1 2 3 4 5 6 7

assignmentOpSelf.cpp

slide-4
SLIDE 4

#pragma once #include "Shape.h" class Square : public Shape { public: double getArea() const; private: // Nothing! };

Square.h

1 2 3 4 5 6 7 8 9 10 11

Square.cpp

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 … class Shape { public: Shape(); Shape(double length); double getLength() const; private: double length_; }; 4 5 6 7 8 9 10 11 12

Shape.h

slide-5
SLIDE 5

Der erived ed Classes es

[Public Members of the Base Class]: [Private Members of the Base Class]:

int main() { Square sq; sq.getLength(); // Returns 1, the length init’d // by Shape’s default ctor ... } 5 6 7 8 … …

main.cpp

slide-6
SLIDE 6

Po Polymorphism

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

slide-7
SLIDE 7

Vi Virtua ual

slide-8
SLIDE 8

Cube.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 RubikCube.cpp RubikCube::print_2() { cout << "Rubik" << endl; } // No print_3() in RubikCube.cpp RubikCube::print_4() { cout << "Rubik" << endl; } RubikCube::print_5() { cout << "Rubik" << endl; }

RubikCube.cpp

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

slide-9
SLIDE 9

Runtime e of Virtual Functions

virtual-main.cpp Cube c; RubikCube c; RubikCube rc; Cube &c = rc; c.print_1(); c.print_2(); c.print_3(); c.print_4(); c.print_5();

slide-10
SLIDE 10

Wh Why y Polym ymorphi phism sm?

slide-11
SLIDE 11

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

animalShelter.cpp

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

slide-12
SLIDE 12

MP1 P1 Artw twork

slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19

Abstr trac act t Clas lass:

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

slide-20
SLIDE 20

class Cube { public: ~Cube(); }; class RubikCube : public Cube { public: ~RubikCube(); };

virtual-dtor.cpp

15 16 17 18 19 20 21 22 23

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); };

MP2: 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

Abstr trac act t Da Data a Type

slide-23
SLIDE 23

Li List A ADT

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Tem emplates es

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

#pragma once 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