An overview of getting started in C++ and trying to create your first C++ program

  • 2020-04-02 03:20:41
  • OfStack

C++ program composition and writing form
1) a C++ program can be composed of one or more program units. Each program unit is treated as a file. At program compilation time, the compilation system compiles individual files separately, so a file is a compilation unit.

2) in a program unit, it can include the following parts:
Preprocess commands. The #include command is included in all four programs in the previous section.
The global declaration part (the declaration part outside the function). This section includes declarations of user-defined data types and definitions of variables used in the program.
Function. A function is the part that implements the operation, so a function is a necessary and fundamental part of a program. Each program must include one or more functions, of which there must be one (and only one) main function.

However, every program file does not have to have the above three parts, can be missing some parts (including functions).

3) a function consists of two parts:
The head of the function, the first row of the function. Include function name, function type, function attribute, function parameter (parameter) name, parameter type. Note: a function name must be followed by a pair of parentheses. Function arguments can be default, such as int main().
The body of the function, that is, the part of the function below the curly braces. If there are multiple braces in a function, the outermost pair of {} is the range of the function body.

The function body generally includes:
Locally declared part (the declared part within a function). Includes the types used in the function, the declaration of the function, and the definition of the variables. Note: declarations of data can be placed outside a function (whose scope is global) or inside a function (whose scope is local and valid only within this function).
The executive part. It is composed of several execution statements, which are used to perform relevant operations to achieve the function function.

4) statements include two types: one is the declaration statement, and the other is the execution statement. C++ assigns a specific function to each statement. Statements are the basic components that implement operations, and obviously, functions without statements are meaningless. C++ statements must end with a semicolon.

5) a C++ program is always executed from the main function, regardless of the position of the main function in the entire program.

6) class is an important new data type added by C++, which is the most important development of C++. With class, we can realize the functions of encapsulation, information concealment, inheritance, derivation, polymorphism and so on in the object-oriented programming method. A class can include data members and member functions, which can be specified as private and public properties. Private data members and member functions can only be called by member functions of this class.

7) C++ program writing format is free, a line can be written several statements, a statement can be written on more than one line. C++ programs have no line Numbers, and unlike FORTRAN or COBOL, they do not have strict formatting (statements must be written from a column).

8) a good, useful source program should add the necessary comments to increase the readability of the program. C++ also retains the annotation form of C, and you can use "" to annotate any part of a C++ program. Everything in between "and" serves as a comment.
When annotated with "//", the valid range is only one line, that is, the line is valid and cannot cross lines. The valid range for comments with "" is multiple lines. Just have a "" at the beginning. Therefore, the general rule is that simple comments with less content are often "//" and longer ones are "".

The first C++ program
Case 1


#include <iostream> //Contains the header iostream
using namespace std; //Use the namespace STD
int main( )
{
  cout<<"This is a C++ program.";
  return 0;
}

At runtime, the following line of information is printed on the screen:


This is a C++ program.

Use the name main for the "main function". Every C++ program must have a main function. The int before main declares that the function is of type integer. Line 6 of the program returns a zero value to the operating system. If the program does not execute properly, it automatically returns a non-zero value to the operating system, typically -1.

The body of the function is defined by curly braces {   } enclosed. In this case, there is only one statement in the main function that begins with cout. Note that all C++ statements should end with a semicolon.

Now look at line 1, "#include < iostream > ", this is not a C++ statement, but a C++ preprocessing command, it begins with "#" to distinguish from C++ statements, the end of the line without a semicolon. # include < iostream > Is an "include command" that replaces the command line by including the contents of the file iostream in the program file where the command resides. The function of the file iostream is to provide the program with the information it needs for input or output. Iostream is a combination of the three words i-o-stream. From its form, you can know that it stands for "input/output stream". When the program is compiled, all preprocessing commands are processed, the header file is replaced by the #include command line, and then the program unit is compiled as a whole.

Line 2 of the program "using namespace STD; "Means" use the namespace STD." Classes and functions in the C++ standard library are declared in the namespace STD, so if you need to use the C++ standard library in your program (in this case, the #include command line), you need to use "using namespace STD; "Declares that the contents of the namespace STD are to be used.

When you're just starting to learn C++, you don't have to dig into lines 1 and 2 of this program, just know that if the program has input or output, you must use "#include < iostream > "Command to provide the necessary information, while using the" using namespace STD;" , so that the program can use this information, otherwise the program will compile an error.

Case 2
Find the sum of a and b. You can write the following program:


//Find the sum of the two Numbers (the line is the comment line)
#include <iostream> //Preprocessing command
using namespace std; //Use the namespace STD
int main( ) //Head of main function
{ //Function body start
  int a, b, sum; //Define variables
  cin>>a>>b; //The input statement
  sum=a+b; //Assignment statement
  cout<<"a+b="<<sum<<endl; //Output statements
  return 0; //If the program ends normally, a zero value is returned to the operating system
} //End of the function

The purpose of this procedure is to find the sum of two integers a and b. Line 1, "// sum of two Numbers", is a comment line, and C++ specifies that if "//" appears on a line, everything from its beginning to the end of the line is a comment.

If entered from the keyboard at run time


  123 456 � 

The output of


  a+b=579

Example 3
I'm going to give you two Numbers x and y, and I'm going to take the big one. In this example, there are two functions.


#include <iostream> //Preprocessing command
using namespace std;
int max(int x,int y) //Define the Max function, whose value is integer, and the formal parameters x and y are integer
{ //The Max body starts
  int z; //Variable declaration that defines the variable z used in this function to be an integer
  if(x>y)
   z=x; //If statement, if x> Y, then assign the value of x to z
  else z=y; //Otherwise, assign the value of y to z
   return(z); //Returns the value of z, via Max, back to the call
} //End of Max function
int main( ) //The main function
{ //The main function Body started 
  int a,b,m; //Variable declarations
  cin>>a>>b; //The values of the input variables a and b
  m=max(a,b); //Call the Max function and assign the resulting value to m
  cout<<"max="<<m<<'n'; //Output the value of the large number m
  return 0; //If the program ends normally, a zero value is returned to the operating system
} //The main function The end of the 

This program includes two functions: the main function main and called function Max. The program runs as follows:


18 25  �   (input 18 and 25 to a and b ) 
max=25  (output m The value of the) 

Note that input data is separated by one or more Spaces, not by commas or other symbols.

In the above program, the Max function appears before the main function, so when the Max function is called from the main function, the compilation system recognizes that Max is the defined function name. If the positions of the two functions are swapped, that is, the main function is written first and the Max function is written later. At this time, when the main function meets Max, the compilation system cannot know the meaning of Max, so it cannot be compiled.

To solve this problem, the called function needs to be declared in the main function. The above program can be rewritten as follows:


#include <iostream>
using namespace std;
int main( )
{
  int max(int x,int y); //Declare the Max function
  int a,b,c;
  cin>>a>>b;
  c=max(a,b); //Call the Max function
  cout<<"max="<<c<<endl;
  return 0;
}
int max(int x,int y) //Define the Max function
{
  int z;
  if(x>y) z=x;
  else z=y;
  return(z);
}

Simply add a semicolon to the end of the first part of the called function to make it a function declaration. The position of the function declaration should precede the function call.

The following is a C++ program that contains classes and objects to give the reader a taste of how C++ embodies an object-oriented approach to programming.

Example 4
C++ program containing classes.


#include <iostream>//Preprocessing command
using namespace std;
class Student//Declare a class named Student
{
  private: //The following are the private parts of the class
  int num; //Private variable num
  int score; //Private variable score
  public: //The following are the common parts of the class
  void setdata( ) //Define the public function setdata
  {
   cin>>num; //Enter the value of num
   cin>>score; //Enter the value of score
  }
  void display( ) //Define a common function display
  {
   cout<<"num="<<num<<endl; //Output the value of num
   cout<<"score="<<score<<endl;//Output the score value
  };
}; //Class declaration ends
Student stud1,stud2; //Define stud1 and stud2 as variables of the Student class, called objects
int main( )//Head of main function
{
  stud1.setdata( ); //Call the setdata function of the object stud1
  stud2.setdata( ); //Call the setdata function of the object stud2
  stud1.display( ); //Call the display function of the object stud1
  stud2.display( ); //Call the display function of the object stud2
  return 0;
}

There are two types of members in a class: data and functions, called data members and member functions. In C++, a set of data is encapsulated with functions entitled to call it, forming a data structure called a class. In the above program, the data member num, score and member functions setdata, display form a "class" type named Student. Member functions are used to manipulate data members. That is, a class consists of a set of data and functions that operate on it.

Class can represent the encapsulation of data and information hiding. In the above program, when declaring the Student class, the data and functions in the class are divided into two categories: private and public. Specify all data (num, score) as private and all functions (setdata, display) as public. In most cases, all data is specified as private for information hiding.

A variable with the characteristics of a class type is called an object.

Lines 18 through 24 are the main function.

The program runs as follows:


1001 98.5  �   ( Enter the student 1 Student Numbers and grades )
1002 76.5  �   ( Enter the student 2 Student Numbers and grades )
num=1001 ( Output the student 1 The student id )
score=98.5  ( Output the student 1 The achievement of )
num=1002 ( Output the student 2 The student id )
score=76.5  ( Output the student 2 The achievement of )


Related articles: