data types storage size domain of all possible values operations 1

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

Upload: berenice-white

Post on 17-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

Data Types

• Storage Size • Domain of all possible values • Operations

1

Page 2: Data Types Storage Size Domain of all possible values Operations 1

C++ Data Types• char • int (short, long)• float (double) • string (C++ string)• bool• Array• C string (array of char as string)• Enumeration type• Structure• (typedef)• (ClassClass)• . . . 2

Page 3: Data Types Storage Size Domain of all possible values Operations 1

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

Page 4: Data Types Storage Size Domain of all possible values Operations 1

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

Page 5: Data Types Storage Size Domain of all possible values Operations 1

C++ Operations

• Input• Output• Assignment• Comparison• Arithmetic operations• Function return value• Function parameter• Others?

5

Page 6: Data Types Storage Size Domain of all possible values Operations 1

Operations on intint x, y;

// Inputcin >> x;inFile >> x;

// Outputcout << x;outFile << x;cout << setw(9) << x << endl;

// Assignmentx = 10;y = x;

// Comparisonif (x <= y) cout << “x is no more than y.”;if (x == y) cout << “x is the same as y.”;// Syntax and Style

// Arithmetic operationsx += y;y = x % 5;

6

Page 7: Data Types Storage Size Domain of all possible values Operations 1

Operations on int

// Function return valueint ReturnInt();

// Function parameters// Params: (in, out, inout)void IntParameters(int IntIn, int& IntOut, int& IntInOut);

7

Page 8: Data Types Storage Size Domain of all possible values Operations 1

Operations on charchar x, y;

// Inputcin >> x; // skip white spacescin.get(x); // read in white spacesinFile.get(x);

// Outputcout << x;cout << setw(9) << x << endl;

// Assignmentx = ‘0’; // not null charx = ‘\0’; // null chary = x;

// Comparison: ASCII code valueif (x <= y) cout << “x is no more than y.”;if (x == ‘A’) cout << “x is a ‘A’.”;

// Arithmetic operationsx ++;y = x % 5;

8

Page 9: Data Types Storage Size Domain of all possible values Operations 1

Operations on char

// Function return valuechar ReturnChar();

// Function parameters// Params: (in, out, inout)void charParameters(char charIn, char& charOut, char &charInOut);

9

Page 10: Data Types Storage Size Domain of all possible values Operations 1

Operations on float

Same as int, except %

float x, y;

// Input// Output// Assignment// Comparison// Arithmetic operationsy = x % 5; // NO!// Function return value// Function parameters// Params: (in, out, inout)

10

Page 11: Data Types Storage Size Domain of all possible values Operations 1

Operations on string(C++ string)

string x, y;

// Inputcin >> x;inFile >> x;

// Outputcout << x;cout << setw(9) << x;

// Assignmentx = “My String.”;y = x;

// Comparisonif (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

Page 12: Data Types Storage Size Domain of all possible values Operations 1

Operations on string (C++ string)

// Function return valuestring ReturnString();

// Function parameters// Params: (in, out, inout)void stringParameters(string stringIn, string& stringOut, string& stringInOut);

12

Page 13: Data Types Storage Size Domain of all possible values Operations 1

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 StringStudentType CS143[25];

13

Page 14: Data Types Storage Size Domain of all possible values Operations 1

Operations on Arrayint GoodIntArray[MAX_SIZE * 2 - 100];float floatArray[100];char charArray[MAX_SIZE + 1];

// Inputcin >> 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

Page 15: Data Types Storage Size Domain of all possible values Operations 1

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

Page 16: Data Types Storage Size Domain of all possible values Operations 1

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

Page 17: Data Types Storage Size Domain of all possible values Operations 1

Operations on Array

// Function return valueint A[] CannotReturnArray();// NO!

// Function parameters// Params: (in, out, inout, in)void arrayParameters(const int arrayIn[], float arrayOut[], char arrayInOut[], int size);

17

Page 18: Data Types Storage Size Domain of all possible values Operations 1

C String (Array of Char)

• Using C++ string #include <string>• Using C string #include <cstring> Functions: strlen() strcpy() strcmp()

18

Page 19: Data Types Storage Size Domain of all possible values Operations 1

Operations on C-String#include <cstring>

char Name[21]; // up to 20 chars, ending with ‘\0’

cin >> Name;// Yes, ‘\0’ is inserted

cout << name;// Yes, stop at ‘\0’

19

Page 20: Data Types Storage Size Domain of all possible values Operations 1

Operations on C-StringName = “CS2430”;// NO!

strcpy(Name, “CS2430”);

if (Name > “CS1430”) cout << “Larger than 1430”;// NO!

if (strcmp(Name, “CS1430”) > 0) cout << “Larger than 1430”;

20

Page 21: Data Types Storage Size Domain of all possible values Operations 1

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

Page 22: Data Types Storage Size Domain of all possible values Operations 1

Operations on structStudentType s1, s2;

// Inputcin >> s1;// NO!

cin >> s1.gpa;// YES!

// Outputcout << s1;// NO!

cout << s1.gpa;// YES!

s2 = s1;// Yes!

if (s1 < s2) cout << “s1 is smaller.”;// NO!

22

Page 23: Data Types Storage Size Domain of all possible values Operations 1

Function Parameters

InThe function uses the value of the actual parameter.

OutThe 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)

InOutBoth In and Out

23

Page 24: Data Types Storage Size Domain of all possible values Operations 1

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

Page 25: Data Types Storage Size Domain of all possible values Operations 1

Test 3

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

25