The first knowledge of C + +, to introduce a few I used to do the practice of writing a few small programs, are c++primer above the exercises, share to everyone.
EG1: writes a program that uses the decrement operator to print integers between 10 and 1 in descending order in a loop.
The code is as follows:
#include <iostream> #include <cstdlib>using namespace Std;int main () {int I=10;while (i) {cout<<i;--i;} System ("pause"); return 0;}
Operating Result: 10 9 8 7 6 5 4 3 2 1
Childe Tip: Because I added a using namespace STD before the main function, this sentence is used directly to the cout. Before you add it, remember to write std::cout<< "i=" <<endl; this way. Because the prefix std:: Indicates that cout and Endl are defined in the namespace named Std (namespace) . Namespaces can help us avoid accidental name collisions and conflicts that result from using the same name in the library. All names defined by the standard library are in the namespace Std. This is not in the C language. There is a side effect of using the standard library through namespaces, and when we use it we have to explicitly explain that we want to use names from namespaces, such as std::cout.using, which can no longer be explicitly stated after a namespace declaration. Both methods are available, but it is recommended that you make a statement, because you can write a lot of things less. 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
EG2: writes a program that prompts the user to enter two integers, printing out all integers in the range specified by the two integers.
The code is as follows:
#include <iostream> #include <cstdlib>using namespace Std;int main () {int num1=0;int num2=0;int ret=0;cin >>num1>>num2;while (num1<=num2) {cout<<num1<< "; num1++;} System ("pause"); return 0;}
Run Result: 3 8
3 4 5 6 7 8
Childe's tip: We used the output operator (<<) to print information on standard output in the above program. The << operator receives two operands: the left operand must be an Ostream object, and the operand on the right is the value to be printed. In this example, our output statements use two << times. Because this operator returns the operand to its left, the result of the first operator becomes the operand to the left of the second operator, so that we can connect the output request.
EG3: writes a program that reads a set of numbers from CIN and outputs its and.
The code is as follows:
#include <iostream> #include <cstdlib>using namespace Std;int main () {int Sum=0;int val=0;while (cin>> val) {sum+=val;} cout<< "sum=" <<sum;cout<<endl;system ("pause"); return 0;}
Operating Result: 1 2 3 4 5 6 7 8 9 10
Sum=55
Although the above example is very simple, most of us have mastered the C language, but it can play a good role in practice, so that we are familiar with C + + operators, and input and output streams, experience the difference between the two.
Getting Started with C + + applet exercises