Lesson 1: The basics of C++ This tutorial series is designed for everyone: even if you've never programmed before or if you

have extensive experience programming in other languages and want to expand into C++! It is for everyone who wants the feeling of accomplishment from a working program.

What do I mean? C++ is a programming language--it will allow you to control your computer, making it do what you want it to do. This programming tutorial series is all about helping you take advantage of C++. Getting Set Up - C++ Compilers

The very first thing you need to do, before starting out in C++, is to make sure that you have

into an executable that your computer can actually understand and run. If you're taking a cou

own, your best bet is to use Code::Blocks with MinGW. If you're on Linux, you can use g tutorial. Intro to the C++ Language

compiler, such as Turbo C++, you'll need to read this page on compatibility issues.) If you h

A C++ program is a collection of commands, which tell the computer to do "something". T

Commands are either "functions" or "keywords". Keywords are a basic building block of the own outline, composed of sections. Each section might have its own outline, or it might functions and keywords

you'll see this in our very first program, below. (Confused? Think of it a bit like an outline fo

By including header files. cin. "using namespace std. The quotes tell the compiler that y .But how does a program actually start? Every program in C++ has one function.". The #include is a "preprocessor" directive that t actually creating the executable. If you have programmed in another langu however. the cout object is used to display text (pronounced "C out"). you. This line tells the compiler that there is a function nam }) signal the beginning and end of functions and other code blo The next line of the program may seem strange. The semicolon is part of the syntax of C++ The next important line is int main(). Let's loo Let's look at the elements of the program. I'm alive! Oh. This line tells the compiler to use a group of functions semicolon is used to end allow the program to use functions such as cout. always nam also call other functions whether they are written by u So how do you get access to those prewritten functions? To access those standard functions 1 2 3 4 5 6 7 8 9 } int main() { cout<<"HEY. using namespace std. #include <iostream> this does is effectively take everything in the header and paste it into your program. and Hello World!\n". you gain access to many differen the statement. It uses the << symbols call with the ensuing text as an argument to the function.get().

This command keeps that window f tim Upon reaching the end of main.character that stands for a newline (we'll talk about this later in more detail). using namespace std. and Hello World!\n". int main() { cout<<"HEY. cin. but if we wanted to return something else. our program will return the value of 0 (and value is important as it can be used to tell the OS whether our program succeeded or not. You should try compiling this program and running tutorial actually If you are takes not you using through Code::Blocks. This is another function call: it reads in input and expects Including that line gives you window. run the program. #include <iostream> The final brace closes off the function. creating you should a simple read the . such The next command is cin. A functions require you to manually return a value). It moves the c end of most lines.get().get(). you. 1 2 3 4 5 6 7 8 9 10 11 } return 1. and then close the window. the closing brace. I'm alive! Oh.

but some will not.is each a keyword that you use when you d What's with all these variable types? right variable type can be important for making your code readable and for efficiency--some Sometimes it can be confusing to have multiple variable types when it seems like some varia . when you the name of the variable. Several basic A variable of type char stores a single character.char. before you try to receive input. it is also possible for your program to Of course. int. you must have a place to store that input. why don't you try playing around with the cout An Aside on Commenting Your Programs As you are learning to program. which tells the compiler that the rest of the line is a comment. variables of type int store integers (numbers Each of these variable types . it is useful to be abl User interaction and Saving Information with Variables So far you've learned how to write a simple program to display information typed in by you operator what about interacting with your user? Fortunately.g. numbers versus letters). When you are learning to program. Be certain no comment) you need for the program. and float . you should also start to learn how to explain your program frequently to help When you tell the compiler a section of text is a comment. it will ignore it when running comment use either //. In pro variables which store different kinds of information (e. environments will change the color of a commented area.Once you've got your first program running.

your program will not be compi mistake. Usually. you might have seen that declaration of a variable is always function). all declaration and the use of the variable is one reason you might get an undeclared variable erro Using Variables Ok. float the_float. all language keywords.".are actually stored in memory. Common Errors when Declaring Variables in C++ If you attempt to use a variable that you have not declared. In C++. a float is "inexact". It is permissible to declare multiple variables of the same type on the same line. If you were watching closely. this is called an undeclared variable. Case Sensitivity Now is a good time to talk about an important concept that can easily throw you off: case sen words Cat and cat mean different things to the compiler. and should not be used when you need to s Declaring Variables in C++ To declare a variable you use the syntax "type <name>. Here are some variable declaration e 1 2 3 int x. so you now know how to tell the comp Here is a sample program demonstrating the use of a variable: . b. d. c. each one sho 1 int a. char letter.

int main() { int thisisanumber.ignore() is another function t be truncated (that is. Notice that w the output would be "You Entered: thisisanumber. cout<<"You entered: "<< thisisanumber <<"\n". cin>> thisisanumber. cin. but in no case is it particularly pretty.ignore(). cin. Keep in mind that the v response will vary from input to input.get(). cout<<"Please enter a number: ". We don't need this. the decimal component of the number will be ignored). Try typing in it takes the enter key too. } Let's break apart this program and examine it line by line. The keyword int declares thisisa must press enter before the number is read by the program.1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std." The lack of quotation marks informs the of the variable in order to replace the variable name with the variable when executing the ou . cin. so we throw it away.

Including multiple insertion operators on one line is perfectly acceptable and all enclosed in quotation marks) and variables by giving each its own insertion operators (<<). // (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.subtracts. it checks to often use == in such constructions as conditional statements and loops. variable equal to the value o ord Here are a few examples: 1 2 3 a = 4 * 6. The * multiplies. The other form of equal. which must be one. is not a way to assign a value to a variable. In some languages. Changing and Comparing Variables not try it. It is of course important to realize tha equal sign. no matter what type you use. it checks to see if a equals 5. Do not forget to end functions and declarations with a semicolon. variables are uninteresting without the ability to mo >. You can prob For example: 1 2 3 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. and the + adds. the equal sign compares the value of the left and right values should be used on the right side of an equal sign in left input to the equal sign. the .on one line. and only one. T the program. Rather. for good measure variables isn't really useful until you have some way of Comparing . ==. <. If you forget th Of course.

1 C. BEGIN and END D. . start() B. { } B. : D. ( and ) 4.com ebook. What is the only function all C++ programs must contain? A. 2. It place. C. Programs do not return a value. B. be sure to read through Cprogramming. ' 5. Buy Jumping into C++ today! Quiz: The basics of C++ If you haven't already done so. What punctuation is used to signal the beginning and end of code blocks? A.com's introduction to 1. -> and <C. main() D. Which of the following is a correct comment? . along with tons of sample code and practice problems. system() C. program() 3.If you enjoyed this tutorial. Jumping into C++. . check out the Cprogramming. What punctuation ends most lines of C++ code? A. 0 D. What is the correct value to return to the operating system upon the successful completion A. -1 B.

What punctuation ends most lines of C++ code? A. ( and ) 4. -1 B. 0 D. { Comment } 6. equal D. main() D. { } B. := B. system() C. float B. -> and C. Which of the following is the correct operator to compare two variables? A. What punctuation is used to signal the beginning and end of code blocks? A. ** Comment ** C. B. == Quiz: The basics of C++ 1. double 7. . Programs do not return a value. real C. What is the correct value to return to the operating system upon the successful completion of a program A. 2. . What is the only function all C++ programs must contain? A.A. /* Comment */ D. BEGIN and END D. program() 3. int D. 1 C. = C. */ Comments */ B. start() B. . Which of the following is not a correct variable type? A.

Which of the following is the correct operator to compare two variables? A. Which of the following is not a correct variable type? A. */ Comments */ B. { Comment } 6. /* Comment */ D. real C. ' 5. ** Comment ** C.C. equal D. = C. == . double 7. Which of the following is a correct comment? A. : D. := B. int D. float B.

Sign up to vote on this title
UsefulNot useful