Lecture 4: The Vector class

../_images/L4-title.png Lecture 4 slides Lecture 4 panopto Lecture 4 podcast

(Originally recorded 2019-04-11)

In this lecture we start to get into some of the features of C++ that make it such a powerful language for HPC – and, in general, for building large-scale software systems.

In particular, we go step-by-step through the development of a C++ class for representing a mathematical vector (an N-tuple).

By the end of lecture we 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_;
};

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 functions

  • private and public

  • constructors

  • initialization syntax

  • operators

  • operator+

  • operator()

  • const