Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types Storage Size Domain of all possible values Operations 1.

Similar presentations


Presentation on theme: "Data Types Storage Size Domain of all possible values Operations 1."— Presentation transcript:

1 Data Types Storage Size Domain of all possible values Operations 1

2 C++ Data Types 2

3 Storage Size System dependent char: 1 byte int: 2 bytes float: 4 bytes bool: 1 or 2 bytes C++ string: Reference (num of chars + 1) Array: max number of elements type of elements C string (array of char as string) max num of chars + 1 Structure Sum of all member fields 3

4 Domain of Possible Values char ASCII code table int -32768 -- 32767 float Much larger range string (both C string and C++ string) Any thing within “a string” Array All elements are of the same type char, int, float, string, bool, structure… Struct 4

5 C++ Operations Input Output Assignment Comparison Arithmetic operations Function return value Function parameter Others? 5

6 Operations on int int x, y; // Input cin >> x; inFile >> x; // Output cout << x; outFile << x; cout << setw(9) << x << endl; // Assignment x = 10; y = x; // Comparison if (x <= y) cout << “x is no more than y.”; if (x == y) cout << “x is the same as y.”; // Syntax and Style // Arithmetic operations x += y; y = x % 5; 6

7 Operations on int // Function return value int ReturnInt(); // Function parameters // Params: (in, out, inout) void IntParameters(int IntIn, int& IntOut, int& IntInOut); 7

8 Operations on char char x, y; // Input cin >> x; // skip white spaces cin.get(x); // read in white spaces inFile.get(x); // Output cout << x; cout << setw(9) << x << endl; // Assignment x = ‘0’; // not null char x = ‘\0’; // null char y = x; // Comparison: ASCII code value if (x <= y) cout << “x is no more than y.”; if (x == ‘A’) cout << “x is a ‘A’.”; // Arithmetic operations x ++; y = x % 5; 8

9 Operations on char // Function return value char ReturnChar(); // Function parameters // Params: (in, out, inout) void charParameters(char charIn, char& charOut, char &charInOut); 9

10 Operations on float Same as int, except % float x, y; // Input // Output // Assignment // Comparison // Arithmetic operations y = x % 5; // NO! // Function return value // Function parameters // Params: (in, out, inout) 10

11 Operations on string (C++ string) string x, y; // Input cin >> x; inFile >> x; // Output cout << x; cout << setw(9) << x; // Assignment x = “My String.”; y = x; // Comparison if (x > “CS1430”) cout << “x is larger than CS1430.”; if (x > “CS1430”) cout << "x is larger than " << char(34) << "CS1430" << char(34); if (x == y) cout << “x is the same as y.”; // Arithmetic operations // NO! 11

12 Operations on string (C++ string) // Function return value string ReturnString(); // Function parameters // Params: (in, out, inout) void stringParameters(string stringIn, string& stringOut, string& stringInOut); 12

13 Operations on Array const int MAX_SIZE = 100; int size = 100; int GoodIntArray[MAX_SIZE * 2 - 100]; int badIntArray[size]; float floatArray[100]; string stringArray[MAX_SIZE]; char charArray[MAX_SIZE]; // Array of char or C String StudentType CS143[25]; 13

14 Operations on Array int GoodIntArray[MAX_SIZE * 2 - 100]; float floatArray[100]; char charArray[MAX_SIZE + 1]; // Input cin >> GoodIntArray; // NO! cin >> floatArray; // NO! cin >> charArray; // Yes! Treated as a String (null char inserted) cout << GoodIntArray; // NO! cout << floatArray; // NO! cout << charArray; // Yes if treated as a string (must be null terminated) 14

15 Operations on Array int GoodIntArray[100]; float floatArray[100]; char charArray[101]; int A[100]; A = GoodIntArray; // NO! float B[100]; B = floatArray; // NO! char C[101]; C = charArray; // NO! 15

16 Operations on Array // Comparison int A[100]; if (A == GoodIntArray) cout << “Same array!”; // NO! float B[100]; if (B <= floatArray) cout << “B is smaller than floatArray.”; // NO! char C[101]; if (C > charArray) cout << “C is larger than charArray.”; // NO! 16

17 Operations on Array // Function return value int A[] CannotReturnArray(); // NO! // Function parameters // Params: (in, out, inout, in) void arrayParameters(const int arrayIn[], float arrayOut[], char arrayInOut[], int size); 17

18 C String (Array of Char) Using C++ string #include Using C string #include Functions: strlen() strcpy() strcmp() 18

19 Operations on C-String #include char Name[21]; // up to 20 chars, ending with ‘\0’ cin >> Name; // Yes, ‘\0’ is inserted cout << name; // Yes, stop at ‘\0’ 19

20 Operations on C-String Name = “CS2430”; // NO! strcpy(Name, “CS2430”); if (Name > “CS1430”) cout << “Larger than 1430”; // NO! if (strcmp(Name, “CS1430”) > 0) cout << “Larger than 1430”; 20

21 Array of char vs. C-String const int NUM_QUESTIONS = 50; const int NAME_SIZE = 20; char quizName[NAME_SIZE + 1]; char quizAnswer[NUM_QUESTIONS]; cin >> quizName; // C String! // ‘\0’ at the end for (int i = 0; i < NUM_QUESTIONS; i ++) cin >> quizAnswer[i]; // Not C String! // Not ending with ‘\0’ 21

22 Operations on struct StudentType s1, s2; // Input cin >> s1; // NO! cin >> s1.gpa; // YES! // Output cout << s1; // NO! cout << s1.gpa; // YES! s2 = s1; // Yes! if (s1 < s2) cout << “s1 is smaller.”; // NO! 22

23 Function Parameters In The function uses the value of the actual parameter. Out The function updates the value of the actual parameter and passes it back to the calling function. (a function can only return one value using the return statement) InOut Both In and Out 23

24 Passing Function Parameters Passing basic data types (int, float, char, string, bool) IN: Pass by value (without &) OUT, INOUT: Pass by reference (with &) Passing arrays (including C-String) Always pass by reference (NO &) IN: const (No &) Passing structs (Our programming rule) Always pass by reference with & IN: const (with &) Passing structs (C ++ rule) IN: Pass by value (without &) OUT, INOUT: Pass by reference (with &) 24

25 Test 3 Wednesday 60 points File I/O Enumeration C-String Selection Sort Structure Dynamic List Deleting 25


Download ppt "Data Types Storage Size Domain of all possible values Operations 1."

Similar presentations


Ads by Google