1 / 72

CSIS 113A

CSIS 113A. Introduction To C++. Overview Of A Computer. The Hardware. CPU. Disk. I/O Devices. Memory. Memory. The Memory. Memory is addressed by byte In our Windows machines 1 byte = 8 bits Each bit can be 0 or 1 Memory can hold Program instructions Data. 000. 10010110. 001.

Download Presentation

CSIS 113A

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. CSIS 113A Introduction To C++ Glenn Stevenson CSIS 113A MSJC

  2. Overview Of A Computer The Hardware CPU Disk I/ODevices Memory Glenn Stevenson CSIS 113A MSJC

  3. Memory The Memory • Memory is addressed by byte • In our Windows machines 1 byte = 8 bits • Each bit can be 0 or 1 • Memory can hold • Program instructions • Data 000 10010110 001 00110010 002 10111101 003 11000110 004 01010000 005 11011110 006 10100101 007 11011111 008 00110101 … Max 00101101 Glenn Stevenson CSIS 113A MSJC

  4. Memory II TheMemory • Memory is split into space for program instructions and for data • Data space is usually further subdivided • Static/constant • Stack • Heap ProgramInstructions Data Glenn Stevenson CSIS 113A MSJC

  5. CPU And Memory The CPU ProgramCounter Instruction register Other Registers Memory Other Registers ... OtherRegisters • In our Windows machines these registers are 32 bits wide ALU Glenn Stevenson CSIS 113A MSJC

  6. The OS • Operating System • Windows • Linux • Applications • Editor • Compiler • The programs we will write Applications OperatingSystem APIs Drivers Hardware = Software Glenn Stevenson CSIS 113A MSJC

  7. Developing C++ Programs • Steps to develop a program • Write C++ instructions, store in source-code file • The following two steps are usually automated • Use compiler to turn source into object code • Use linker to create executable • Use a debugger to examine and test program • Standalone tools vs. an IDE • Integrated Development Environment combines tools • You should know how to use individual tools Glenn Stevenson CSIS 113A MSJC

  8. Compile, Link, Execute SourceCode ObjectCode Preprocessor Compiler Comments (discarded) Linker .exe Glenn Stevenson CSIS 113A MSJC

  9. Comments • Way to leave notes in your code • Ignored by compiler • You should use them liberally • 2 types • Single line // • Everything after the double slash is a comment up to the end of the line • Multi-line /* */ • /* everything between • Is a comment */ Glenn Stevenson CSIS 113A MSJC

  10. Required for homework • All assignments must have comments at the top of each page! /*Name: Glenn StevensonSID#: 1234567Date: todays dateLab #: 1Description: you can simply copy and paste from the assignment descript itself */ • Failure to add comments to your assignments can result in a 0 grade Glenn Stevenson CSIS 113A MSJC

  11. Functions & main • C++ is typically a cooperating group of functions • Functions • Like a small program within a large program • Used to perform a specific task • main – function where your program begins • Your program must have a main function • Programs need a place to start • Program will not link without a main function Glenn Stevenson CSIS 113A MSJC

  12. Anatomy of main • C++ is a case Sensitive language • Function Header • First line of function • Function Body • Code between opening and closing braces • All blocks have an opening and a closing brace • Return Statement • What this function returns • Where does in return to? Glenn Stevenson CSIS 113A MSJC

  13. Statements / Blocks • Statements • Any valid line of C++ code • Statements are terminated by a semicolon • Can span multiple lines compiler looks for ; as a marker • Compiler generates an error when a statement is not terminated by a semicolon • Blocks • Any code that has an opening & closing brace • Not terminated by a semicolon • Function main has opening and closing brace • Function body then is a block of code Glenn Stevenson CSIS 113A MSJC

  14. Our First Program! // myfirst.cpp - A simple C++ program #include <iostream> // Preprocessor statement using namespace std; // Make cout visible int main() // Function header { cout << "Hi, my name is Glenn Stevenson"; cout << "\n"; return 0; } Glenn Stevenson CSIS 113A MSJC

  15. Ananlyizing myFirst.cpp • #include <iostream> • Has definitions input and output streams • C++ has much functionality included • using namespace std; • Use built functionality from standard library • int main() • Function header of main followed by the body of main (code between the baces) • cout << “Hi my name is Glenn Stevenson”; • cout puts information to screen • cout << “\n”; • Moves the cursor to the next line on the console window • Called an escape character, more about that later Glenn Stevenson CSIS 113A MSJC

  16. What is wrong with this code? // myfirst.cpp - A simple C++ program #include <iostream> // Preprocessor statement using namespace std; // Make cout visible int Main(); // Function header { cout << "Hi, my name is Glenn Stevenson"; cout << "\n"; return 0; }; Glenn Stevenson CSIS 113A MSJC

  17. The preprocessor • Separate program called CPP [C Pre-Processor] • Massages source code before giving it to compiler • Preprocessor instructions are called directives • Preprocessor directives all begin with # • The #include <iostream> directive says • "Send the declarations and definitions in the iostream header to the compiler at this point" • What is a "header"? • "Family" of declarations and definitions • Normally stored in a file (or translation unit as it is called in C++), but doesn't have to be Glenn Stevenson CSIS 113A MSJC

  18. The Preprocessor II #include <iostream> // Preprocessor statement SourceCode ObjectCode Preprocessor Compiler Header Glenn Stevenson CSIS 113A MSJC

  19. The Preprocessor III • Traditional [C-style] headers all end in .h • Standard C++ headers: • Don't have ".h" suffix • Don't necessarily refer to file names • Older [pre-IS0] compilers won't support • Under Turbo C++, for instance, you write: • #include <iostream.h> • And don’t get namespace functionality • Do not use the C-style headers • C++ Style Guide 7.1.1 Glenn Stevenson CSIS 113A MSJC

  20. The Standard Headers • 51 headers available in Standard C++ • 18 ANSI C Standard library headers • Available in traditional and new styles • <stdio.h> and <cstdio> • 13 Standard Template Library [STL] headers • Collections and algorithms [generic programming] • 9 Input/Output [I/0] stream headers • String, exception, memory management, and various data structure classes not in the STL Glenn Stevenson CSIS 113A MSJC

  21. Namespaces using namespace std; // Make cout visible • A namespace defines a region where names are valid • In C, all function names are "global" • Headers combine functions into "families" • Namespaces combine families into "neighborhoods" • All Standard C++ library functions and objects are in the std namespace • C library functions also in std with new headers • Some older compilers have not fully implemented this Glenn Stevenson CSIS 113A MSJC

  22. Namespaces II • To use a function in a namespace • Prefix it with namespace and :: [scope resolution] • Example: using the standard sqrt() function • #include <cmath> // In std namespacedouble ans = std::sqrt(5.25); • In this class, you'll often import entire std namespace • "Import" with using namespace declaration Glenn Stevenson CSIS 113A MSJC

  23. Literals • Called literals because they are values that are literally stated • 1 // integer literal – whole number • 2.2 // floating point literal • .3 // floating point literal • 'h‘ // single character literal • "Hello“ // string literal • Don’t confuse single characters for strings! • This is not legal ‘hello’ and • ‘h’ is different from “h” Glenn Stevenson CSIS 113A MSJC

  24. C++ Output cout << "Hi, my name is Glenn Stevenson"; • I/O is supplied as part of the standard library • Definitions found in the <iostream> header • Standard I/O is represented as a stream • cout uses the stream extraction operator • << notice that it points at cout! • Can use many insertion operators to separate types • Cout << “hello my lucky number is “ << 7 << endl; • endl does the same thing as “\n” • Moves cursor to next line on console window Glenn Stevenson CSIS 113A MSJC

  25. Changing Numbers With cout • You can output numbers in many formats • Send format type to cout • Hexadecimal – Base 16 • cout << hex << 16 << endl; • Outputs 10 because 10 is 16 in hex • Octal – Base 8 • cout << oct << 9 << endl; • Outputs 10 because 9 is 10 in octal • Decimal – Base 10 • cout << dec << 10 << endl; • Outputs 10 • Will output desired format until changed or program ends Glenn Stevenson CSIS 113A MSJC

  26. What is a stream? • Streams are abstract information flows • Data flows into your program from a source • Your program processes the data • Information flows out of your program to a sink • Sending info to a sink is called writing • Getting info from a source is called reading Glenn Stevenson CSIS 113A MSJC

  27. Standard streams • C++ initially creates at least 3 stream objects • Also known as standard input, output, and error • cin : input connected to the keyboard • cout : output connected to the monitor • cerr : output connected to the monitor Glenn Stevenson CSIS 113A MSJC

  28. Formatting your code • C++ is a free-form language • Interchangably use spaces, tabs and newlines • int main() { cout << "Hi"; return 0; } • int main ( ) {cout <<"Hi" ; return 0 ; } Glenn Stevenson CSIS 113A MSJC

  29. Formatting II • Cannot use whitespace between parts of a token • A token is one or more characters with a single meaning • /*, */, <<, // are all tokens • Keywords like int and return are tokens • Here are some uses of illegal whitespace with tokens in t main( ) { cout < < "Hi" ; ret urn 0; } • You cannot put a literal newline in a string cout << “This is illegal”; cout << “But this ” “is legal!”; Glenn Stevenson CSIS 113A MSJC

  30. Code Formatting III • Must use whitespace to separate some tokens • cout<<"Hello there"; // Not required • intmain(){return0;} // Spaces neededint main(){return 0;} // OKint main(){return(0);} // OK Glenn Stevenson CSIS 113A MSJC

  31. Values & Variables • Values are discrete quantities of data • Here are some values: • 1, 3.1459, “Glenn”, D, true • Values represent different kinds of things • In programming, this is called a value's type • integers, real, text [strings], characters, Boolean • Variables are: • Named storage locations that holdvalues • Every variable holds a value of a certain type • The type defines the legal operations and the required memory to store it (size) Glenn Stevenson CSIS 113A MSJC

  32. Declaring Variables • C++ has two statements devoted to variables • Declaration statements and assignment statements • A declaration statement specifies: • The name of memory location • The kind of thing (type) stored at the location int fleas; • This creates an integer variable named fleas • The compiler sets aside memory to store an int • The amount of memory for an int is implementation dependent • Makes sure that you use variable appropriately • Cannot store a string in fleas, for instance Glenn Stevenson CSIS 113A MSJC

  33. Variable Definition I • I like to think of the compiler taking a box of the correct size for the type of variable (a variable can be from 1 to n bytes in size), and writing the name, type of variable and its address on the outside. • The box will hold the value of the variable when it’s used. • The boxes are then stacked away in storage Name: i Type: int Address: 5000 Name: ch Type: char Address: 5004 Name: r Type: double Address: 5005 4 byte box 1 byte box 8 byte box Glenn Stevenson CSIS 113A MSJC

  34. Variable Definition II x int 5000 ch char 5004 r double 5005 5000 … x5001 …5002 …5003 … 5004 … ch5005 … r5006 …5007 … … Glenn Stevenson CSIS 113A MSJC

  35. LValues • An LValue is nothing more than something that is to the left of the equal sign. • Since values get assigned from right to left, the lvalue must alway be a variable . • This means that you cannot do something like this: int x = 30; 25 = x; //Can’t Do this Glenn Stevenson CSIS 113A MSJC

  36. Variable Naming Rules • Variable names can consist of letters, numbers, and the underscore but cannot begin with a number. • They can also not be a C++ keyword like if, else, for, etc. • Variable names cannot contain a space character. int my Var; // This is not validint myVar; // This is valid Glenn Stevenson CSIS 113A MSJC

  37. Naming Conventions • Naming conventions have changed over the years it used to be that if you had a variable name that had two words in it you would separate the words with an underscore. • int my_int_variable; • Later people starting capitalizing every word. • int MyIntVariable; • Currently, the convention is to capitalize from the second word on: • int myIntVariable; Glenn Stevenson CSIS 113A MSJC

  38. Number Types • C++ has several number types in two categories • Integral • Counting numbers • Signed • Unsigned • Floating-point • Real numbers (numbers with a fractional part) Glenn Stevenson CSIS 113A MSJC

  39. Numeric Type Sizes Glenn Stevenson CSIS 113A MSJC

  40. Losing Precision • Copying values from one type of a variable to another type can force a loss of data. double d = 33.456;int x; x = d; • x holds an integer value and the code is attempting to store double in it. • These are different size buckets! • the double will get truncated to an integer • The value stored in x is 33. Glenn Stevenson CSIS 113A MSJC

  41. Losing Precision II • You can put smaller values into bigger buckets because they will fit: . double d;int x = 30; d = x; //This is fine, no loss of data • You may actually want to store a large value in a small bucket. • You can do this without the compiler complaining by casting the value. • Future topic Glenn Stevenson CSIS 113A MSJC

  42. Binary Numbers • Unsigned (positive) binary integers • Converting a binary number to decimal • Each binary digit represents a power of 2 • Just like decimal digits represent power of 10 • So what is 10110101 binary in decimal? • 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 • 1 0 1 1 0 1 0 1 • 128 0 32 16 0 4 0 1 • = 181 Glenn Stevenson CSIS 113A MSJC

  43. Binary Numbers II • So what’s 0x608bfa05 in decimal? • 5 x 160 = 5 x 1 = 50 x 161 = 0 x 16 = 0A x 162 = 10 x 256 = 2,560F x 163 = 15 x 4096 = 61,440B x 164 = 11 x 65536 = 720,8968 x 165 = 8 x 1048576 = 8,388,6080 x 166 = 0 x 16777216 = 06 x 167 = 6 x 268435456 = 1,610,612,736 1,619,786,245 • So 01100000100010111111101000000101= 014042775005 = 0x608bfa05 = 1,619,786,245 Glenn Stevenson CSIS 113A MSJC

  44. Binary Numbers III • Converting a decimal number to binary • Find largest power of 2 and subtract; repeat • Example: Decimal 89 as binary • Largest power of 2 is 64 : 0100-0000 • Subtract 64 from 89 = 25 • Find next power of 2 [16] : 0101-0000 • Subtract 16 from 25 = 9 • Find next power of 2 [8] : 0101-1000 • Subtract 8 from 9 = 1 • Find next power of 2 [1] : 0101-1001 • = 0x59 Glenn Stevenson CSIS 113A MSJC

  45. Binary Number IV • A way with a lot less work – convert to hex first • Find the largest power of 16 and subtract the multiples • Example: Decimal 89 as binary • Largest power of 16 is 16: 0x50 • Subtract 5 * 16 from 89 = 9 • Next smallest power of 16 is 1: 0x59 • Subtract 9 * 1 from 9 = 0 • Convert hex to binary by inspection • = 0101 1001 Glenn Stevenson CSIS 113A MSJC

  46. Binary Numbers V • An easier way to convert • In Windows there is a calculator application (usually in the accessories group) • Make certain that the scientific option is selected under the view menu • Use the Hex, Oct, Dec and Bin radio buttons to convert numbers • You’re probably now wondering why do I need to know this stuff??? • C++, like it predecessor C, can do low-level programming • You can “bit-twiddle” which is frequently necessary when dealing with hardware and systems programming • When examining memory the addresses and values are normally displayed in hex Glenn Stevenson CSIS 113A MSJC

  47. Binary Numbers VI • How can signed binary integers be stored? • One scheme is called sign-magnitude method • Give up a bit to represent sign (0 = positive) • Remaining bits represent magnitude • 1011 - 0101 = - (32 + 16 + 4 + 1) = -53 • Not generally used because of the two-zeros problem • 0000-0000 = +0 1000-0000 = -0 Glenn Stevenson CSIS 113A MSJC

  48. Binary Numbers VII • One’s complement • MSB [most-significant-bit] is used for sign bit • If it is set, then one's complement is used to calculate the magnitude of negative number • One’s complement: • Reverse every bit (0  1 and 1  0) • Also called complement • Interpret result as an unsigned number • Place minus sign in front of answer • Still results in two zeros • 0000 – 0000 = +0 • 1111 – 1111 = -0 Glenn Stevenson CSIS 113A MSJC

  49. Binary Numbers VII • Two's complement results in one zero • MSB [most-significant-bit] is used for sign bit • If it is set, then two's complement is used to calculate the magnitude of negative number • Two's complement: • Reverse every bit [simple, or one's complement] • Add one to the result • Interpret result as an unsigned number • Place minus sign in front of answer Glenn Stevenson CSIS 113A MSJC

  50. Binary Numbers VIII • Wait a minute, how do I add 1 in binary? • 0 0 1 +0 +1 +1 0 1 10 • 1 + 1 = 0 carry 1 • So • 1011 + 1 1100 Glenn Stevenson CSIS 113A MSJC

More Related