cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra...

25
CS 225 Data Structures September 11 - Inheritance G Carl Evans

Transcript of cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra...

Page 1: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

CS 225Data Structures

September 11 - InheritanceG Carl Evans

Page 2: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Destructor[Purpose]:

Page 3: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

#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.h123456789

1011121314151617181920

namespace cs225 {Cube::Cube() {

length_ = 1;cout << "Default ctor"

<< endl;}

Cube::Cube(double length) {length_ = length;cout << "1-arg ctor"

<< endl;}

// ...

cs225/Cube.cpp789

10

1112131415

16171819202122232425…

Page 5: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

MP 1 Art

Page 6: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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

Page 7: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

#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.h123456789

1011121314151617181920

cs225/Cube.cpp40414243444546474849505152535455565758596061

Page 8: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

One Very Special OperatorDefinition Syntax (.h):Cube & operator=(const Cube& s)

Implementation Syntax (.cpp):Cube & Cube::operator=(const Cube& s)

Page 9: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Assignment OperatorSimilar to Copy Constructor:

Different from Copy Constructor:

Page 10: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Assignment OperatorCopies an object Destroys an object

Copy constructor

Assignment operator

Destructor

Page 11: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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. 16 (11:59pm)+6 points: Complete by Tuesday, Sept. 17 (11:59pm)+5 points: Complete by Wednesday, Sept. 18 (11:59pm)+4 points: Complete by Thursday, Sept. 19 (11:59pm)+3 points: Complete by Friday, Sept. 20 (11:59pm)+2 points: Complete by Saturday, Sept. 21 (11:59pm)+1 points: Complete by Sunday, Sept. 22 (11:59pm)MP2 Due Date: Monday, Sept. 23

Page 12: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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 13: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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 14: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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 15: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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 16: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Inheritance

Page 17: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

class Shape {public:

Shape();Shape(double length);double getLength() const;

private:double length_;

};

Shape.h456789

1011121314151617181920

Shape::Shape() {length_ = 1;

}

Shape::Shape(double length) {length_ = length;

}

double Shape::getLength() const {

return length_;}

Shape.cpp89

10111213141516171819202122232425262728…

Page 18: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

#pragma once

#include "Shape.h"

class Square {public:

double getArea() const;

private:// Nothing!

};

Square.h123456789

1011121314151617181920

Square.cpp89

10111213141516171819202122232425262728…

Page 19: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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...

}

5678……

main.cpp

Page 20: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

#pragma once

#include "Shape.h"

class Square {public:

double getArea() const;

private:// Nothing!

};

Square.h123456789

1011121314151617181920

Square.cpp89

10111213141516171819202122232425262728…

Page 21: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

class Cube {public:

double getVolume() const;double getSurfaceArea() const;

private:// Nothing!

};

Cube.h456789

1011121314151617181920

Cube.cpp89

10111213141516171819202122232425262728…

Page 22: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

#pragma once

class RubikCube : public Cube {public:

void solve();

void turnRow(int r);void turnColumn(int c);void rotate(int direction);

private:// ...

};

RubikCube.h123456789

10111213141516171819202122

#include "RubikCube.h"RubikCube.cpp

123456789

10111213141516171819202122

Page 23: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Virtual

Page 24: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

Cube.cpp123456789

10111213141516171819202122

// 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.cpp123456789

10111213141516171819202122

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 25: cs225fa19-07-inheritance-slides · Copy constructor Assignment operator Destructor. MP: Extra Credit The most successful MP is an MP done early! Unless otherwise specified in the

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