User Tools

Site Tools


docs:c_basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:c_basics [2009/02/16 07:58]
gibson
docs:c_basics [2010/02/02 07:55] (current)
Line 5: Line 5:
 etc. For example, you can define %%Matrix%% and %%Vector%% types and then write high-level ​ etc. For example, you can define %%Matrix%% and %%Vector%% types and then write high-level ​
 linear algebra programs, like Matlab scripts. ​ linear algebra programs, like Matlab scripts. ​
 +
 +====== Using classes ======
  
 For example, you could program with typical %%Matrix%% and %%Vector%% types like this For example, you could program with typical %%Matrix%% and %%Vector%% types like this
Line 28: Line 30:
  
 Some C++ vocabulary Some C++ vocabulary
-   * a user-defined type is called a **class**. %%Matrix%% is a class. Classes are roughly like fundamental types. +   * 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**. %%A%% is an object of type %%Matrix%%. +   * 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** ​ %%Matrix A(M,N)%% constructs an 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.  +   * 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 ​operation, and %%y.dim()%% calls the %%dim()%% member function of object %%y%%.+   * 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.     * %%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 Of course, the code above requires the %%Matrix, Vector%% classes and their member functions to
Line 42: Line 46:
  
 The declarations of the %%Matrix%% and %%Vector%% classes used above might look like this The declarations of the %%Matrix%% and %%Vector%% classes used above might look like this
-((normally ​they would go in two files %%matrix.h%% and %%vector.h%% but I'll cram the text  +((normally ​the class declarations ​would go in two files %%matrix.h%% and %%vector.h%% but  
-together))+I'll cram the text together ​here))
 <code c++> <code c++>
  
Line 51: Line 55:
     Matrix(int M, int N);               // ctor for M x N matrix     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     double& operator()(int i, int j);   // get/set (i,j) element
 +
     int rows() const; ​                  // return # rows     int rows() const; ​                  // return # rows
     int cols() const; ​                  // return # cols     int cols() const; ​                  // return # cols
Line 64: Line 71:
     Vector(int dim);     Vector(int dim);
  
-    double& operator()(int i);         ​// get/set (i) element +    ​operator=(const Vector& x);         // assignment 
-    int dim() const; ​                  ​// return # rows+ 
 +    ​double& operator()(int i);          // get/set (i) element 
 + 
 +    int dim() const; ​                   // return # rows
   ​   ​
   private:   private:
Line 77: Line 87:
 </​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.
docs/c_basics.1234799931.txt.gz · Last modified: 2009/02/16 07:58 by gibson