"11 Long Vacation C + + 8th Talk" Eight days hands-on C + + first: Basic statement and data type __c++

Source: Internet
Author: User

is also the annual 11 holiday, because of various reasons, I need to quickly start C + +, but has been maintained before the study of a language must write a diary of the habit of writing notes to make me only look at the hands really uncomfortable. This is exactly the eight days of this content to be slowly written out. This process, I feel enough to get a person to start C + +, so I also try this type of article.

So, first of all, explain my own situation:

Before this, my main language is Java, I have more than a year of Java experience, but also registered on the SourceForge has a Java-based open source project. Have a certain object-oriented programming ideas. In addition, I learned C before 1.5 and had about 3000 lines of C code experience, but now I basically forget. Therefore, for learning C + +, the start is still very fast. So, I believe this article will be suitable for people like me. In addition, before in our school library has seen a seemingly is the C + + and Java comparison Learning Java book, but did not find the reverse book, have to know friends recommend ha.

  

Next, I'll talk about my eight articles : basic statement and data type function and the organization structure of the pointer program object oriented object-oriented advanced C + + generics and template programming exception handling mechanism use a small project to summarize

Here, I will use a simple Joseph ring small project to sum up the content of these days, here I'll use my knowledge of the fur of UML and unit test knowledge to build this project in VS2008, in fact, this Joseph ring is my first task of data structure experiment, Although the direct use of simple process programming less than 100 lines of code is done, why so troublesome to get so many messy things. It's simple, one, to learn C + +, a small project can also use C + + most of the features. Second, a house does not sweep why sweep the world, a small procedure can not be structured well, it is possible to make a big project.

Finally, my two weeks of learning C + + reference to the two books, the first is Stanley B. Lippman's <essential c++> and one is the money can write the second edition of C + + programming tutorial. To tell the truth, the former is very suitable for people with a certain background knowledge of C + +, nonsense, and all aspects of C + + has been mentioned. But the disadvantage is not very image of a project process to start from the most basic file, so if not clear header file, source code files in the case is still more difficult to find what you want immediately. And the latter is generally the textbook of the University, the book honestly, or I have seen the school hair book inside a better one, although more nonsense, but the whole book is quite logical, the content is very organized, in short, it is the directory to do well in line with the ISO standards, this let me very much like. And the content is also more suitable for beginners to get started.

So, the C + + road starts here.

program, or in accordance with the traditional HelloWorld start it:

#include <iostream> using namespace std; int main (int argc, int* argv) {cout << "Hello World";}

C + + HelloWorld, and Java are not much different. The function is to print out the Helloword. #include就是引用了, use <> to search by system or compiling environment, use "" is search in this catalogue, writing can be like this:

#include <cstdio> #include ". /test/test.h "#include"./no.h "

Note, however, that there is no space between that <>.

One More program:

#include <iostream> using namespace std; int main (int argc, int* argv) {int var1;//Declare a variable of type int, where int and Longint are 4 bytes of char var2, var3= ' 1 ';//Declare a variable of two char types, and The second declared variable is ' 1 ' (ASCII), where Char is a double test=1.0 of 1 bytes; Some descriptions of floating-point numbers//In addition, this annotation format is also a lot of people like//================================================================// Declare a double-precision floating-point variable and assign a value of 1.0, commonly, float is 4 bytes,//indicates ±3.4x10^38, accurate to 7 digits after decimal (10),//Double is 8 byte, can represent the month ±1.8x10^308, accurate to 15 digits after the decimal point There is also a long double that is 10 bytes, accurate to 19-bit CIN >> var1 after the decimal point; Waits for the user to enter the data, and assigns the input data to the VAR1. If you must know, usually the CIN cout is considered to be a see in, and so on, so read. cout << "Your type is" << var1 << Endl; The continuous << number is used to separate the parts, and the final Endl represents a newline. VAR1 + 1; It doesn't really work here. Just compute a var1 value in this line plus a 1 result. var1 = var1 + 1; Here the result of VAR1 + 1 is assigned to VAR1. Note that assignment, not judgment, is not equal. VAR1 = 5; To determine whether the value of var1 is equal to 5, the result of this line is a logical true or false value Var1 = 5 * 3; Subtraction all the same. There is also a% of the meaning of modulo. The compiler will scold you if you want to save 0. A description of expressions in C + +. ===================================================//Usually an expression of C + + var1 = 3*3; the var1 on the left side of such a sentence is one//left value (l-value), 3x on the right. 3 of the resultsis a right value (R-value), not around//interchange, the left value must be a variable or expression, the right value can be any legitimate things. The left and right are relative, such as Test = VAR1; Here the var1 is turned to the right-hand value ... Var1 + 2; This sentence is equivalent to var1 = var1 + 2, in fact, only = = This operator has this effect. Of course the corresponding also has-=,*=,/=,%=, the last one is to the right of the number modulo, such as//VAR1 is 5%=2 result is 5/2 of the remainder 1. Var1 + +; This classic, if you declare a variable called C, you have C + +. The idea of this operator is to increase the var1 by 1, which is equal to VAR1 + + 1; var1= var1 + 1; This is the result, but in fact there is a difference. --that's since minus 1. About + +,--description of the operator//==========================================================//You can actually write var1++; can also write ++var1; The difference between the two ways is that the former first take the value of var1 to use, the use of the 1, the latter is the first from the increase//again to use, for example, first assume that there is var1==3, there is an int variable t, then T = var1++; The result of the execution of this sentence is t==3,var1==4. and t= ++var1 The result is t==4,var1==4. -the operator is the self reduction, the effect is the same. Next is the floating-point and shaping direct conversion float f1 = 3.333f; Give the value of float type to F1 3.333 float F2 = 3.333; Converts a double of 3.333 to a float type, given F2, you guessed right, the direct write decimal will be treated as a double type float f3 = 2.33e+8; The 2.33x10^8 to F3, here is the scientific notation, the front of the 2.33 is the 2.33,e+8 of 8 times 10 double d1 = F1; The value of the F1 will be converted to the double type to give the D1 long double ld1 = 133.3L; Long double ld2 = 22.3; The same 22.3 was converted to a long double type. Next is some other type bool A = true; BOOL B = false; BOOL A =3; Remember not 0 as true. However, the value of a is 1, bool a = A-1//result is false, because A==1 enmu week{Mon, Tue, Wed, Thu, Fri, Sat, sun=11}; Note the above;, this is the enumeration type, which defines the variable week of the enumeration type with the following constants. The default is to start from 0, you can also make your own. Like it's up there. Sun is defined as 11//Note Here is the name of the curly brace representing the integer value. The use of the time can be used directly inside the enumerator name//such as a = = Mon. }

Through this section of the basic already know to assign value and beginner commonly used cin,cout.

Let's look at some of the other BASIC programming statements below:

#include <iostream> using namespace std; int main (int argc, int* argv) {bool a= false; int i1= 0;//============================================================== ==========//if statement if (a) {cout<< ' A is true ' <<endl;//If A is true, execute the statement inside the curly braces, and if there is only one sentence, you may not write curly braces. else {cout << "A is false" <<endl;//If A is false, execute this statement}//a where you fill in a condition, you can change to I1, this will be not 0来 judge true or false. You can also write a>3//You can also change to a = = False && I1 ==1, this && operator is an operator representing the "and" relationship,//There is a so-called short-circuit calculation, that is, &&, As long as the left one is false this sentence is false, so the right//direct skim, the same | | Operators represent "or" relationships, as long as the left is true, the sentence is true, the right side is//Don't forget. This is often a skill in programming. ----------------------------------------------------------------------//conditional operator, unique ternary operator (A&GT;5)? cout<< "yes": cout<< "no"; You can read this: the expression in parentheses is true, it really executes: the sentence on the left, or on the right. =======================================================================//switch Statement switch (IL) {case 1:cout< < "I1==1"; Break I1 = = 1 O'Clock execution words 2:cout<< "il==2"; I1 = = 2 o'clock execution words default:cout<< "end"; If the value of the i1 is not defined above, execute the sentence///offIn the description of the switch//======================================================//This sentence more said, first is the switch behind the parentheses inside the statement,//It only allows the result is integral type, Character type, or enum type. The position of the 1,2 behind the case must match the result. Of course, you didn't specify to execute the default. In addition, here is quite a jump, when the switch after the value of the time,//jump to the corresponding case line to start execution, so we have to use a break to make a situation when the completion of the time to stop the execution, If you do not write a break, it will always execute to the switch corresponding to the flower//parenthesis End or it encountered the break place//such as the above statement if it is i1==2 print: i1==2 end//----------------------------- ----------------------------------------- //==================================================================== = =//below describes the loop statement. while (a) {cout<< ' A is True ' <<endl;}//The sentence is printed as long as it is true, which means a dead loop int i=1; while (a) {i++-if (i==2) continue; if (i==5) break; cout<< "A is True" <<endl;}//This is interesting, each cycle I will increase by 1, the key in that two if//its meaning Thinking is, when the i==2 is executed continue//when I==5 executes a break, otherwise print a "a" is true,//So, what does continue and break mean? In fact, continue says to skip this round loop and continue the next round of loops,//Therefore, when i==2, it will not print. And the break indicates the end loop. So when i==5, this while loop ends. So you will print only 4 A is true;--------------------//There is also a Do While statement do{cout<< "test" <<endl;} while(a)//the interpretation of this statement is that each round cycle first executes the sentence in a do, and then judges//If A is true, then continue the next cycle, or jump out. In addition, it is important to note that if you use a while inside the variable must be defined in do outside. //--------------------------------------------------------------------- //====================================== ===============================//for loop for (int j=0; j< 5; j + +) {cout<< j<< Endl;}//for inside the semicolon divides it into three parts, from left to right The first part//is for the fact that the condition, only at the beginning of the loop into the first execution//j<5 is the termination condition. That is, when J becomes 5, it stops the loop. Each round loop executes the rightmost state//correction statement immediately after the cout within the loop, and here is the J 1, so this loop will print out the 0,1,2,3,4. Note that the loop termination statement is judged every time, so//If you start with a ratio of 5, the loop body of the loop is not executed. //--------------------------------------------------------------------- }

Well, with the basics above, you can already write a very simple program, like I have a small program that prints a diamond asterisk:

#include <iostream> using namespace std; int main (int argc, int* argv) {int no;//diamond maximum number of stars cout<< "Please enter a diamond with a maximum number of asterisks" <<endl; cin>> no; for (int i=0; I&L T;no; i++) {if (2* (i+1)-1) <= No) {for (int j=0; j< (no/2-i); j + +) {cout<< "";} Blank, number (no/2-i-1) for (int x=1; x<= (2* (i+1)-1); x + +) {cout<< "*";} The asterisk in the upper half is typed (2* (i+1)-1)} else {for (int j=0; j< (no/2-(No-i-1)); J + +) {cout<< "";} Play a blank for (int x= (2* (no-i)-1); x>0; x--) {cout<< "*";} Play the lower half of the asterisk} cout<<endl; System ("pause"); Let the program pause and display Press any key to continue return 0; Note: This program obviously has better wording and is interested in you can study it yourself}

When I entered the 15, the result of the program execution was as follows:

* * * * ******* * * * * Press any key to continue ...

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.