1 / 37

Data Types & Operators

Data Types & Operators. EECE 351 Spring 2000 Lecture # 2. Data Types & Operators. Introduction Preprocessor Commands Scope & Lifetimes Data Types Operators Errors Program Life cycle What we know Where we are going. Introduction.

karsen
Download Presentation

Data Types & Operators

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. Data Types & Operators EECE 351 Spring 2000 Lecture # 2 EECE 351 – Lecture 2

  2. Data Types & Operators • Introduction • Preprocessor Commands • Scope & Lifetimes • Data Types • Operators • Errors • Program Life cycle • What we know • Where we are going EECE 351 – Lecture 2

  3. Introduction • Data types are used by the compiler to set aside regions of memory for you to use. • The variables you use will be one of the many different data types C/C++ allows you to use. • You must first declare your variable before you can use it. • intnX; //Declaration • nX = 32; //Use EECE 351 – Lecture 2

  4. Preprocessor Commands • The preprocessor gets things ready for the compiler to work with. • Common commands • #include – open this file and compile it • #import – same as above (used with .DLLs) • #define – gives a meaningful name to a constant in your program • The #if directive, with the #elif, #else, and #endif directives, controls compilation of portions of a source file. (covered later) EECE 351 – Lecture 2

  5. Scope • If a variable is placed inside a “{” “}” pair, it is said to be local to that block. • If a variable is placed outside a block, it is called global. • A global variable can be accessed by anything below it. • A local variable only lives inside its block of code. EECE 351 – Lecture 2

  6. Scope Example int g_nX; //Global integer, can be seen by all void main (void) //Starting point { int nX; //Local int, only main can access nX = 0; //Okay, grabs local int g_nX = 3; //Okay, can get to g_nX nY = 2; //NO! Can’t go inside fn g_nY = 1; //NO! Don’t know who g_nY is } EECE 351 – Lecture 2

  7. Scope Example (cont.) int g_nY; //Global integer, can only be seen by below void fn(void) //Another function { int nY; //Local int nX = 0; //NOPE!! Don’t know this guy g_nX = 3; //Okay, I can get to it nY = 2; //My int, so yes I can get to it g_nY = 1; //Yes, I know about g_nY } EECE 351 – Lecture 2

  8. Storage Class types (AKA lifetimes) • auto (C++ default)– local lifetime • register – Not supported by MS! • static – global lifetime, but local scope • Multiple declarations of same static variable only creates 1 variable. • extern – used to link up with variables that exists in other files, blocks, etc. • Other MS specific (thread, DLLs, etc.) EECE 351 – Lecture 2

  9. Data Types • Predefined • Numbers • ints, floats, doubles (oh my!) • characters • User defined • enums • union • structs / classes (will cover later) EECE 351 – Lecture 2

  10. Ints • integers can only be whole numbers. • They use 4 bytes of memory. • Can be other “sub types” • short – 2 bytes • long – 4 bytes • _int“N” – N is either 8, 16, 32, or 64. N specifies number of bits type requires. • unsigned – eliminates signed bit EECE 351 – Lecture 2

  11. Floats & Doubles • floats can have a fractional part. • They use 4 bytes of memory. • doubles can be either. • They use 8 bytes of memory. • More precise, can hold larger values. EECE 351 – Lecture 2

  12. Limits and Overflow • All limits are placed in either <limits.h> or <climits> (usingnamespace std;). • If variable is incremented past its limit, overflow occurs. • If number is signed, value becomes negative. • If number is unsigned, value becomes 0. EECE 351 – Lecture 2

  13. Characters • We know about numbers; how about letters? • Well, C++ gives us the char data type. • Takes up 1 byte of memory. • chars can be any members of the execution character set — in Microsoft C++, this is ASCII. • It is a special type of int. EECE 351 – Lecture 2

  14. Characters (cont.) • That’s great, but what if we want to set aside space for more than one letter? • Well, that (arrays and pointers) will be covered later. • For now, just do this: • char szName[1000]; • Sets aside space for 1000 characters. • Indexed through 0 – 999. EECE 351 – Lecture 2

  15. enum • User-defined type called an enumeration (increasing integer constant representations of words). • The following: enum Move {FRONT=1, BACK, LEFT, RIGHT}; • Makes the identifier FRONT always equal 1 (Back = 2, Left = 3, …) • If no number is specified, default is 0. • Consider the following: EECE 351 – Lecture 2

  16. enum //Declare three enums; C++ is always case sensitive. enum MOVE {FRONT = 1, BACK, LEFT, RIGHT}; enum LOOK {front, back, left, right}; enum BLOCK {Front, Back = 4, Left, Right}; void main() { //What numbers will the following print out? cout << FRONT << " " << BACK << " " << LEFT << " " << RIGHT << endl; cout << front << " " << back << " " << left << " " << right << endl; cout << Front << " " << Back << " " << Left << " " << Right << endl; } EECE 351 – Lecture 2

  17. enum EECE 351 Data Type Lecture--Figure 2 enum example version 1.0 1 2 3 4 0 1 2 3 0 4 5 6 Thank you. Press any key to continue. EECE 351 – Lecture 2

  18. union • Used to represent objects with shared storage space • Only one is “active” at a time. • Number of bytes claimed is equal to largest member. • Declared the same way as a struct or class • Name is unnecessary – anonymous unions • Example on following slide EECE 351 – Lecture 2

  19. union static union /*Name*/ //Declare anonymousunion. { //Must be static because it’s global. int nX; //Could be an int float fY; //Or a float double dZ; //Or a double }; //Allocates space for double int main(int argc, char* argv[]) { //Name objectName would be needed nX = 1; //”Name.” would be needed here and  cout << nX << " " << fY << " " << dZ << endl; EECE 351 – Lecture 2

  20. union fY = 1.0f; //Set the float to 1 //Print out the different options. cout << nX << " " << fY << " " << dZ << endl; dZ = 1.0; //Repeat process for double. cout << nX << " " << fY << " " << dZ << endl; return 0; } EECE 351 – Lecture 2

  21. union EECE 351 Data Type Lecture--Figure 3 union example version 1.0 1 1.4013e-045 4.94066e-324 1065353216 1 5.26354e-315 0 0 1 Thank you. Press any key to continue. EECE 351 – Lecture 2

  22. Operators • Symbols that specify an evaluation to be performed. There are 3 types: • Unary • Only require 1 argument • Binary • Need 2 Arguments to work • Ternary • 3 sections (only one “? :”) EECE 351 – Lecture 2

  23. Unary Operators • – ~  ! – Negation and complement • *  & – Indirection and address of (used with pointers) • sizeof – Returns the size of a var.or • ++ –– – Pre/Post increment & decrement • Pre is placed before the var. (increments var. before using); post is after. EECE 351 – Lecture 2

  24. Unary Operator –Type Casting • Type Casting allows you to convert one data type to another (with some limits). • There are five types (in order of use): • “Old style” – standard casting operators • Dynamic – Used for polymorphic types • Static – Used for nonpolymorphic types • Const – Toggles const state • Reinterpret – bit reinterpretation EECE 351 – Lecture 2

  25. Binary Operators • *  /  % Multi, div, Modulus (remainder) • +  –Additive subtraction • <<>>Shiftoperators • <   >   <=   >=   ==   !=Relational • &   |  ^Bitwise AND, OR, NOT • &&   ||Logical AND, OR • , Sequential-evaluation EECE 351 – Lecture 2

  26. Ternary Operator • Conditional – used to evaluate relationships • Test ? True : False; • Example: • int i, j; //Declare 2 ints • cin >> i; //User inputs i • //j becomes absolute value of i • j = ( i < 0 ) ? ( -i ) : ( i ); EECE 351 – Lecture 2

  27. Errors • Bug origin • Error Life Cycle • Error Cost to Fix • Syntax Errors • Semantic Errors • The Software Engineering Role EECE 351 – Lecture 2

  28. Bug Origin • U.S. Navy Admiral Grace Murray Hopper came up with the term "bug." The first “bug” in computers was a real bug -- a moth. At Harvard one August night in 1945, Hopper and her associates were working on the "granddaddy" of modern computers, the Mark I. "Things were going badly," she said. "Finally, someone located the trouble spot and, using ordinary tweezers, removed the problem, a two-inch moth. From then on, when anything went wrong with a computer, we said it had bugs in it." EECE 351 – Lecture 2

  29. Error Life Cycle EECE 351 – Lecture 2

  30. Error Cost to Fix EECE 351 – Lecture 2

  31. Syntax Errors • These are errors you find when compiling your program. • MSDEV gives you a brief description of the error. • Double-clicking on the error will take you to the line that caused the problem. • Don’t just look at this line; the one above it might be the trouble maker “;” • USE MSDN!!!!! EECE 351 – Lecture 2

  32. Semantic Errors • These are errors that are encountered when you run the program. • Abort, Retry, Cancel dialog box • Weird printouts, incorrect answers • Only review will help to correct the problem. EECE 351 – Lecture 2

  33. The Software Engineering Role • Why does a Software Engineer care about bugs? • Software Engineering is about QUALITY! • OK, how does a Software Engineer eliminate the bugs? • 1 Word (repeated incessantly) • TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST EECE 351 – Lecture 2

  34. Program Life Cycle • You all should have read Chapter 1 by now. Recognize this? EECE 351 – Lecture 2

  35. The Pie Where the programming effort goes: The reason why this is so big is because this is so small. The more tests you run, the less maintenance you’ll need to perform. CATCH THE BUGS AS SOON AS YOU CAN! EECE 351 – Lecture 2

  36. What we know • Data Types • Numbers, characters, user specified • Operators • Unary • Binary • Ternary • Our Enemies!! • Bugs, errors (syntax & semantic) EECE 351 – Lecture 2

  37. Where we are going • Lecture – Control Structures • if/else, while, for • Lab – Debugging • Homework – Calculator EECE 351 – Lecture 2

More Related