1 / 165

C++ Boot Camp

C++ Boot Camp. A review of your first C++ class. The Skeleton. #include <iostream.h> void main ( ) { // Go ahead and memorize this. // It has to be there! } // Note: this should go in a .cpp file. Documenting Your Programs.

Download Presentation

C++ Boot Camp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Boot Camp A review of your first C++ class

  2. The Skeleton #include <iostream.h> void main ( ) { // Go ahead and memorize this. // It has to be there! } // Note: this should go in a .cpp file

  3. Documenting Your Programs • This is called “commenting”, and the computer ignores this when compiling. • There’s 2 ways to do it in C++! //This is for a single line comment. /* This is for a paragraph of comments and can go on and on. It doesn’t matter until you see the */

  4. Printing to the Screen • Use cout to do this (pronounced “see-out”) • cout can print out just about anything • Variables of most data types • Multiple things at once • Special characters like ‘\n’ (newline), etc. • Print multiple things with additional <<‘s • Print out endl to drop to next line

  5. Cout • Some examples: cout << “This is a sample string”; cout << my_int << endl; cout << “C++ scares me”; cout << “Hello” << “ world!” << endl;

  6. Getting Input from the User • Use cin (pronounced “see-in”) • Read into variables (later) • Example: // Declare a variable int myInt; // Load it with information from the keyboard cin >> myInt; // Print it out cout << “myInt is “ << myInt << endl;

  7. Cin • More examples: cin >> my_int; cin >> age >> name;

  8. Chaining stream Operations • There is no limit to the number of things that you can print & read • Simply chain them together like this: cout << “First item” << “second item” << endl;

  9. Special Escape Sequences • Added within strings • Performs “special character” \n newline \r carriage return (not common) \t tab \a alert (beep) \\ backslash \’ single quote \” double quote

  10. Your First C++ Program #include <iostream.h> void main ( ) { cout << “Hello World” << endl; } // Note: yes, this should be put into a .cpp file

  11. Rules about Syntax • Where do the ;’s go? • Semicolons come after single statements • Similar to a period at the end of a sentence • Where do the { and } go? • Creates a block of text for multiple single statements • { tells the computer to begin a section • } tells the computer to end a section • Can be nested (or inside of each other) • Let’s go back to our first c++ program

  12. Your First C++ Program #include <iostream.h> void main ( ) { cout << “Hello World” << endl; } Begin the main End the main End a single statement

  13. Compiling Your Program • You’ll use one of two things: • The command-line: g++ (on Unix) • Your IDE (like Visual C++) • When you compile, you’ll have one of two things happen: • It compiles • It doesn’t • If it doesn’t compile, you have syntax errors

  14. Knowing When to Compile • As a rule, the less comfortable you are, the more you should compile • This isolates the errors • As a rule, compile: • After you type in the skeleton • After every 5 or 10 lines of new code • Don’t become dependent on the compiler!

  15. Overview of Variables • Computer programs manipulate data • There is a need to store data • Variables: • Represent a named cell in the computer’s memory • Used to hold information • When you declare a variable: • You open up some of the computer’s memory • You’re giving the variable an identifier (name) • You only do this once per variable

  16. Data Types • A data type describes the kind of information a variable can hold • Some data types are built into the language • Some data types we define ourselves (later) • Different Forms: • Primitive data types • Complex data types

  17. Primitive Data Types • These are the simplest kinds of data types • Built-in • Whole numbers: • short (relatively small numbers) • int (larger numbers) • long (VERY large numbers) • Number of bytes these take up depends on compiler and OS!

  18. Primitive Data Types • Decimal numbers: • float (less precise than a double) • double (more precise than a float) • Precision means how many numbers come after the decimal point • Others: • char (holds characters like ‘a’, ‘A’, ‘1’, ‘ ‘) • bool (holds only a true or false value)

  19. How to Declare a Variable(of any type) • Format: <data type> <variable name>; • Variable name you can choose almost anything • Examples: byte age; float gpa; String name; char letterGrade; Computer's Memory age gpa ? ? name letterGrade ? ?

  20. How Big is it: sizeof( ) • sizeof( ) will tell you how much memory something takes up in bytes • Usage: int myInt; cout << sizeof (myInt) << endl;

  21. Reserved Words in C++ • These words are used by C++ for special reasons, so you can’t use them • List: • auto, bool, break, case, catch, char, class, const, continue, default, do, double, else, enum, extern, float, for, friend, goto, if, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, void, volatile, while

  22. Legal Variable Names • Can’t be a reserved word • Can’t begin with a number • Can’t contain special symbols • Like #, @, !, ^, &, /, ), or SPACES • Exceptions: underscore _, and dollar sign $ • Examples: byte $theValue; // legal char test_value; // legal double double; // not legal int rum&coke; // not legal boolean true or false; // not legal for two reasons!

  23. Literal Values • Values that are constant • Examples: • String literal – “Hello, World!” • Whole number literal – 17 • Decimal literal – 3.14159 • Character literal – ‘V’

  24. Initializing Variables • Initializing a variable means giving a variable some kind of starting value • Assignment operator is = • Copies the information on the right into the variable on the left and must be compatible • Can be a literal OR another variable! • Examples: int age; age = 15; char letterGrade = ‘B’; char yourGrade = letterGrade;

  25. Why Size Matters • Each data type takes up a different amount of the computers memory (in bytes) • Can’t put a larger data typed variable into a smaller data typed variable: short s = 5; long l = 5; long l = s; short s = l; long long long short short short

  26. Operators • + adds two variables/literals together (including Strings) • - subtraction • / division • * multiplication • % modulus (remainder) • ( and ) follows standard math rules • Example: int result = ( (4 % 3) * 5) + 1; // result gets 6

  27. Type Casting • This occurs when you want to put something larger into something smaller • Can possibly lose precision or value • Has format: <smaller var> = (<smaller data type>)<larger var>; • Example: long myLong = 17; short myShort = (short)myLong; // We are squeezing myLong into myShort!

  28. Conditionals and Boolean Expressions

  29. A Flow Diagram Start Blame it on someone else Lay low and keep hidden Did you break anything? Yes Yes No Have your parents asked about it? Yes Are you lying? No Are you passing your classes? No No Ask for $$ Yes End

  30. The Boolean • A bool is a something that resolves to true (1) or false (0) • You can get booleans several different ways • Simple • Complex • These booleans will be used (later) to make decisions to change your program’s behavior

  31. Relational Operators(for primitive data types) • Relational Operators > greater than < less than == equals ! Not != not equal >= greater than or equal <= less than or equal • Notice that all of these return us a true or false value!

  32. Relational Operator Examples • Literal Example 5 > 3 // true 6 != 6 // false (4 <= 2) // false ‘c’ != ‘b’ // true !0 // true (prints out 1) • Variable Example int num1 = 5; short num2 = 7; bool result = num2 > num1; //Note: result is now true

  33. && and || Operators • These operators check for multiple conditions • && (AND) needs both the left and the right to be true in order to return true • || (OR) needs either one (or both) to be true to return true • Can shortcut if necessary • Examples: ( (6 > 5) && ( ‘c’ == ‘b’) ) // false ( (6 > 5) && ( 7 < 9) ) // true ( (6 > 5) || ( ‘c’ == ‘b’) ) // true ( (6 > 6) || ( ‘c’ == ‘b’) ) // false

  34. Now Let’s Do Something! • Using the if statement, we can decide whether or not to execute some code! • Has format: if (<boolean value>) { // all the code that’s in here will only execute // if and only if the boolean above is true }

  35. “if” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if cout << “B”; } // end main

  36. “if” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if cout << “B”; } // end main Output A B

  37. “if” Example(Part II) #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if cout << “B”; } // end main

  38. “if” Example(Part II) #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if cout << “B”; } // end main Output B

  39. The “else” statement • if has a counterpart – the else statement • If the if clause didn’t execute, the else clause will! • Has format: if (<boolean value>) { // statements that execute if the boolean is true } else { // statements that execute if the boolean is false } • Notice only one set of statements executes, no matter what!

  40. “else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main

  41. “else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main Output B

  42. “else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main

  43. “else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main Output A

  44. else if’s • Selecting one from many • As soon as one is true, the rest are not considered • Has format: if (<boolean value>) { // statements that execute if the above boolean is true } elseif (<boolean value>){ // statements that execute if the above boolean is true } elseif (<boolean value>){ // statements that execute if the above boolean is true } else { // something that executes if nothing matched above }

  45. Combining into “else if”s(Selecting one from many) #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { cout << “A”; } // end if else if (start < end) { cout << “B”; } // end else if } // end main

  46. Combining into “else if”s(Selecting one from many) #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { cout << “A”; } // end if else if (start < end) { cout << “B”; } // end else if } // end main Output A

  47. else catch-all Example #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { cout << “A”; } // end if else if (start > end) { cout << “B”; } // end else if else { cout << “C”; } // end else } // end main

  48. else catch-all Example #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { cout << “A”; } // end if else if (start > end) { cout << “B”; } // end else if else { cout << “C”; } // end else } // end main Output C

  49. Switch statements • Simplify the else-if statements for primitive data types • Trying to find a match • Once a match is found, it will keep going until it sees the break keyword (telling it to stop) • Has format: switch (<primitive variable>) { case <value>: case <value>: }

  50. Switch Example #include <iostream> using namespace std; void main ( ) { int myInt = 1; switch (myInt) { case 1: cout << “A”; case 2: cout << “B”; case 3: cout << “C”; } //end switch } //end main // Prints out A, B, and C. Why?

More Related