The first C + + program

Source: Internet
Author: User

Example 1.1, the first C + + program in this tutorial, outputs a single line of characters: "This is a C + + programs." The procedure is as follows:

    1. #include <iostream> //Include header file iostream
    2. Using namespace STD; //Using the namespace std
    3. int main( )
    4. {
    5. cout<<"This is a C + + program." ;
    6. return 0;
    7. }

The

Prints the following line of information on the screen at run time:
This is a C + + program.
The
uses main to represent the name of the "main function". Each C + + program must have a main function. The function of int in front of main is to declare the type of the function to be integral type. The function of line 6th of the program is to return a value of 0 to the operating system. If the program does not perform properly, it will automatically return a non-0 value to the operating system, typically-1. The body of the

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

Look at line 1th of the program, "#include <iostream>", which is not a C + + statement, but rather a preprocessing command for C + +, which begins with "#" and differs from the C + + statement, with no semicolon at the end of the line. #include <iostream> is a "include command" that is used to include the contents of the file iostream in the program file where the command is located, instead of the command line. The role of file iostream is to provide the program with some information that is required for input or output. Iostream is a combination of the I-o-stream 3 words, which in its form can be known as the "input and output stream" meaning, because such files are placed at the beginning of the program unit, so called "header file" (head files). When the program compiles, all pre-processing commands are processed, the specific contents of the header file are replaced by the #include命令行, and then the program unit is compiled as a whole. Line 2nd of the

Program, using namespace std; "means" using 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; "As a declaration, indicating that you want to use the contents of the namespace Std.

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

Example 1.2 asks for the sum of the two numbers of A and B. The following programs can be written:

  1. Sum of two numbers (the bank is the comment line)
  2. #include <iostream> //Pre-processing commands
  3. Using namespace STD; //Using the namespace std
  4. int main( ) //main function number first
  5. { //function body start
  6. int a, b, sum; //define variables
  7. CIN>>a>>b; //Input statement
  8. Sum=a+b; //Assignment Statements
  9. cout<<"a+b="<<sum<<endl; //Output statement
  10. return 0; //If the program ends normally, return a 0 value to the operating system
  11. } //function End

The purpose of this procedure is to ask for sum sums of two integers a and b. Line 1th "//Two sum" is a comment line, C + + specifies that if "//" appears in a row, the entire content from the beginning 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 1.3" gives two numbers x and Y, for the largest of the two numbers. In this example, two functions are included.

  1. #include <iostream> //Pre-processing commands
  2. Using namespace std;
  3. int max(int x,int y) //define Max function, function value is integer, form parameter x, Y is integral type
  4. { //max function body begins
  5. int z; //variable declaration, defining the variable z used in this function as an integral type
  6. if(x>y)
  7. Z=x; //if statement, if x>y, assigns the value of X to Z
  8. Else z=y; //Otherwise, the value of y is assigned to Z
  9. return(z); //Returns the value of Z, brought back to the call by Max
  10. } //max function ends
  11. int main( ) //main function
  12. { //main function body start
  13. int a, b, M; //Variable declaration
  14. CIN>>a>>b; //input variables A and B values
  15. M=Max(a, b); //Call the Max function to assign the resulting value to M
  16. cout<<"max="<<m<<'\ n'; //output large number of M values
  17. return 0; //If the program ends normally, return a 0 value to the operating system
  18. } //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 for A and B)
Max=25 (value of output m)

Note that the two data you enter is separated by 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 a defined function name. If the position of the two functions, that is, write the main function, and then write the Max function, when the compiler main function encountered Max, the compilation system cannot know what the meaning of max, and therefore can not compile, according to error processing.

In order 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 Max function {   int z;   if (x>y) z=x;   else z=y;   return (z);}

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

The following is a C + + program containing classes (class) and objects (object), intended to give the reader a preliminary understanding of how C + + embodies the object-oriented programming approach.

"Example 1.4" contains a C + + program for a class.

 #include <iostream>//preprocessing command using namespace Std;class student//declares a class with the class name student{private:/// The private part of the class int num;  Private variable num int score; Private variable Score Public://The following is the common part of the class void SetData ()//define a common function SetData {cin>>num;//Enter the value of Num CIN>&G  T;score; Enter the value of score} void display ()//define common function display {cout<< "num=" <<num<<endl;//Output num value cout << "score=" <<score<<endl;//output score value};}; The declaration of the class ends student stud1,stud2;  Defines STUD1 and STUD2 as variables of the student class, called the object int main ()//main function number first {stud1.setdata ();  Call the SetData function of the object Stud1 Stud2.setdata ();  Call the SetData function of the object Stud2 Stud1.display ();  Call Object Stud1 the Display function stud2.display (); Call Object Stud2 the display function return 0;} 


There are two types of members in a class: Data and functions, called data members and member functions, respectively. In C + +, a set of data is packaged together with functions that have permission to invoke the data to form a data structure called class. In the above program, the data member Num,score and the member function Setdata,display form a class type named student. member functions are used to manipulate data members. In other words, a class consists of a batch of data and functions that manipulate it.

Classes can represent 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 divided into two categories: private (private) and public (common). The entire data (Num,score) is specified as private, and all functions (Setdata,display) are specified as public. In most cases, all data is designated as private to enable information concealment.

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 number and score)
1002 76.5 (Enter Student 2 's number and score)
num=1001 (output student number 1)
score=98.5 (output 1 of the student's score)
num=1002 (output student number 2)
score=76.5 (output 2 of the student's score)

The first C + + program

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.