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

24
CS 225 Data Structures January 30 - Inheritance Wade Fagen-Ulmschneider, Craig Zilles

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

Page 1: cs225sp19-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,

CS 225Data Structures

January 30 - InheritanceWade Fagen-Ulmschneider, Craig Zilles

Page 2: cs225sp19-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,

Destructor[Purpose]:

Page 3: cs225sp19-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,

Destructor[Purpose]: Free any resources maintained by the class.

Automatic Destructor:1. Exists only when no custom destructor is defined.

2. [Functionality]:

[Invoked]:

Page 4: cs225sp19-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,

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

namespace cs225 {Cube::Cube() {length_ = 1;cout << "Default ctor"

<< endl;}

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

<< endl;}

// ...

cs225/Cube.cpp78910

1112131415

16171819202122232425…

Page 5: cs225sp19-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,

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

Page 6: cs225sp19-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,

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

cs225/Cube.cpp40414243444546474849505152535455565758596061

Page 7: cs225sp19-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,

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

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

Page 8: cs225sp19-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,

Assignment OperatorSimilar to Copy Constructor:

Different from Copy Constructor:

Page 9: cs225sp19-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,

Assignment OperatorCopies an object Destroys an object

Copy constructor

Assignment operator

Destructor

Page 10: cs225sp19-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,

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, Feb. 4 (11:59pm)+6 points: Complete by Tuesday, Feb. 5 (11:59pm)+5 points: Complete by Wednesday, Feb. 6 (11:59pm)+4 points: Complete by Thursday, Feb. 7 (11:59pm)+3 points: Complete by Friday, Feb. 8 (11:59pm)+2 points: Complete by Saturday, Feb. 9 (11:59pm)+1 points: Complete by Sunday, Feb. 10 (11:59pm)MP2 Due Date: Monday, Feb. 11

Page 11: cs225sp19-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,

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: cs225sp19-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,

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: cs225sp19-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,

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: cs225sp19-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,

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: cs225sp19-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,

Inheritance

Page 16: cs225sp19-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,

class Shape {public:Shape();Shape(double length);double getLength() const;

private:double length_;

};

Shape.h4567891011121314151617181920

Shape::Shape() {length_ = 1;

}

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

}

double Shape::getLength() const {return length_;

}

Shape.cpp8910111213141516171819202122232425262728…

Page 17: cs225sp19-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,

#pragma once

#include "Shape.h"

class Square {public:double getArea() const;

private:// Nothing!

};

Square.h1234567891011121314151617181920

Square.cpp8910111213141516171819202122232425262728…

Page 18: cs225sp19-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,

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 19: cs225sp19-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,

#pragma once

#include "Shape.h"

class Square {public:

double getArea() const;

private:// Nothing!

};

Square.h1234567891011121314151617181920

Square.cpp8910111213141516171819202122232425262728…

Page 20: cs225sp19-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,

class Cube {public:double getVolume() const;double getSurfaceArea() const;

private:// Nothing!

};

Cube.h4567891011121314151617181920

Cube.cpp8910111213141516171819202122232425262728…

Page 21: cs225sp19-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,

#pragma once

class RubikCube : public Cube {public:void solve();

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

private:// ...

};

RubikCube.h12345678910111213141516171819202122

#include "RubikCube.h"RubikCube.cpp

12345678910111213141516171819202122

Page 22: cs225sp19-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,

Virtual

Page 23: cs225sp19-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,

Cube.cpp12345678910111213141516171819202122

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

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: cs225sp19-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,

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