User Tools

Site Tools


docs:c_basics

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

====== 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. For example, you could program with typical %%Matrix%% and %%Vector%% types like this <code c++> 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; </code> Some C++ vocabulary * a user-defined type is called a **class**. %%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%%. * 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. * 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%%. * %%cout << y << endl%% prints object %%y%% to standard output followed by a new line. 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 they would go in two files %%matrix.h%% and %%vector.h%% but I'll cram the text together)) <code c++> class Matrix { public: Matrix(); // default ctor Matrix(int M, int N); // ctor for M x N matrix 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); 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>

docs/c_basics.1234799931.txt.gz · Last modified: 2009/02/16 07:58 by gibson