(Click on title slide to access entire slide deck.)

Lecture Panopto Watch the lecture here.

Lecture 5

In this lecture we continue exploration of C++ support for classes, looking at constructors and accessors and overloading. And const.

We ultimately arrive at the following Vector class:

#include <vector>

class Vector {
public:
  Vector(size_t M) : num_rows_(M), storage_(num_rows_) {}

        double& operator()(size_t i)       { return storage_[i]; }
  const double& operator()(size_t i) const { return storage_[i]; }

  size_t num_rows() const { return num_rows_; }

private:
  size_t num_rows_;
  std::vector<double> storage_;
};

Note that there are two defintions for operator().

This is a straightforward, but surprisingly powerful, representation for a vector.

Important C++ concepts that we focus on during the development are

  • class definition
  • member functionws
  • private and public
  • constructors
  • initialization syntax
  • operators
    • operator+
    • operator()
  • const

Previous section:
Next section: