PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The...
Transcript of PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The...
![Page 1: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/1.jpg)
CS 225Data Structures
September 12 - InheritanceWade Fagen-Ulmschneider
![Page 2: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/2.jpg)
Destructor[Purpose]:
![Page 3: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/3.jpg)
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 · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/4.jpg)
#pragma once
namespace cs225 {
class Cube {
public:
Cube();
Cube(double length);
Cube(const Cube & other);
~Cube();
double getVolume() const;
double getSurfaceArea() const;
private:
double length_;
};
}
cs225/Cube.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
namespace cs225 {
Cube::Cube() {
length_ = 1;
cout << "Default ctor"
<< endl;
}
Cube::Cube(double length) {
length_ = length;
cout << "1-arg ctor"
<< endl;
}
// ...
cs225/Cube.cpp7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
…
![Page 5: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/5.jpg)
Operators that can be overloaded in C++
Arithmetic + - * / % ++ --
Bitwise & | ^ ~ << >>
Assignment =
Comparison == != > < >= <=
Logical ! && ||
Other [] () ->
![Page 6: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/6.jpg)
#pragma once
namespace cs225 {
class Cube {
public:
Cube();
Cube(double length);
Cube(const Cube & other);
~Cube();
double getVolume() const;
double getSurfaceArea() const;
private:
double length_;
};
}
cs225/Cube.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cs225/Cube.cpp40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
![Page 7: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/7.jpg)
One Very Special OperatorDefinition Syntax (.h):Cube & operator=(const Cube& s)
Implementation Syntax (.cpp):Cube & Cube::operator=(const Cube& s)
![Page 8: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/8.jpg)
Assignment OperatorSimilar to Copy Constructor:
Different from Copy Constructor:
![Page 9: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/9.jpg)
Assignment OperatorCopies an object Destroys an object
Copy constructor
Assignment operator
Destructor
![Page 10: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/10.jpg)
MP: Extra CreditThe 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, Sept. 17 (11:59pm)+6 points: Complete by Tuesday, Sept. 18 (11:59pm)+5 points: Complete by Wednesday, Sept. 19 (11:59pm)+4 points: Complete by Thursday, Sept. 20 (11:59pm)+3 points: Complete by Friday, Sept. 21 (11:59pm)+2 points: Complete by Saturday, Sept. 22 (11:59pm)+1 points: Complete by Sunday, Sept. 23 (11:59pm)MP2 Due Date: Monday, Sept. 24
![Page 11: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/11.jpg)
MP: Extra CreditWe 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
![Page 12: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/12.jpg)
MP: Extra CreditWe 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)
![Page 13: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/13.jpg)
MP: Extra CreditWe 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 compile error:Wednesday: +5 * 0% = +0 earned (okay to score lower later)
…
![Page 14: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/14.jpg)
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 15: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/15.jpg)
Inheritance
![Page 16: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/16.jpg)
class Shape {
public:
Shape();
Shape(double length);
double getLength() const;
private:
double length_;
};
Shape.h4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Shape::Shape() {
length_ = 1;
}
Shape::Shape(double length) {
length_ = length;
}
double Shape::getLength()
const {
return length_;
}
Shape.cpp8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
…
![Page 17: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/17.jpg)
#pragma once
#include "Shape.h"
class Square {
public:
double getArea() const;
private:
// Nothing!
};
Square.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Square.cpp8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
…
![Page 18: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/18.jpg)
Derived Classes[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
![Page 19: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/19.jpg)
#pragma once
#include "Shape.h"
class Square {
public:
double getArea() const;
private:
// Nothing!
};
Square.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Square.cpp8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
…
![Page 20: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/20.jpg)
class Cube {
public:
double getVolume() const;
double getSurfaceArea() const;
private:
// Nothing!
};
Cube.h4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Cube.cpp8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
…
![Page 21: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/21.jpg)
#pragma once
class RubikCube : public Cube {
public:
void solve();
void turnRow(int r);
void turnColumn(int c);
void rotate(int direction);
private:
// ...
};
RubikCube.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "RubikCube.h"
RubikCube.cpp12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
![Page 22: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/22.jpg)
Virtual
![Page 23: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/23.jpg)
Cube.cpp12
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.cpp12
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 Cube::print_5() = 0;
![Page 24: PowerPoint Presentation · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the MP, we will](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dd49b06529c50a64a9997/html5/thumbnails/24.jpg)
Runtime 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();