当前位置:网站首页>Basic knowledge of C + +

Basic knowledge of C + +

2020-11-07 22:16:39 Three ah three water

C++  Basic grammar

C++ A program can be defined as a collection of objects , These objects interact by calling each other's methods . Now let's take a brief look at what a class is 、 object , Method 、 Real time variables .

object -  Objects have states and behaviors . for example : The state of a dog - Color 、 name 、 Varieties , Behavior - Shake 、 Cry 、 eat . Object is an instance of a class .

class -  Class can be defined to describe object behavior / Template of state / The blueprint .

Method -  Basically , A method represents a behavior . A class can contain multiple methods . You can write logic in a method 、 Manipulate the data and perform all the actions .

Real time variables -  Each object has its own unique immediate variable . The state of an object is created by the values of these immediate variables .

 

C++ Program structure

 

Let's look at a simple piece of code , Can output words Hello World.

Now let's talk about the procedure above :

C++ The language defines some header files , These header files contain necessary or useful information in the program . In the above procedure , Contains header files <iostream>.

The next line using namespace std; Tell the compiler to use std Namespace . The namespace is C++ A relatively new concept in .

The next line // main() It's where the program starts It's a single line comment . Single line comment with // start , End of line .

The next line int main() It's the main function , The program starts from here .

The next line cout << "Hello World"; A message will be displayed on the screen "Hello World".

The next line return 0; End main( ) function , And return the value... To the calling process 0.

 

Xiaobian recommends a study C Language /C++ Learning skirt of 【  712,284,705】, Whether you are Daniel or Xiaobai , If you want to change your career or join it, you can learn about it and learn from it together ! There are development tools in the skirt , A lot of dry goods and technical information sharing !

 

 

compile & perform C++ Program

Next, let's see how to save the source code in a file , And how to compile and run it . Here are the simple steps :

Open a text editor , Add the above code .

Save the file as hello.cpp.

Open Command Prompt , Go to the directory where the file is saved .

type 'g++ hello.cpp ', Enter enter enter , Compile code . If there are no errors in the code , The command prompt jumps to the next line , And generate a.out Executable file .

Now? , type ' a.out' To run the program .

You can see on the screen that ' Hello World '.

Please make sure your path contains g++ compiler , And make sure to include the source file hello.cpp The directory in which it runs .

You can also use the makefile To compile the C/C++ Program .

Xiaobian recommends a study C Language /C++ Learning skirt of 【  712,284,705】, Whether you are Daniel or Xiaobai , If you want to change your career or join it, you can learn about it and learn from it together ! There are development tools in the skirt , A lot of dry goods and technical information sharing !

 

C++ Semicolon in & Sentence block

 

stay C++ in , Semicolons are statement terminators . in other words , Each statement must end with a semicolon . It shows the end of a logical entity .

for example , Here are three different sentences :

 

A statement block is a set of logically connected statements enclosed in braces . for example :

C++ Identification that does not end of line as terminator , therefore , You can place multiple statements on a line . for example :

Equate to

C++ identifier

C++ Identifiers are used to identify variables 、 function 、 class 、 modular , Or any other user-defined project name . An identifier is in letters A-Z or a-z Or the underscore _ Start , Followed by zero or more letters 、 Underline and numbers (0-9).

C++ Punctuation characters... Are not allowed in identifiers , such as @、& and %.C++ It's a case sensitive programming language . therefore , stay C++ in ,Manpower  and  manpower  Are two different identifiers .

 

A few valid identifiers are listed below :

C++ keyword

 

The following table lists them C++ Reserved word in . These reserved words cannot be used as constant names 、 Variable name or other identifier name .

Three character group

A three character group is a sequence of three characters used to represent another character , Also known as a three character sequence . Three character sequences always start with two question marks .

Three character sequences are not very common , but C++ The standard allows certain characters to be specified as a three character sequence . In the past, in order to indicate the characters not on the keyboard , This is an essential method .

Three character sequences can appear anywhere , Including strings 、 Character sequence 、 Comments and preprocessing instructions .

 

Here is a list of the most commonly used three character sequences :

If you want to have two consecutive question marks in the source program , And don't want to be replaced by the preprocessor , This happens with character constants 、 String literal value or in program comments , The alternative is to use automatic concatenation of strings :"...?""?..." Or the escape sequence :"...?\?...".

from Microsoft Visual C++ 2010 version , By default, the compiler no longer automatically replaces three character groups . If you need to use a three character group to replace ( For example, in order to be compatible with ancient software code ), Need to set compiler command line options /Zc:trigraphs

g++ Three character groups are still supported by default , But a compilation warning will be given .

 

C++ Spaces in

 

Lines that contain only spaces , It's called a blank line , Maybe with comments ,C++ The compiler will completely ignore it .

stay C++ in , Spaces are used to describe spaces 、 tabs 、 Line breaks and comments . Spaces separate parts of a statement , Let the compiler recognize an element in a statement ( such as int) Where to end , Where does the next element start . therefore , In the following sentence :

ad locum ,int and age There must be at least one space character between ( It's usually a blank character ), So that the compiler can distinguish them . On the other hand , In the following sentence :

fruit and =, perhaps = and apples The space character between is not required , But to enhance readability , You can add some spaces as needed .

 

If you are right about C/C++ Interested in , Want to learn more about , Here is a communication group to recommend to you .

There are material packages in the group , Beginner learning resources , There are also free courses in project development . It's even more amazing 、 Daniel online answers professional knowledge for you , Help you on the way to study ~

Whether it's Xiaobai or an advanced person , You can grow up here . Click me to enter the shortcut


版权声明
本文为[Three ah three water]所创,转载请带上原文链接,感谢