User Tools

Site Tools


docs:c_basics

C++ Basics

C++ is an object-oriented language. You can define new data types and their operations and then program with them much as you program with fundamental types such as int, char, float, etc. For example, you can define Matrix and Vector types and then write high-level linear algebra programs, like Matlab scripts.

Using classes

For example, you could program with typical Matrix and Vector types like this

  int M = 13;
  int N = 11;
  Matrix A(M,N);      // construct a matrix A
  Vector x(N);        // construct a vector x
 
  for (int i=0; i<M; ++i) 
    for (int j=0; j<N; ++ j) 
      A(i,j) = drand48();    // assign random numbers to A
 
  for (int j=0; j<N; ++j)
     x(j) = drand48();       // ditto for x
 
  Vector y = A*x;            // multiply A*x and then set y = A*x
 
  // print the results
  cout << "y's dimension is " << y.dim() << endl;
  cout << "y's value is " << y << endl;  

Some C++ vocabulary

  • a user-defined type is called a class; e.g. Matrix is a class. Classes are roughly like fundamental types.
  • variables of user-defined types are often called objects, e.g. A is an object of type Matrix.
  • objects are initialized or constructed by constructors, e.g. the statement Matrix A(M,N); constructs an M x N Matrix object named x. The (M,N) is an argument list for the Matrix constructor, in this case the row and columns dimensions. Constructors typically allocate memory and assign initial values to the object's internal data structures.
  • Classes have member functions and operators. A*x calls the Matrix, Vector multiplication operator, and y.dim() calls the dim() member function of object y.
  • cout << y << endl prints object y to standard output followed by a new line.

Reading header files

Of course, the code above requires the Matrix, Vector classes and their member functions to be defined elsewhere. Usually classes are declared in header files (matrix.h and vector.h) and implemented in source files (matrix.cpp and vector.cpp). The header file declarations define the high-level user interface to the class, and the source file defines the low-level programming that actually makes the classes work. To use a well-written library you will usually not have look at the source files. But you will want to able to read and understand the header files.

The declarations of the Matrix and Vector classes used above might look like this 1)

class Matrix {
  public:
    Matrix();                           // default ctor
    Matrix(int M, int N);               // ctor for M x N matrix
 
    operator=(const Matrix& A);         // assignment
 
    double& operator()(int i, int j);   // get/set (i,j) element
 
    int rows() const;                   // return # rows
    int cols() const;                   // return # cols
 
  private:
    // ... ignore everything labelled private ...
};
 
class Vector {
  public:
    Vector();
    Vector(int dim);
 
    operator=(const Vector& x);         // assignment
 
    double& operator()(int i);          // get/set (i) element
 
    int dim() const;                    // return # rows
 
  private:
    // ...ignore...
};
 
Vector operator*(const Matrix& A, const Vector& x);         // Matrix * Vector operator
 
operator ostream& operator<<(ostream& os, const Matrix& A); // Matrix print operator
operator ostream& operator<<(ostream& os, const Vector& x); // Vector print operator

Each line in the class declaration declares an object or a function that can be used in programming with the class. how a declaration translate to usage is tricky. The following table will help you get started. Remember, “declaration” means how the function declaration appears in a header file, and “usage” means how it is used in high-level programming.

DeclarationUsageMeaning
Matrix() Matrix A; construct a 0x0 Matrix named A
Matrix(int M, int N) Matrix A(3,4); construct a 3×4 Matrix named A
operator=(const Matrix& A) A = B; assign Matrix B to Matrix A
double& operator()(int i, int j) A(i,j) = 0.34; assign the i,j element of A…
double Aij = A(i,j) …or get the value of the i,j element
rows() int M = A.rows(); get the number of rows in A
Vector operator*(const Matrix& A, const Vector& x); Vector y = A*x; multiply A times x

This barely scratches the surface of C++ programming but it's enough to get you started programming with channelflow. Please refer to C++ books and online documentation for more information.

1)
normally the class declarations would go in two files matrix.h and vector.h but I'll cram the text together here
docs/c_basics.txt · Last modified: 2010/02/02 07:55 (external edit)