PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources...

18
CS 225 Data Structures

Transcript of PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources...

Page 1: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

CS 225 Data Structures

Page 2: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Destructor [Purpose]:

Page 3: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor is defined.

2. [Invoked]:

3. [Functionality]:

Page 4: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

#ifndef SPHERE_H

#define SPHERE_H

namespace cs225 {

class Sphere {

public:

Sphere();

Sphere(double r);

Sphere(const Sphere &s);

~Sphere();

// ...

private:

double r_;

};

}

#endif

sphere.h 1 2

3

4

5

6

7

8

9

10

11

12

13

14

26

27

28

29

30

31

32

#include "sphere.h“

namespace cs225 {

// ...

}

sphere.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Page 5: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Operators that can be overloaded in C++

Arithmetic + - * / % ++ --

Bitwise & | ^ ~ << >>

Assignment =

Comparison == != > < >= <=

Logical ! && ||

Other [] () ->

Page 6: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

#ifndef SPHERE_H

#define SPHERE_H

namespace cs225 {

class Sphere {

public:

Sphere();

Sphere(double r);

Sphere(const Sphere &s);

~Sphere();

// ...

private:

double r_;

};

}

#endif

sphere.h 1 2

3

4

5

6

7

8

9

10

11

12

13

14

26

27

28

29

30

31

32

#include "sphere.h“

namespace cs225 {

// ...

}

sphere.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Page 7: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

One Very Special Operator Definition Syntax (.h): Sphere & operator=(const Sphere& s) Implementation Syntax (.cpp): Sphere & Sphere::operator=(const Sphere& s)

Page 8: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Assignment Operator Similar to Copy Constructor:

Different from Copy Constructor:

Page 9: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Assignment Operator Copies an object Destroys an object

Copy constructor

Assignment operator

Destructor

Page 10: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

The “Rule of Three” If it is necessary to define any one of these three functions in a class, it will be necessary to define all three of these functions: 1. 2. 3.

Page 11: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Inheritance

Page 12: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

#ifndef PLANET_H_

#define PLANET_H_

#include “Sphere.h“

public Planet :

public cs225::Sphere {

}

#endif

Planet.h 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include “Planet.h"

Planet.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Page 13: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

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

int main() {

Planet p;

p.getRadius(); // Returns 1, the radius init’d

// by Sphere’s default ctor

...

}

5

6

7

8

Page 14: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

#ifndef PLANET_H_

#define PLANET_H_

#include “Sphere.h“

public Planet :

public cs225::Sphere {

public:

private:

}

#endif

Planet.h 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include “Planet.h"

Planet.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Page 15: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Virtual

Page 16: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

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;

Page 17: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

Runtime of Virtual Functions

Sphere obj; Planet obj;

Planet r;

Sphere &obj = r;

obj.print_1();

obj.print_2();

obj.print_3();

obj.print_4();

obj.print_5();

Page 18: PowerPoint Presentation - University Of Illinois...Destructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor

CS 225 – Things To Be Doing Theory Exam 1 is ongoing More Info: https://courses.engr.illinois.edu/cs225/sp2018/exams/ MP2 Released! Extra Credit Deadline: Monday, Feb. 5th – Up to +7 Extra Credit Due Date: Monday, Feb.12th lab_memory Due: Sunday, Feb. 4th POTD Every Monday-Friday – Worth +1 Extra Credit /problem (up to +40 total)