Latest web development tutorials

C ++ basic grammar

C ++ programs can be defined as a collection of objects, these objects interact with each other by a method call. Now let's briefly look at what is classes, objects, methods, real-time variables.

  • Objects - objects that have state and behavior.For example: a dog state - colors, name, breed, behavior - shaking, call out to eat. Objects are instances of classes.
  • Class - class may be defined to describe the behavior of the object / state Templates / blueprint.
  • Method - Basically, a method represents an act.A class can contain multiple methods. Logic can be written in the method, operational data and perform all actions.
  • Instant Variables - Each object has its unique real variable.State of the object is determined by the values ​​of these variables instantly created.

C ++ Program Structure

Let's look at a simple piece of code, you can output the wordsHello World.

#include <iostream>
using namespace std;

// main() 是程序开始执行的地方

int main()
   cout << "Hello World"; // 输出 Hello World
   return 0;

Next we explain above, this procedure:

  • C ++ language defines several headers, these headers contain useful information or programs required. Above, this program includes the header file<iostream>.
  • Lineusing namespace std; tell the compiler to use the std namespace.C ++ namespace is a relatively new concept.
  • The next line// main () is where the program begins execution is a single-line comment.Single-line comments begin with //, the line at the end of the end.
  • The next lineint main () is the main function, program execution begins here.
  • The next linecout << "Hello World"; will be displayed on the screen message "Hello World".
  • The next linereturn 0; termination of main () function returns the value 0 to the calling process.

Compile & execute a C ++ program

Let's look at how to save the source code in a file, and how to compile and run it. Below are simple steps:

  • Open a text editor, add the above code.
  • Save the file as hello.cpp.
  • Open a command prompt, change to the directory to save the file.
  • Type 'g ++ hello.cpp', press enter, compile the code. If there are no errors in the code, the command prompt will jump to the next line and generates a.out executable file.
  • Now type 'a.out' to run the program.
  • You can see on the screen 'Hello World'.
$ g++ hello.cpp
$ ./a.out
Hello World

Make sure that your path is included g ++ compiler, and make sure to run it in the directory containing the source file hello.cpp.

You can also use the makefile to compile C / C ++ program.

C ++ semicolons & Block

In C ++, the semicolon is a statement terminator. That is, each statement must end with a semicolon. It indicates the end of a logical entity.

For example, here are three different statements:

x = y;
y = y+1;
add(x, y);

Block is a group of statements enclosed in curly braces logical connection. E.g:

   cout << "Hello World"; // 输出 Hello World
   return 0;

C ++ does not end with the end of the line as a symbol of identity, so you can place multiple statements on one line. E.g:

x = y;
y = y+1;
add(x, y);

Equivalent to

x = y; y = y+1; add(x, y);

C ++ Identifier

C ++ identifier is used to identify the variables, functions, classes, modules, or any other user-defined project name. An identifier with the letters AZ or az or underscore _ Start, followed by zero or more letters, underscores and numbers (0-9).

Allowed punctuation characters, such as @, $ and% in the C ++ identifier. C ++ is a case-sensitive programming language. Thus, in C++, Manpower and manpowerare two different identifiers.

Here are some valid identifiers:

mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      retVal

C ++ keywords

The following table lists the C ++ reserved word. These words can not be reserved as a constant name, variable name, or other identifier names.



Three-character group is a three-character sequence for another character representation, also known as the three-character sequence. Two three-character sequence is always a question mark at the beginning.

Three-character sequence is less common, but the C ++ standard allows certain characters to specify a three-character sequence. In order that there is no previous character on the keyboard, which is a method essential.

Three-character sequence can appear anywhere, including strings, character sequences, comments and preprocessor directives.

Listed below are the most commonly used three-character sequence:


All compilers do not support the three groups of characters, in order to avoid confusion, do not recommend the use of three groups of characters.

C ++ spaces

Line contains only spaces, known as a blank line, possibly with comments, C ++ compiler to ignore it completely.

In C ++, the space for describing the blank, tabs, line breaks, and comments. Various parts of a space separate statements, so the compiler can identify the statement an element (such as int) where it ends, the next element where to start. Therefore, in the following statement:

int age;

Here, you must have at least one space character (usually a whitespace) between int and age, so the compiler to be able to distinguish between them. On the other hand, in the following statement:

fruit = apples + oranges;   // 获取水果的总数

fruit and =, = or space character between apples and is not required, but in order to enhance readability, you can add some appropriate spaces as needed.