cs 225
play

CS 225 Data Structures Se Septembe ber 9 Ov Overloading G G - PowerPoint PPT Presentation

CS 225 Data Structures Se Septembe ber 9 Ov Overloading G G Carl Evans Des Destr tructor [Purpose]: Des Destr tructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom


  1. CS 225 Data Structures Se Septembe ber 9 – Ov Overloading G G Carl Evans

  2. Des Destr tructor [Purpose]:

  3. Des Destr tructor [Purpose]: Free any resources maintained by the class. Automatic Destructor: 1. Exists only when no custom destructor is defined. 2. [Invoked]: 3. [Functionality]:

  4. cs225/Cube.h cs225/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 … // ...

  5. MP 1 Art

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

  7. cs225/Cube.h cs225/Cube.cpp 1 #pragma once 40 2 41 3 namespace cs225 { 42 4 class Cube { 43 5 public: 44 6 Cube(); 45 7 Cube(double length); 46 8 Cube(const Cube & other); 47 9 ~Cube(); 48 10 49 11 50 12 51 13 52 14 53 15 double getVolume() const; 54 16 double getSurfaceArea() const; 55 17 56 18 private: 57 19 double length_; 58 20 }; 59 } 60 61

  8. On One Very y Special Op Operator Definition Syntax (.h): Cube & operator=(const Cube& s) Implementation Syntax (.cpp): Cube & Cube::operator=(const Cube& s)

  9. As Assignment O Operator Similar to Copy Constructor: Different from Copy Constructor:

  10. As Assignment O Operator Copies an object Destroys an object Copy constructor Copy Assignment operator Destructor

  11. MP: MP: Extr tra a Cred edit it The most successful MP is an MP done early! Unless otherwise specified in the MP, we will award up to 8 points of extra credit for completing part 1 by the extra credit deadline (the Monday following the release of the MP) Scaled by tests passed Example on MP 2 (19 tests) 19/19 = 8 points EC 18/19 = 7.58 points EC 17/19 = 7.16 points EC 16/19 = 6.74 points EC …

  12. The The “R “Rul ule e of f Thr Three” ee” 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.

  13. The The “R “Rul ule e of f Zer ero” Corollary to Rule of Five Classes that declare custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership. Other classes should not declare custom destructors, copy/move constructors or copy/move assignment operators –Scott Meyers

  14. In In CS 225

  15. Rv Rvalue Re Reference or Move Semantics • Rvalue • Move Cube(const Cube&& s) noexcept • Move Assignment Cube & operator=(const Cube&& s) noexcept

  16. The The “R “Rul ule e of f Fi Five” e” If it is necessary to define any one of these five functions in a class, it will be necessary to define all five of these functions: 1. 2. 3. 4. 5.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend