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 // ...fill in values of A and 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> In C++ * 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. The

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