An introductory overview of C + + and an attempt to create the first C + + program _c language

Source: Internet
Author: User
Tags error handling

Composition and writing form of C + + program
1 A C + + program can be composed of one program unit or multiple program units. Each program unit acts as a file. When the program compiles, the compilation system compiles each file separately, so a file is a compilation unit.

2 in a program unit, you can include the following sections:
preprocessing commands. The #include command is included in the 4 programs in the previous section.
The Global Declarations section (the Declarations section outside the function). In this section, you include a definition of a variable that is used in the declaration of the user's own defined data type and in the program.
Function. A function is a part of the implementation, so a function is a necessary and fundamental part of the program. Each program must include one or more functions, which must have one (and only one) main function (the main function).

However, it does not require that each program file must have more than 3 parts, and that some parts (including functions) are missing.

3 A function consists of two parts:
The first line of a function. Includes the function name, function type, function property, function argument (parameter) name, parameter type. Note: A function name must be followed by a pair of parentheses, and the function argument can be default, such as int main ().
function body, that is, the part of the curly braces underneath the first part of the letter. If there is more than one brace in a function, the outermost pair of {} is the scope of the function body.

Function bodies generally include:
Local Declarations section (in the Declarations section within a function). Includes definitions of the types, functions, and variables used in this function. Note: A declaration of data can be placed outside of a function (its scope is global) or within a function (its scope is local and only valid within this function).
Executive part. Consists of several execution statements that are used to perform related operations to implement function functions.

4 The statement includes two categories: one is the declaration statement and the other is the execution statement. C + + assigns a specific function to each statement. The statement is the basic component of the implementation operation, obviously, the function without the statement is meaningless. C + + statements must end with semicolons.

5 a C + + program always starts with the main function, regardless of the position of the main function throughout the program.

6 classes (Class) is the new addition of C + + important data types, is the C + + for the most important development. With the class, we can realize the functions of encapsulation, information concealment, inheritance, derivation and polymorphism in object-oriented programming method. You can include data members and member functions in a class, and they can be specified as private (private) and common (public) properties. Private data members and member functions can only be invoked by member functions of this class.

7 C + + program writing format free, a line can write several statements, a statement can be written on multiple lines. C + + programs do not have line numbers and are not as strict as FORTRAN or COBOL in writing format (statements must be written from a column).

8 A good, useful source program should be added with the necessary comments to increase the readability of the program. C + + also retains the form of annotations in the language, which can be annotated with "/*......*/" for any part of the C + + program. All content between "/*" and "* *" as a comment.
When commenting with "//", the valid range is only one line, that is, the bank is valid and cannot span rows. When you use "/*......*/" as a comment, the valid range is multiple lines. As long as there is a "/*" At the beginning, there is a "* *" at the end of the last line. Therefore, the general custom is: less content of simple annotations commonly used "//", the content of the longer common "/*......*/."

First C + + program
Example 1

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

The following line of information is printed on the screen at run time:

This is a C + + program.

Use main to represent the name of the main function. Each C + + program must have a main function. The function of the int before main is to declare the type of the function as an integral type. The 6th line of the program is to return a value of 0 to the operating system. If the program does not perform correctly, it will automatically return a non-0 value to the operating system, typically-1.

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

Then look at the 1th line of the program "#include <iostream>", which is not a C + + statement, but a C + + preprocessing command, which begins with "#" to distinguish it from C + + statements, and there is no semicolon at the end of the line. #include <iostream> is a "include command" that is used to include the contents of a file iostream in the program file where the command is located, in place of the command line. The role of file iostream is to provide the program with some information needed for input or output. Iostream is a combination of the I-o-stream 3 words, from which it can be known that it represents an "input-output stream", since such files are placed at the beginning of the program unit and are called "header Files" (head file). When the program is compiled, all preprocessing commands are processed first, the contents of the header file are replaced #include命令行, and then the whole program unit is compiled.

The 2nd line of the procedure "using namespace std;" "means" using a 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 (which requires the #include command line) in your program, you need to use the "using namespace std;" "declares that the contents of the namespace Std are to be used.

In the beginner C + +, the 1th, 2 lines in this program can not be delved into, just know: If the program has input or output, you must use the "#include <iostream>" command to provide the necessary information, and to use "using namespace std;", Enables the program to use this information, otherwise the program compiles with an error.

Example 2
find the sum of a and b two numbers. You can write the following program:

Two number of the sum (line is commented)
#include <iostream>//preprocessing command
using namespace std;//using the namespace std
int main ()//main letter of the first
{//function body start
  int A, b, sum;//define variable
  cin>>a>>b;//input statement
  sum=a+b;//assignment Statement
  cout<< "a+b=" <<sum<<endl; Output statement
  return 0//////////////////////////////
function end

The function of this program is to find the sum of two integers a and b. Line 1th "//Seek the sum of two" is a comment line in which C + + stipulates that if "//" appears in a row, all content from the beginning of it to the end of the bank is commented.

If you enter from the keyboard at run time

  123 456↙

The output is

  a+b=579

Example 3
give two numbers x and Y, and ask for the large number in two. In this example, two functions are included.

#include <iostream>//preprocessing command
using namespace std;
int max (int x,int y)//define Max function, the function value is integral type, the form parameter x,y is integral type
{//max function body begins
  int z;//variable declaration, define variable z of this function as Integer
  if (x>y)
   z=x//if statement, if x>y, assigns the value of X to Z
  else z=y;//Otherwise, assigns the value of Y to Z
   return (z);//Returns the value of Z, takes back to the call by Max and
ends with the//max function
int main ()//main function
{//main function body start
  int a,b,m;//variable declaration
  cin>>a>>b;//input variable A and B value
  M=max (a,b);//Call Max function, assign the resulting value to M
  cout<< "max=" <<m<< ' \ n '; Output large m value return 0//////////////////////////////////
main function end

This program consists of two functions: 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 value)

Note that the two data you enter is spaced with one or more spaces, and cannot be separated by commas or other symbols.

In the above program, the Max function appears before the main function, so when you call the Max function in the main function, the compilation system recognizes that Max is the defined function name. If you swap the position of two functions, that is, write the main function first, then write the Max function, when the compiler main function encountered Max, the compiler can not know what the meaning of max, and therefore can not compile, according to error handling.

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

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

As long as a semicolon is appended to the end of the header of the called function, it becomes a function declaration on the function. The position of the function declaration should precede the function call.

Here's a C + + program that contains classes (class) and objects (object) to give the reader a preliminary idea of how C + + embodies object-oriented programming methods.

Example 4
C + + programs that contain classes.

#include <iostream>//preprocessing command
using namespace std;
Class student//declares a category named Student
{private
  ://The following is the private part of class
  int num;//private variable num
  int score;//private variable Score
   public://Following is the common part of the class
  void SetData ()//define the common function SetData
  {
   cin>>num;//Enter num value
   cin>> Score Enter value for score
  }
  void Display ()//define common function display
  {
   cout<< "num=" <<num<<endl;// Output num value
   cout<< "score=" <<score<<endl;//output score value};
//class declaration end
Student STUD1,STUD2; A variable that defines STUD1 and STUD2 as the student class, called object
int main ()//main function First
{
  stud1.setdata ();//Call STUD1 function
  of Object SetData Stud2.setdata (); Call the object stud2 SetData function
  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, which are called data members and member functions, respectively. Encapsulate a set of data in C + + with the functions that have permission to call the data, and form a data structure called class. In the above program, the data member Num,score and member function Setdata,display form a "class" type named Student. member functions are used to manipulate data members. That is, a class consists of a batch of data and a function that operates on it.

Class can embody the encapsulation of data and the concealment of information. In the above program, when declaring the student class, the data and functions in the class are grouped into two broad categories: private and public. Specify all the data (Num,score) as Private and all functions (Setdata,display) as public. In most cases, all data is specified as private to enable information to be hidden.

A variable with a "class" type feature is called an object.

The 18th to 24th line in the program is the main function.

The program runs as follows:

1001 98.5↙  (enter Student 1 's study number and achievement)
1002 76.5↙  (enter Student 2 's school number and grade)
num=1001 (output Student 1 's study number)
score=98.5  ( Output 1 of students)
num=1002 (output 2 student number)
score=76.5  (output Student 2 of the results)

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.