Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMS 261 Computer Science I

Similar presentations


Presentation on theme: "COMS 261 Computer Science I"— Presentation transcript:

1 COMS 261 Computer Science I
Title: C++ Fundamentals Date: September 13, 2004 Lecture Number: 10

2 Announcements Read 4.1 – 4.6 Homework

3 Review Standard data types Names Precedence
Strings (not part of C++, but are common) Real (float) Names Precedence

4 Outline Assignment Statement const Non-fundamental Types string

5 Memory Depiction 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b . ffff

6 Memory Depiction float y = 12.5; float occupies 4 bytes
int temperature = 32; char letter = 'c'; 1001 1002 y 1003 12.5 1004 1005 1006 1007 Temperature 32 1008 1009 Letter c 100a 100b . ffff

7 Assignment Statement Basic form Action object = expression ;
celsius = (fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object

8 Definition int NewStudents = 6;

9 Definition int newStudents = 6; int oldStudents = 21;

10 Definition int newStudents = 6; int oldStudents = 21;
int totalStudents;

11 Assignment Statement int newStudents = 6; int oldStudents = 21;
int totalStudents; totalStudents = newStudents + oldStudents;

12 Assignment Statement int newStudents = 6; int oldStudents = 21;
int totalStudents; totalStudents = newStudents + oldStudents;

13 Assignment Statement int newStudents = 6; int oldStudents = 21;
int totalStudents; totalStudents = newStudents + oldStudents; oldStudents = totalStudents;

14 Assignment Statement int newStudents = 6; int oldStudents = 21;
int totalStudents; totalStudents = newStudents + oldStudents; oldStudents = totalStudents;

15 Consider int value1 = 10;

16 Consider int value1 = 10; int value2 = 20;

17 Consider int value1 = 10; int value2 = 20; int hold = value1;

18 Consider int value1 = 10; int value2 = 20; int hold = value1;
value1 = value2;

19 Consider int value1 = 10; int value2 = 20; int hold = value1;
value1 = value2;

20 Consider int value1 = 10; int value2 = 20; int hold = value1;
value1 = value2; value2 = hold;

21 Consider int value1 = 10; int value2 = 20; int hold = value1; value1 = value2; value2 = hold; We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

22 Incrementing int i = 1; i = i + 1;
Assign the value of expression i + 1 to i Evaluates to 2

23 Const Definitions Modifier const indicates that an object cannot be changed Object is read-only Useful when defining objects representing physical and mathematical constants const float Pi = ;

24 Const Definitions Value has a name that can be used throughout the program const int SampleSize = 100; Makes changing the constant easy Only need to change the definition and recompile

25 Assignment Conversions
Floating-point expression assigned to an integer object is truncated Integer expression assigned to a floating-point object is converted to a floating-point value

26 Assignment Conversions
float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

27 Nonfundamental Types Nonfundamental as they are additions to the language C++ permits definition of new types and classes A class is a special kind of type Class objects typically have Data members that represent attributes and values Member functions for object inspection and manipulation Members are accessed using the selection operator j = s.size(); selection operator

28 Nonfundamental Types Libraries often provide special-purpose types and classes Programmers can also define their own types and classes Examples Standard Template Library (STL) provides class string Class string Used to represent a sequence of characters as a single object

29 Class string Some definitions string name = "Joanne";
string decimalPoint = "."; string empty = ""; string copy = name; string question = '?'; // illegal

30 Nonfundamental Types To access a library use a preprocessor directive to add its definitions to your program file #include <string>

31 Nonfundamental Types The using statement makes syntax less clumsy
Without it std::string s = "Sharp"; std::string t = "Spiffy"; With it using namespace std; //std contains string string s = "Sharp"; string t = "Spiffy";

32 Class string Some string member functions
size() determines number of characters in the string string Saying = "Rambling with Gambling"; cout << Saying.size() << endl; // 22 substr() determines a substring (Note first position has index 0) string Word = Saying.substr(9, 4); // with

33 Class string find() computes the position of a subsequence
int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?

34 Class string Auxiliary functions and operators
getline() extracts the next input line string Response; cout << "Enter text: "; getline(cin, Response, '\n'); cout << "Response is \"" << Response << "\"” << endl; Example run Enter text: Want what you do Response is "Want what you do"

35 Class string Auxiliary operators + string concatenation
string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; += compound concatenation assignment string ThePlace = "Brooklyn"; ThePlace += ", NY";

36 #include <iostream>
using namespace std; int main() { cout << "Enter the date in American format: " << "(e.g., January 1, 2001) : "; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(0, i); int k = Date.find(","); string Day = Date.substr(i + 1, k - i - 1); string Year = Date.substr(k + 2, Date.size() - 1); string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0; }

37 Boolean Algebra Logical expressions have the one of two values - true or false A rectangle has three sides The instructor has a pleasant smile The branch of mathematics is called Boolean algebra Developed by the British mathematician George Boole in the 19th century Three key logical operators And, Or, Not

38 Boolean Algebra Truth tables Example
Lists all combinations of operand values and the result of the operation for each combination Example P Q P and Q False False False False True False True False False True True True

39 Boolean Algebra Or truth table P Q P or Q False False False
False True True True False True True True True

40 Boolean Algebra Not truth table P not P False True True False

41 Boolean Algebra Can create complex logical expressions by combining simple logical expressions not (P and Q) A truth table can be used to determine when a logical expression is true P Q P and Q not (P and Q) False False False True False True False True True False False True True True True False

42 A Boolean Type C++ contains a type named bool
Type bool has two symbolic constants true, false Boolean operators The and operator is && The or operator is || The not operator is ! Warning & and | are also operators so be careful what you type

43 A Boolean Type Example logical expressions
bool p = true; bool q = false; bool r = true; bool s = (P && Q); bool t = ((!Q) || R); bool u = !(R && (!Q));

44 Relational Operators Equality operators Examples == != int i = 32;
int k = 45; bool q = (i == k); bool r = (i != k);

45 Relational Operators Ordering operators < Examples > >= <=
int i = 5; int k = 12; bool p = (i < 10); bool q = (k > i); bool r = (i >= k); bool s = (k <= 12);

46 Operator Precedence Revisited
Precedence of operators (from highest to lowest) Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment

47 Operator Precedence Revisited
Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!

48 Operator Precedence Revisited
Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || (!false) == (5 < 24))

49 Conditional Constructs
Provide Ability to control whether a statement list is executed Two constructs If statement if if-else if-else-if Switch statement Left for reading

50 The Basic If Statement Syntax if (Expression) Action
If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action true false

51 Example if (Value < 0) { Value = -Value; }

52 Sorting Two Numbers cout << "Enter two integers: "; int Value1;
cin >> Value1 >> Value2; if (Value1 > Value2) { int RememberValue1 = Value1; Value1 = Value2; Value2 = RememberValue1; } cout << "The input in sorted order: " << Value1 << " " << Value2 << endl;

53 Semantics

54 What is the Output? int m = 5; int n = 10; if (m < n) ++m; ++n;
cout << " m = " << m << " n = " n << endl;

55 The If-Else Statement Syntax if (Expression) Action1 else Action2
If Expression is true then execute Action1 otherwise execute Action2 if (v == 0) { cout << "v is 0"; } else { cout << "v is not 0"; } Expression Action1 Action2 true false

56 Finding the Max cout << "Enter two integers: "; int Value1;
cin >> Value1 >> Value2; int Max; if (Value1 < Value2) { Max = Value2; } else { Max = Value1; cout << "Maximum of inputs is: " << Max << endl;

57 Finding the Max

58 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice if-else-if statement if-else statements “glued” together Switch statement An advanced construct

59 An If-Else-If Statement
if ( nbr < 0 ){ cout << nbr << " is negative" << endl; } else if ( nbr > 0 ) { cout << nbr << " is positive" << endl; else { cout << nbr << " is zero" << endl;

60 A Switch Statement switch (ch) { case 'a': case 'A':
case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

61 cout << "Enter simple expression: ";
int Left; int Right; char Operator; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; default: cout << "Illegal operation" << endl; }

62


Download ppt "COMS 261 Computer Science I"

Similar presentations


Ads by Google