====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docs:c_basics [2009/02/16 07:08] gibson created |
docs:c_basics [2010/02/02 07:55] (current) |
||
---|---|---|---|
Line 6: | Line 6: | ||
linear algebra programs, like Matlab scripts. | linear algebra programs, like Matlab scripts. | ||
- | For example, | + | ====== Using classes ====== |
+ | For example, you could program with typical %%Matrix%% and %%Vector%% types like this | ||
<code c++> | <code c++> | ||
+ | int M = 13; | ||
+ | int N = 11; | ||
+ | Matrix A(M,N); // construct a matrix A | ||
+ | Vector x(N); // construct a vector x | ||
- | int N = 10; | + | for (int i=0; i<M; ++i) |
- | Matrix A(N,N); | + | for (int j=0; j<N; ++ j) |
- | Vector x(N); | + | A(i,j) = drand48(); // assign random numbers to A |
- | // ...fill in values of A and x... | + | 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; | ||
+ | </code> | ||
+ | |||
+ | 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 | ||
+ | ((normally the class declarations would go in two files %%matrix.h%% and %%vector.h%% but | ||
+ | I'll cram the text together here)) | ||
+ | <code c++> | ||
+ | |||
+ | 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 | ||
| | ||
- | Vector y = A*x; | + | private: |
+ | // ... ignore everything labelled private ... | ||
+ | }; | ||
- | cout << y << endl; | + | 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 | ||
</code> | </code> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | ^Declaration^Usage^Meaning^ | ||
+ | |%%Matrix()%% | %%Matrix A;%% | construct a 0x0 Matrix named A | | ||
+ | |%%Matrix(int M, int N)%% | %%Matrix A(3,4);%% | construct a 3x4 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. |