<>C++ Basic introduction

<>1 C++ First acquaintance

allegedly C++ Very good , Then we'll write the first program Hello World Procedure , After all, this is classic . that C++ What are the steps of program creation ?

<>1.1 first C++ program

Write a C++ The program is divided into 4 Steps

* Create project
* create a file
* Writing code
* Running program
<>1.1.1 Create project

​ Visual Studio We used it this time to write C++ Main tools of the program , Let's open it to create a new project .

<>1.1.2 create a file

Right click the source file , Select Add -> New item

to C++ File name , Then click Add .

So the first C++ Project creation completed , That's it ~
What about next ? Can I write code , Eager to try …

<>1.1.3 Writing code
#include<iostream> using namespace std; int main() { cout << "Hello world" <<
endl; system("pause"); return 0; }
first hello world The program is finished , Let's see how it works !

<>1.1.4 Running program

So the first C++ The program is finished , Run successfully . There should be applause here , Give yourself a little encouragement ~~

What exactly does the above code mean ? How do you do it according to your own idea Write and implement functions ? take it easy , Step by step , Now let's talk about C++ How to write code .

<>1.2 notes

Write your own code. Of course, you have to write comments , So that others can understand the meaning of your code .

effect : Add some explanation and explanation to the code , Make it easy for yourself or other programmers to read the code

Two formats

* Single-Line Comments :// Descriptive information
* Usually placed above a line of code , Or the end of a statement , Description of this line of code
* multiline comment : /* Descriptive information */
* Usually placed above a piece of code , Give an overall description of the code
Will the annotation compiler execute ?
The answer was …

can't , When the compiler compiles code , The contents of the comment are ignored .

<>1.3 variable

Now let's talk about C++ Custom variables used in the program .

effect : Name a specified memory space , Easy to operate this memory

grammar : data type Variable name = Initial value ;

Example :
#include<iostream> using namespace std; int main() { // Definition of variables // grammar : data type Variable name =
Initial value int a = 10; cout << "a = " << a << endl; system("pause"); return 0; }
In this way, you can create space in memory , Used to store variables a la . Of course, we must pay attention to one thing when creating variables . What is it ?

ps:C++ When creating variables , A variable must be given an initial value , Otherwise, an error will be reported .

<>1.4 constant

With custom variables , Is there a constant ?of course! necessary !

effect : Used to record unchangeable data in the program

C++ There are two ways to define constants

*
#define Macro constant : #define Constant name constant value

* Usually defined above the file , Represents a constant
*
const Decorated variable const data type Constant name = constant value

* Usually add keyword before variable definition const, Modify the variable as a constant , Not modifiable
Example :
//1, Macro constant #define day 7 int main() { cout << " In a week " << day << " day " << endl;
//day = 8; // report errors , Macro constants cannot be modified //2,const Modifier variable const int month = 12; cout << " A total of "
<< month << " Months " << endl; //month = 24; // report errors , Constants cannot be modified system("pause");
return 0; }
This is it. C++ Two common methods of defining constants in , Is it simple ~
so easy~

<>1.5 keyword

Let's start C++ Keywords in , This is C++ Is also very common and important , Listen carefully .

effect : Keyword is C++ Pre reserved words in ( identifier )

We talked about it above C++ Custom variables and constants for , The variable or constant name we defined , Available C++ Keywords in ?

The answer is :
may not , When defining variables or constants , Cannot use keyword .

C++ Keywords are as follows :

asmdoifreturntypedef
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizeofunion
caseenummutablestaticunsigned
catchexplicitnamespacestatic_castusing
charexportnewstructvirtual
classexternoperatorswitchvoid
constfalseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrowwhile
defaultfriendregistertrue
deletegotoreinterpret_casttry
Tips : When naming variables or constants , Don't use C++ keyword , Otherwise, there will be ambiguity . The compiler will also report errors .

As mentioned above, you cannot use keywords for naming custom variables and constants , Are there any other requirements ?
take it easy , Let's start with variables , Naming rules for constants .

<>1.6 Identifier naming rules

effect :C++ Specified to identifier ( variable , constant ) When naming , Have their own set of rules

* Identifier cannot be a keyword
* Identifier can only be composed of letters , number , Underline composition
* The first character must be a letter or underscore
* Letters in identifiers are case sensitive
Tips : When naming an identifier , Strive to achieve the effect of knowing what is known , Convenient for yourself and others to read . For example, we want to define a constant to indicate that a year has 12 Months , Then the constant name can be used Month Yes ~

<>2 data type

Now let's start to understand C++ Medium data type . data type ? What is this thing ? Full of question marks ???

The data type is ,C++ Specify when creating a variable or constant , The corresponding data type must be specified , Otherwise, memory cannot be allocated to variables . For example, in the above example int la .

I don't understand ? be free , Now let's start to talk about what these ghosts are ?

<>2.1 integer

effect : Integer variables represent integer type data

C++ There are several ways to represent integer types in , The difference lies in the memory space occupied :

Value range of space occupied by data type
short( Short )2 byte (-2^15 ~ 2^15-1)
int( integer )4 byte (-2^31 ~ 2^31-1)
long( Long shaping )Windows by 4 byte ,Linux by 4 byte (32 position ),8 byte (64 position )(-2^31 ~ 2^31-1)
long long( Long plastic surgery )8 byte (-2^63 ~ 2^63-1)
See here , I guess the above short,int, long Are they all C++ Keywords in ?

binggo That's right ~ Success remember C++ Three keywords in .

<>2.2 sizeof keyword

Is there a way for us to know the memory space occupied by these defined data types ?
of course , Senior Daniel thought of it long ago ,sizeof emerge as the times require .

effect : utilize sizeof Keyword can count the memory size occupied by the data type

grammar : sizeof( data type / variable )

Example :
int main() { cout << "short Memory space occupied by type is : " << sizeof(short) << endl; cout <<
"int Memory space occupied by type is : " << sizeof(int) << endl; cout << "long Memory space occupied by type is : " <<
sizeof(long) << endl; cout << "long long Memory space occupied by type is : " << sizeof(long long) <<
endl; system("pause"); return 0; }
Integer occupied space conclusion :short < int <= long <= long long

All the above are integer types , Can the PI be expressed ? Entangled ...

Now let's talk about how to express decimal floating point type .

<>2.3 Solid form ( float )

effect : Used to represent decimals

There are two types of floating point variables :

* Single precision float
* Double precision double
The difference between the two lies in the range of significant digits .

Data type occupied space significant digit range
float4 byte 7 Significant digits
double8 byte 15~16 Significant digits
Example :
int main() { float f1 = 3.14f; double d1 = 3.14; cout << f1 << endl; cout <<
d1<< endl; cout << "float sizeof = " << sizeof(f1) << endl; cout << "double
sizeof = " << sizeof(d1) << endl; // Scientific counting float f2 = 3e2; // 3 * 10 ^ 2 cout <<
"f2 = " << f2 << endl; float f3 = 3e-2; // 3 * 0.1 ^ 2 cout << "f3 = " << f3 <<
endl; system("pause"); return 0; }
WOW ! I've learned the definitions of integers and decimals . keyword : short,int,long,long long,float,double.
so easy!

I remember the first one C++ program , Exported hello world. That's not a number , What kind is it ?

take it easy , Let's start .

<>2.4 character

Start with the simple , letter ‘a’ start .

effect : Character variables are used to display a single character

grammar :char ch = 'a';

Of course, there must be precautions for a single character WOW .

be careful 1: When displaying character variables , Enclose characters in single quotation marks , Do not use double quotation marks

be careful 2: Only one character can be inside a single quotation mark , Cannot be a string

Integer type above , Floating point type , We all know how much memory is used , What about this character type ?

* C and C++ Medium character variables only occupy 1 Bytes .
* Character variables do not store characters in memory , Instead, the corresponding ASCII Code into storage unit
Example :
int main() { char ch = 'a'; cout << ch << endl; cout << sizeof(char) << endl;
//ch = "abcde"; // error , Double quotation marks are not allowed //ch = 'abcde'; // error , Only one character can be referenced within a single quotation mark cout << (int)ch
<< endl; // View character a Corresponding ASCII code ch = 97; // Can be used directly ASCII Assign values to character variables cout << ch << endl;
system("pause"); return 0; }
ASCII Code table :

ASCII Value control character ASCII Value character ASCII Value character ASCII Value character
0NUT32(space)64@96,
1SOH33!65A97a
2STX34"66B98b
3ETX35#67C99c
4EOT36$68D100d
5ENQ37%69E101e
6ACK38&70F102f
7BEL39,71G103g
8BS40(72H104h
9HT41)73I105i
10LF42*74J106j
11VT43+75K107k
12FF44,76L108l
13CR45-77M109m
14SO46.78N110n
15SI47/79O111o
16DLE48080P112p
17DCI49181Q113q
18DC250282R114r
19DC351383S115s
20DC452484T116t
21NAK53585U117u
22SYN54686V118v
23TB55787W119w
24CAN56888X120x
25EM57989Y121y
26SUB58:90Z122z
27ESC59;91[123{
28FS60<92/124|
29GS61=93]125}
30RS62>94^126`
31US63?95_127DEL
ASCII The code is roughly composed of the following two parts :

* ASCII Non print control character : ASCII Figures on the table 0-31 Assigned to control character , Used to control peripheral devices such as printers .
* ASCII Print character : number 32-126 Assigned to characters that can be found on the keyboard , Appears when you view or print a document .
don't be afraid ,ASCII Of course we don't need to remember the code table , Please help yourself if you can't forget .

We just have to remember a few common ones , such as a representative 97,A representative 65.

<>2.5 Escape character

Let's talk about the meaning of some special characters ~

effect : Used to indicate something that cannot be displayed ASCII character

The escape characters we commonly use at this stage are :\n \\ \t

Escape character meaning ASCII Code value ( decimal system )
\a alert 007
\b Backspace (BS) , Move current position to previous column 008
\f Page change (FF), Move the current position to the beginning of the next page 012
\n Line feed (LF) , Moves the current position to the beginning of the next line 010
\r enter (CR) , Move the current position to the beginning of the line 013
\t Horizontal tabulation (HT) ( Skip to next TAB position )009
\v Vertical tabulation (VT)011
\\ Represents a backslash character ""092
’ Represents a single quotation mark ( apostrophe ) character 039
" Represents a double quote character 034
? Represents a question mark 063
\0 number 0000
\ddd8 Hexadecimal escape character ,d Range 0~73 position 8 Base
\xhh16 Hexadecimal escape character ,h Range 09,af,A~F3 position 16 Base
Example :
int main() { cout << "\\" << endl; cout << "\tHello" << endl; cout << "\n" <<
endl; system("pause"); return 0; }
Just said hello world And ? What type is it ?
keep looking down to the bottom , The answer is coming soon ~

<>2.6 String type

effect : Used to represent a string of characters

Two styles

*
C Style string : char Variable name [] = " String value "

Example :
int main() { char str1[] = "hello world"; cout << str1 << endl;
system("pause"); return 0; }
be careful :C Style strings should be enclosed in double quotation marks

*
C++ Style string : string Variable name = " String value "

Example :
int main() { string str = "hello world"; cout << str << endl; system("pause");
return 0; }
be careful :C++ Style string , Need to add header file ==#include<string>==

very good ! Not only do I know hello world What type , Also learned two representations .

Like yourself ~ Let's learn the last one while it's hot C++ Common variable types in .

<>2.7 Boolean type bool

effect : Boolean data types represent true or false values

bool Type has only two values :

* true — really ( The essence is 1)
* false — false ( The essence is 0)
bool Type accounting 1 Byte size

Example :
int main() { bool flag = true; cout << flag << endl; // 1 flag = false; cout
<< flag << endl; // 0 cout << "size of bool = " << sizeof(bool) << endl; //1
system("pause"); return 0; }
Ouch ! original bool Type is used to indicate true and false ,true It means true ,false It means false .

That number can mean bool Type ?

sure , Remember , number 0 express false– false , wrong 0 All numbers indicate true– really .

Just the first program , It's a sentence hello world, Can I input it through the keyboard ?
Here's how the method implements the input content .

<>2.8 Input of data

effect : Used to get data from the keyboard

keyword : cin

grammar : cin >> variable

Example :
int main(){ // Integer input int a = 0; cout << " Please enter an integer variable :" << endl; cin >> a; cout << a
<< endl; // Floating point input double d = 0; cout << " Please enter a floating point variable :" << endl; cin >> d; cout << d
<< endl; // Character input char ch = 0; cout << " Please enter a character variable :" << endl; cin >> ch; cout <<
ch << endl; // String input string str; cout << " Please enter a string variable :" << endl; cin >> str; cout
<< str << endl; // Boolean type input bool flag = true; cout << " Please enter a boolean variable :" << endl; cin >>
flag; cout << flag << endl; system("pause"); return EXIT_SUCCESS; }
okay , That's all for today . Continue tomorrow ~

Let me know , The next issue is about program operators and program flow structure ~

Hard to create , White whoring is not good , Your support and recognition , Is the biggest motivation for my creation , See you in the next article !

Dragon juvenile | writing

If there are any errors in this blog , Please comment , esteem it a favor !

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test