lec 11: OOP (or oops)

administrivia

ex10 due Wednesday

ex11 due next Monday (not this Friday)

midterm review Thursday

midterm exam Friday 11:30–12:20, here

feedback

paper readings: too difficult? too boring? too long? too many unfamiliar terms?

tool

clang-format -style Google -i

    // bad input
# include <iostream>
int main()
{     std::cout <<    "hello world\n";     return 0; }
// bad input
#include <iostream>
int main() {
  std::cout << "hello world\n";
  return 0;
}

today’s plan

destructor

copy constructor

copy assignment operator

vector revisited

start from vector v0

#include <iostream>

typedef int T;

struct vector {
  size_t size;
  T *data;
};

int main(void) {
  size_t n = 10;
  vector v;
  v.size = n;
  std::cout << v.size << std::endl;
}

exercises

what’s the initial value of vector s?

change struct to class?

add constructor (new T[n] with initial_value) & size() & data()

default value

operator overloading []

explicit vs implicit constructor

hide default constructor? add it using default

initialization lists

range-based loop

destructor

valgrind

delete vs delete[] - valgrind again

shallow copy

deep copy

copy constructor

be careful: shallow vs deep copy

explicit vs implicit

copy assignment operator

be careful again: shallow vs deep copy

sum up: syntax craziness

vector v0;       // default constructor
vector v0a = 10; // constructor (implicit)
vector v0b(10);  // constructor (explicit)

vector v1 = v0;  // copy constructor (implicit)
vector v2(v0);   // copy constructor (explicit)

vector v3;
v3 = v0;         // copy assignment operator

rule of three

if you define one of

  • destructor
  • copy constructor
  • copy assignment operator

you probably should define all three of them.

reason: the compiler-generated version won’t work.

move semantics

vector foo(size_t n) {
  vector v(n);
  return v;
}
vector new_v(v);

return vector from a function?

copy constructor is heavy-weight

return a reference? lifetime problem.

use a reference to hold the value? compile error.

return value optimization (RVO): try -fno-elide-constructors

rule of five: move constructor & move assignment operator

move semantics

self-exercise 1

modify your 3D Point class from lec 9 exercise 1

  • disable the copy constructor and copy assignment operator

  • attempt to use copy & assign in code, and see what error the compiler generates

  • write a CopyFrom() member function, and try using it instead

soln

self-exercise 2

write a C++ class that:

  • is given the name of a file as a constructor argument

  • has a “GetNextWord()” method that returns the next whitespace or newline-separate word from the file as a copy of a “string” object, or an empty string once you hit EOF.

  • has a destructor that cleans up anything that needs cleaning up

soln

self-exercise 3

Write a C++ function that:

  • uses new to dynamically allocate an array of strings

  • uses delete[] to free it

  • uses new to dynamically allocate an array of pointers to strings
    • and then iterates through the array to use new to allocate a string for each array entry and to assign to each array element a pointer to the associated allocated string
    • and then uses delete to delete each allocated string
    • and then uses delete[] to delete the string pointer array

soln

see you on Wednesday

C++ object model