C + + Express, suitable for C-base friends (1)

Source: Internet
Author: User
Tags function definition setf

The overall layout of the 1.c++ program
#include <iostream>//"<iostream>" cannot have extra spaces
Using Nameplace STD//here C + + is unique, C not. Another way of writing is to write the above two lines as #include <iostream.h&gt, corresponding to the older version. If the compiler requires such a notation, consider
int main ()//(next line) upgrades the compiler.
{
< statement block >;
return 0;
}
C + + has the same annotation as C, and the requirements for indentation are not as strict as python. Good indentation habits can improve the readability of the program, it is a good habit to develop.

2.c++ Basic Knowledge
A. Variable and assignment C + + is case sensitive
This section and Class C times, here do not repeat. In this reminder, the variable name as much as possible to make a meaningful name, so as to help programming, programming to develop the habit of writing comments.
B. Input and output
%1. Using cout for output:
Eg:cout << number_of_stu << "bedrooms\n"; You can output the value of the Number_of_stu variable and the contents of the following quotation marks.
Equivalent to:
cout << Number_of_stu;
cout << "bedrooms\n";
An expression can also be included in a cout statement, and the expression is enclosed in "()".
Eg:cout << "The total Cost is" << (Price+tax);
If the cout statement is too long, it can become two rows.
Eg:cout << "There is" << number_of_bed
<< "beds and" << number_of_table << "tables in the room\n";
%2. Escape sequence:
Use \ To tell the compiler that the symbol behind the \ needs to be escaped, or to eliminate the default meaning of the escape character, just to display the character.
Common escape characters:/n newline character (Endl meaning same)
/t tab (jumping spaces)
You can consider the end of "/n" when programming, to avoid mixing the output of the next program with this program.
%3. Format a number with a decimal point:
Add the following instructions to the program:
COUT.SETF (ios::fixed);
COUT.SETF (Ios::showpoint);
Cout.precision (2);
You can output a decimal of 2 decimal places.
%4. Input with cin: similar to cout usage:
Eg:cout << "Please enter your age:\n";
CIN >> age;
You can enter two variables from the keyboard, separated by a space:
Eg:cin >> number >> size;
C. Data types and expressions
%1.short/int/long/float/double/long Double/char and C similar, no longer repeat.
It is important to note that the char type refers to any single character on the keyboard. To be represented by '. ' A ' is not the same as ' a ', which is a string class and cannot be represented by a char type.
When programming involves division operations it is best to use float type to avoid errors.
In addition, unlike C, there are two ways to declare.  int num=1; or int num (1);
The%2 bool type has only ture or Fales two values for the spoke statement.
%3 Introduction to the String class:
To use the String class, first include the string library:
#include <string>
Variables are declared and assigned in the same way as before:
string sex;
Sex = "female";
This allows for such operations:
String Day;
Day= "Monday" + "tuseday";
Get the value of day as "Mondaytuseday"
%4 here to add the rules named for constants: using the Const keyword
eg:const int windows_number = 10; You can use a name instead of a number in your program. Although it is not required to name a constant, it has become a habit for most programmers.
D. Simple control process
%1. A simple branching mechanism:
if (boolean_expression)//related to "&&", "| |" Use of parentheses when using. Note the difference between "=" and "= =" Application.
{
yes_statement_1;
yes_statement_2;
...
}
Else
{
no_statement_1;
no_statement_2;
...
This section is basically the same as the C language.
%2. Simple looping mechanism
While loop:
while (boolean_expression)//When the value of Boolean_expression is false to end the loop, note there is no semicolon here.
{
statement_1;
statement_2;
...
}
Do_while cycle: Suitable for the loop body must be performed once the occasion, less common.
Do
{
statement_1;
statement_2;
...
}while (Boolean_expression); Note the semicolon here
%3. Increment, decrement operator
N++,n--, ++n,--n is similar to C. It is important to note that in C + +, they can only be used for single variables, (x+y) + + This expression is illegal.
%4. Multi-Channel Branch: The use of "{}" should be noted in the multi-channel branch to ensure the pairing of If-else is correct.
In C + +, the statement in {} is called a statement block, and the variable declared in the block is a local variable.
%5. Multiple If-else statements:
if (boolean_expression_1)
statement_1;
else if (boolean_expression_2)
statement_2;
else if (boolean_expression_3)
Statement_3;
...
Else
Statement_last;
%6.switch statement:
Switch (controller_expression)
{
Case Constant_1
Statement_sequence_1;
Break Don't forget the use of break.
Case Constant_2
statement_sequence_2;
Break
...
Default://optional, preferably plus can be used to display relevant information.
Statement_sequence_last;
}
%7.for statement:
for (intialization_action;boolean_expression;update_action)
Body_statement;
Eg:for (i=100;i>0;i--)//Do not add a semicolon here, and a semicolon will make the for (i=100;i>0;i--) a separate statement.
Count << "There is" << I << "Students left in this room\n";
Note Using break can also jump out of the loop. Break terminates only the most inner loop that contains it.
When working with nested loops, you also need to be aware of the relationships between the inner and outer layers.

3. Process abstraction and functions that return a value:
A. Predefined function: Refers to a function that has been defined before C + + and is stored in the corresponding header file. When used, declare a header file that contains such headers.
Eg: #include <cmath>
#include <cstdlib>
B. Coercion type conversion: You can use the function static_cast<double> () function to convert the int type to a double type to ensure that the corresponding calculation is correct.
Eg:candy_per_person=static_cast<double> (Total_candy_number)/_of_person;
C. Custom functions:
The description of the%1 function is divided into two parts, namely function declaration and function definition. The declaration of a function is placed in the front position, usually before the main function.
eg:double total_cost (int number_of_basketball, double price_cost_signal); Note the semicolon here
The problem that the shape participates in the argument is consistent with C.
%2 bool Type return value function
The function can return a bool-type value, declared as follows:
BOOL function name (parameter table)
Eg:bool app (int race);
The function body can be this:
BOOL App (int race)
{
return (RACE&GT;10);
}
Note When you call a function, the order of the arguments cannot occur incorrectly.
%3 local variables, global constants, and global variables
The constants declared by the const int are placed in the front of the program, and global constants are declared, which can be used in any function.
Removing the const declaration is a global variable, and the application of global variables can lead to errors, which is not recommended for programmers who are not very experienced.
A local variable is defined inside a function and only has a corresponding meaning within the function. One thing to emphasize is that the call parameter is a local variable, and the direct manipulation of the variable within the function does not affect the variables outside the function.
The value.
%4 Overloaded Function name
C + + allows two or more definitions for the same function name, which means that the names can be reused under different circumstances. Providing two or more definitions for the same function name is called overloading the function name.
The overload of C + + is much simpler, as long as you define a function in the normal way of function definition. When overloading a function name, the parameters of the two function declarations must differ so that the compiler can correctly find
The corresponding function. Only a different return type is not overloaded. Do not overload two functions that are not related to each other.

4. All sub-task functions
a.void function
A function that does not return a value is called a void function.
void function declaration: void Fuction_name (Parameter_list);
B. Transmitting a reference to a calling parameter
A parameter is called using a pass-through value, which is the value of the argument (not the actual argument itself) that is used to replace the parameter.
When invoking a parameter using a pass-through reference, the corresponding argument in the function call must be a variable, which will be an argument variable and the variable itself.
In order to mark a reference calling parameter, you need to add & after the parameter type in the function declaration and function definition header.
Eg:void get_input (double& f_variable)
{
using namespace Std;
Count << "Please enter a number:\n";
Cin >> f_variable;
}
If you want the function to change the value of a variable, use a reference call, and the rest can be called by value.
C. Using process abstraction
The principle of process abstraction is to write a function as a black box, and the user only needs to know his conditions of use and the results of its use, without having to know the way in which it is implemented internally.
When writing a comment for a function, it is a good way to divide into pre-condition and post-condition. The content of the former condition is the necessary condition for using the function, and then the condition can write the result of the function.
D. Testing and debugging functions
You can write a program to test a function, so that you can better locate the error. If the test requires a different function, and it has not yet been written or has not been tested, you can write a stub function placeholder:
Eg:double Price (int. number,double Cost)//this is a stub
{
return (9.99);
}
E. General commissioning techniques
%1. Resolutely stop relying on intuitive changes to the code to look forward to change the mentality!!!
%2. Locating errors is of great significance for fixing bugs. You can strategically insert the cout function to help determine the location of the bug.
%3.ASSERT macro
An Assert macro is a statement that ensures that the expected condition is true at the position of the Assert statement. If the condition is not satisfied, the program will error and exit.
To use an Assert macro first #include <cassert>
form of Use: Assert (Boolean_expression);
Note: Add #define NDEBUG before # include <cassert> to disable all assert macros.

C + + Express, suitable for C-base friends (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.