Getting Started with C + + Chapter II: Start learning C + +
- Enter C + +
First , Below is a C + + program :
//myfirst.cpp display a line of text
#include <iostream>// Preprocessor Compilation Directives
int main ()// function Header
{
using namespace std;// Compiling Instructions
cout<< " Learning C + + ";// displaying Messages
cout<<endl;// Start next line
cout<< " you won't regret it! "; More Output
return 0;// return value
}
from the above program we can know C + + part of the program
- Comments.
- Preprocessor Compilation Directives #include
- function Header: int main ()
- Compile Instructions: Use namespace
- function Body : Use { and } enclosed.
- cout The statement that displays the message.
- End Main () of the function return statement.
We describe several of them separately:
Main () function
C + + in Main () function is defined with the function header int main () start.
int main () represents Main () a function can return a value to an integer that invokes it, and does not get any information from the function that called it.
Main (): Classic C start.
int main (void): no return value.
void Main () : There may be an error.
Comments
/**/ Multi-line comments.
// single-line comment.
cout for C + + output
For example, the following statement:
cout<< "Come up and C + + me some time.";
The double brackets enclose the message to be printed.
<< indicates that the statement sends this string to the cout the symbol indicates the path to the flow of information.
cout is a predefined object that knows how to display strings, numbers, and individual characters.
( 1 ) control character Endl
Example: cout<<endl;
Endl: represents a re-start of a row. Moves the cursor on the screen to the beginning of the next line.
Example: cout<< " Learning C + + "<<endl;
namely: cout<< " Learning C + + " ;
cout<<endl;
( 2 ) C + + format of source code
C + + a semicolon indicates the end, a statement can span multiple lines, and multiple statements can be on the same line. However, you cannot put spaces, tabs, and carriage returns in the middle of an element (such as a name), or you can place a carriage return in the middle of a string. However , a string in the C++11 standard can contain a carriage return.
C + + an indivisible element is called a tag, and usually you must separate two marks with a space, tab, or carriage return.
( 3 ) C + + Source Style
When programming, it is best to follow these rules
- Each statement occupies one row.
- Each function has a start brace and a closing curly brace, with two curly braces in each row.
- The statements in the function are indented relative to the curly braces.
- There is no white space around the parentheses associated with the function name.
C + + statements
cout tricks
cout when you print a variable, you can recognize the variable type and output the value of the variable.
Other C + + statements
Class Introduction
class is C + + one of the core concepts of object-oriented programming.
A class is a user-defined data type. To define a class, you need to describe what information it can represent and what actions he can perform on the data. A class definition describes the data format and its usage.
Note: The class describes all the properties of a data type, including the operations that can be performed with it, and objects are entities created from these descriptions.
Function
- The function is divided into two types with return value and no return value.
- Function properties: function head and function body; accept a parameter; return a value; a prototype is required.
2 Summary
C + + A program consists of one or more modules called functions. The program starts with the main () function. Functions are composed of function heads and function bodies. The function header indicates the type of the function's return value (if any) and the function expects the type of information passed to it through the parameter.
C + + statements are broadly divided into six types:
- Declaration statement: Defines the variable name and type used in the function.
- Assignment statement: Assigns a value to a variable using the assignment operator.
- Message statement: Sends a message to an object, firing a behavior.
- Function call: Executes the function, and after the called function is executed, the program returns to the statement following the function call statement.
- Function prototype: Declares the return type of the function, the number of arguments accepted by the function, and the type.
- Return statement: Returns a value from the called function to the calling function.
A class is a user-defined data type specification that describes in detail how information is represented and what operations can be done on the data. object is created from a class specification, just like a simple variable when the entity created by the data type description.
C + + provides two predefined objects for processing input and output Cin and the cout , they are IStream and the ostream instances of classes that they are in the file iostream defined in the.
C + + Primer Plus study: Chapter II