Вы находитесь на странице: 1из 7

Getting Set Up - C++ Compilers

The first thing you need to do, before starting on the C + +, is to ensure that you have a compiler. What compiler, you ask? A compiler converts a program you write to an executable that your computer iscan correctly understand and execute. If you take the course, you may have one administered through your school. If you start out on your own, your best bet is to use Code :: Blocks with MinGW. If you are on Linux,You can use g + +, and if you are on Mac OS X, you can use Xcode

Intro to the C++ Language


A C + + program is a collection of commands that tell the computer to do "something". Collection of orders is usually called C + + source code, source code or just the code. A good command "function" or "keyword". Keyword is a block basic building blocks of language, while the function is, in fact, usually written in the form of simple functions - you'll see this in our first program, below. ? (Confused Think of it less as a framework for a books; line may indicate each chapter in this book, each chapter may have its own line, made up of sections each section may have its own line, or it may have all. written details) Happily., C + + provides the functionality of the general and the keywords you can use. But how does a program actually begin? Each program in C + + has a function, is always named main, which is always called when your program first starts up. Of the primary, you can also call the functionother functions if they were written by us or, as mentioned earlier, provided by the compiler. So how do you get access to prewritten functions? To access the functions that come standard with compiler, you include a header with the directive # include. What this does is effectively take everything in the header and paste it into your program. Let's look at the work program:
#include <iostream> using namespace std; int main() { cout<<"Qrembiezs Intruder was here!\n"; cin.get(); }

Let's take a look at the elements of the program. The # include is a "preprocessor" directive that tells the compiler to put the code in the header called iostream into our program before actually making the executable. by including header file, you get access to different functions. For example, the function requires iostream court. Follow include the statement, "using namespace std; '. This line tells the compiler to use the group functions is part of the standard library (std). By including this line at the top of the file, you allow the program to using functions like a court. The semicolon is part of the syntax of C + +. This tells the compiler that you at the end of the command. You will see later that the semicolon is used to end most commands in C + +.

The next important line is int main (). This line tells the compiler that a function called main, and that function returns an integer, hence int. The "curly braces" ({and}) signal the beginning and end of the function and other code blocks. You can think of them as the meaning of BEGIN and END. The next line of the program may seem strange. If you have programmed in other languages, you may wish to print will be a function used to display text. In C + +, however, the courts are used for object display text (pronounced "C out"). It uses the << symbols, known as "insertion operator", to show what is the output. cout << result in the function call with the following text as an argument to the function. citing informed compiler that you want to output a string literal is. The sequence '\ n' is actually treated as a single character stands for a new line (we'll discuss this in more detail later). This moves the cursor on your screen to the line next. Again, notice the semicolon: is added to the last line, such as calling a function, in C + +. The next command is cin.get (). This is another function call: it reads the input and expect the user to press the button again. Many compiler environments will open a new console window, run the program, and then close the window. This command makes the window from closing because the program is not finished because you are waiting for press enter. Including the line that gives you time to see the running program. Having achieved the primary end, clamp shut, our program will return the value 0 (and integer, then why we say the main to return int) for the operating system. The return value is important because it can used to tell the OS whether our program succeeded or not. A return value of 0 means success and returned automatically (but only for the main, other functions require you to manually return value), but if we want to return something else, such as 1, we have to do with return statement:
#include <iostream> using namespace std; int main() { cout<<" Qrembiezs Intruder was here!\n"; cin.get(); return 1; }

Final closing brace of the function. You should try to compile and run this program. You can cut and paste the code into the file, save it as a cpp file.

An Aside on Commenting Your Programs


When you learn to program, you also have to start learning how to explain your programs (for yourself, if no one else). You do this by adding a comment to the code, I would use it frequently to help explain the sample code. When you tell the compiler part of the text comments, he will ignore it when running the code,allows you to use the text you want to describe the real code. To make a comment make use of the sign / /, which tells the compiler that the rest of the line is commented out, or / * and then * / to mark the that all of the comments. Specific compiler environments will change the color of the comment area, but some do not will. Be certain not accidentally comment out code (that is, to tell the compiler part of your code is a comments) that you need for this program. When you learn to program, it is useful to be able to comment section of the code to see how the output is affected.

User interaction and Saving Information with Variables


So far you have learned how to write a simple program to display information typed by you, programmer, and how to describe your program with the comment. That's fine, but how it interacts with your users? Fortunately, it is also possible for your program to accept input. You use the function known as cin, and followed by the insertion operator >>. Of course, before you attempt to receive input, you must have a place to store input. In programming, input and the data stored in the variable. There are several types of variable that stores various types of information (eg numbers Versus letters), you tell the compiler when you declare a variable, you must include the data type and its name variable. Several basic types include char, int, and float. A variable of type char stores a single character, variables of type int store integers (numbers without decimal), and variable of type float store numbers with decimal places. Each type of variable - char, int, and float - is each keyword that you use when you declare a variable. What's with all these variable types? Can sometimes be confusing to have some type of variable when it seems like some kind of variables are redundant (why when you have an integer number float). Using the right type of variable may be important to make your code readable and for efficiency - some variables require memory more than others. In addition, because of the way the numbers are actually stored in memory, the float is "exact", and should not be used when you have to keep a "proper" value of an integer.

Declaring Variables in C++ To declare a variable that you use the syntax "<name> type:". Here are some examples of variable declarations:
int x; char letter; float the_float;

It is permissible to declare multiple variables of the same type on the same line, each must be separated by commas.
int a, b, c, d;

You may have seen that the variable declarations are followed by a semicolon (note that this is a procedure that the same used when you call a function). Common Errors when Declaring Variables in C++ If you try to use a variable that you have stated, your program will not compile or run, and you will receive an error message informing you that you have made a mistake. Typically, this is called Undeclare Variable

Case Sensitivity
Now is a good time to talk about an important concept that can easily throw you off: sensitivity of the case. Basically, in C + +, if you use uppercase or lowercase letters p. Cat and kitten word meaning different for the compiler. In C + +, all language keywords, all functions and all variables are case sensitive. Differences in variable declarations between you and the use of variables is one reason you might get error variable

Using Variables
Ok, so you now know how to tell the computer about the variables, but how to use them? Here is a sample program that demonstrates the use of variable
#include <iostream> using namespace std; int main() { int thisisanumber; cout<<"Please enter a number: "; cin>> thisisanumber; cin.ignore(); cout<<"You entered: "<< thisisanumber <<"\n"; cin.get(); }

Let's take a break and check this program line by line. Int declare thisisanumber keyword to an integer. Function cin >> to read the value into thisisanumber, the user must press enter before the number is read by the program. cin.ignore () is a function Another read and discard characters. Remember that when you type input into the program, it takes the enter as well. We do not need this, so we throw it away. Note that the variable is declared integer, if the user tries to type a decimal number, it will be truncated (ie, components of decimal numbers will be ignored). Try typing in sequence of characters or a decimal number when you run the example program; response will vary from input to input, but in case it is not too pretty. Note that when printing out a variable quotation marks are not used. Is there any sign of quotation, the output will be "You Entered:. Thisisanumber" The lack of quotation marks tells the compiler that there are variables, and because the program should check the value of a variable that can change the variable names with variables when running the output function. Do not be confused with the inclusion of two separate insertion operators on one line. Including the operator insertion in one lines perfectly acceptable and all output will go to the same place. In fact, you must separate string literals (strings enclosed in quotation marks) and variables by giving each its own insertion operators (<<). trying to put two variables together with only one << will give an error message, do not try it. Do not forget to end functions and declarations with a semicolon. If you forget the semicolon, the compiler will give an error when You try to compile the program.

Changing and Comparing Variables


Of course, no matter what type you use, the variable is not interesting without the ability to modify them. Some operator used with variables are as follows: *, -, +, /, =, ==,>, <. * Multiply, which - reduced, and the + adds. This is of course important to realize that to change the value of the variable in the program was somewhat important to use the equal sign. In some languages, the equal sign compares the value of the values of the left and right, but in C + + == used for the task. The equal sign is still very useful. This sets the left input to the same sign, which should be one, and only one, variable equal to the value on the right side of the equal sign. Operators that perform mathematical functions should be used in the right side of the equal sign in order to assign the result to a variable on the left side.
a = 4 * 6; // (Note use of comments and of semicolon) a is 24 a = a + 5; // a equals the original value of a with five added to it a == 5 // Does NOT assign five to a. Rather, it checks to see if a equals 5.

Other forms of the same, ==, not a way to assign values to variables. Instead, it checks to see whether the variables are the same. This is useful in other areas of the C + +, for example, you will often use == in constructs such as conditional statements and loops. You can probably guess how <dan> function. They are larger than and less than operators.
a < 5 // Checks to see if a is less than five a > 5 // Checks to see if a is greater than five a == 5 // Checks to see if a equals five, for good measure

Comparing the variables are not really useful until you have several ways to use it

Tutorial The basics of C++ Lesson 1


Special Thanxs To : God Allah My Family

Special Thanxs to My Team : Prabujoyoboyo Loiz Xempaxping Thanxs to Whitehat ./Blacknote Cancer Linuxer46 Shamus Cybermuttaqin See u in Lesson 2 Basic Of c++ (I Hope I can write again)

Вам также может понравиться