Lesson OneYou have two choices when you are deciding to learn C++, "Do I get my compiler from online, or do I purchase it?" Purchasing may be a bit expensive, unless you come upon a deal, but you are guaranteed to get the full program. Online, however, may be cheaper, especially if you can find a compiler for free, but you are not guaranteed to get the whole program. Which ever you choose, once you get everything loaded, you are ready to get started. The first part of your code, no matter what programming language you are using, should be comments. Comments are used to tell people that you are the editor, the name of the program, and what the program does. You do not see these comments when you compile and run the code. Comments are added by typing // before each line. Here are some comments: The next part of our programs will be the preprocessor. The preprocessor is a program that runs before the compiler to handle what source code the compiler sees as input. This is achieved by copying what is in the include directives. This is what our preprocessors will look like: The next line, using namespace std;, means our program will be using objects that are named in a special region name std that we will find useful in writing our programs. Here's where our programs go into action. The next part is the main() function, which specifies the type of result the function will be returning. This is the first called function when the program is compiled and executed. It carries its arguments in the parenthesis that follow. This statement is followed by an opening { curly brace. Within the body of the main() method is the lines of code that will make our program work. Normally, we will be starting our programs out expressions. To show how this program works, we will be using the cout object. The cout operator << inserts everything after it into the named stream. At the end of cout statements is the manipulator endl;, which inserts a new line character. Before we close the main() with a closing }, we want to add a return value of zero to tell the main to close up all programs and run the program, provided there are no errors. Now that we have all this out of the way, we can start writing our programs. Our programs might look like the one below: Notice that there are two more things that we haven't mentioned. One of them being the float Price; statement. This is the definition of "Price" being a floating-point (real, decimal) number. The other statement, cin >> Price, accepts the number that the user types in. The >> is considered an extractrion operator because it extracts the input from the user. You can assign a value to a variable by using the = equals sign. This does not mean that the variable is equal to the value. It means that when the variable is called, so is the value (we will learn how to make a variable equal to a value later on in the lessons). The program above is written in good programming style. It is important that you write your programs this same way to make it easier for both you and anyone after you to edit your programs. C++ uses short, int, and long as object types to hold integer values. A short is stored in 8-bits, int is stored in 16-bits, and long is stored in 32-bits. Characters are represented by the object type char. In most computers, characters are from ASCII (American Standard Code for Information Interchange) character set. However, IBM uses the EBCDIC (Extended Binary Coded Decimal Interchange Code) character set. Floating-point numbers are represented by three object types: float, double, and long double. Float numbers are stored in 32-bits and doubles are stored in 64-bits. These numbers are used where a greater degree of precision is required. A string constant is any amount of characters enclosed in double quotes. String constants end with a special character called an escape mechanism. The table below shows some of the escape codes:
Any string constant that is just " " double quotes is considered a null string. Here's a tip: in C++, an object is considered to be a region of memory that contains a set of values and operations. The type that the object is is up to the programmer. Always remember to give an object an initial value when you define it unless it is immediately given a value through an >> extraction operation. Here's another tip: when writing an interactive program, it is useful to echo back to the user what they typed so they know what they typed and are given the opportunity to change their minds. Also, make sure you are placing your declarations near the point of use. Question: Wow!! C++ sounds like a bunch of rules and technical terms. Is there anything about C++ that is simple? Answer: If C++ were too simple, it wouldn't be one of the most popular programming languages. However, if you open your eyes to the next lesson, it won't seem so overwhelming. If you have any questions or comments about this lesson, feel free to click here and leave me your message. |