Section 2 - More Basics
description
Transcript of Section 2 - More Basics
![Page 1: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/1.jpg)
Section 2 - More Basics
![Page 2: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/2.jpg)
The char Data Type
• Data type of a single character
• Example
char letter;letter = 'C';
![Page 3: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/3.jpg)
A type char character is encapsulated by single quotes ‘
Characters are encoded in the computer using a scheme where an integer represents a particular character
Examples ' ' encoded as 32 '+' encoded as 43
'A' encoded as 65 'Z' encoded as 90
'a' encoded as 97 'z' encoded as 122
This allows us to compare characters.
![Page 4: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/4.jpg)
Character Operations
• Relational (aka comparison) operations are defined for characters types (as well as for other data types)
• The result is either true or false
– 'a' < 'b' is true– '4' > '3' is true– '6' <= '2' is false
![Page 5: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/5.jpg)
Example - Read Characters
To read a character from the keyboard, use
char ch; cout << "Enter a character: ";cin >> ch;
![Page 6: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/6.jpg)
Character Constants
Explicit (literal) characters within single quotes
'a','D','*'
Special characters - delineated by a backslash \
Two character sequences (escape codes)
Some important special escape codes
\t denotes a tab \n denotes a new line
\\ denotes a backslash \' denotes a single quote
\" denotes a double quote
To use, wrap up within single quotes
'\t' for the tab '\n' for the new line
![Page 7: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/7.jpg)
Character Strings (aka Text Strings)
• Can store a series of characters (aka string) in consecutive memory locations:
"Hello“
• Stored with the null terminator, \0, at the end
• Comprises the characters between the " "
H e l l o \0
![Page 8: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/8.jpg)
Literal String Constants
• A literal string constant is a sequence of zero or more characters enclosed in double quotes
– “Walk, don’t run"– “Bulgaria"– ""
• String is not a fundamental type- It is a C++ defined class
![Page 9: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/9.jpg)
• To access a library use a preprocessor directive to add its definitions to your program file
#include <string>
string s = "Sharp";string t = “Dull";
![Page 10: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/10.jpg)
Class string
Used to represent a sequence of characters as a single text string
Some definitions
string Name = “Ivan";
string DecimalPoint = ".";
string empty = "";
string copy = name;
string Question = '?'; // illegal
![Page 11: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/11.jpg)
Class string
• Some string member functions
– size() determines number of characters in the stringstring 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
– find() computes the position of a subsequenceint j = Saying.find("it"); // 10int k = Saying.find("its"); // ?
![Page 12: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/12.jpg)
Class string
• Auxiliary operators
– + string concatenation – join strings togetherstring Part1 = "Me";string Part2 = " and ";string Part3 = "You";string All = Part1 + Part2 + Part3;
– += compound concatenation assignmentstring ThePlace = "Blagoevgrad";ThePlace += ", 2700";
![Page 13: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/13.jpg)
Example
string fname = “Ivan";string lname = “Ivanov";string name = fname + lname;cout << name << endl;name = fname + " " + lname;cout << name << endl;
The output will beIvanIvanovIvan Ivanov
![Page 14: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/14.jpg)
• Represents values that are true or false• false is represented by 0, and true by 1
bool allDone = true;
bool finished = false;
• What happens here?bool b = true;int i = b;cout << b << endl;cout << i << endl;
The bool Data Type
![Page 15: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/15.jpg)
Logical Expressions
Logical expressions have the one of two values - true or false
- A rectangle has three sides
- The instructor has a pleasant smile
Three key logical operations
And operations
Or operation
Not operation - negate
![Page 16: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/16.jpg)
Type bool has two symbolic constants
truefalse
Logical (aka Boolean) operators
The And operator is &&
The Or operator is ||
The Not operator is !
![Page 17: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/17.jpg)
Example logical expressions
bool P = true;
bool Q = false;
bool R = true;
bool S = (P && Q);
bool T = ((!Q) || R);
bool U = !(R && (!Q));
![Page 18: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/18.jpg)
Write a program that lets the user enter a year and checks whether it is a leap year.
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400.
So you can use the following Boolean expression to check whether a year is a leap year:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
![Page 19: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/19.jpg)
#include <iostream>using namespace std;
int main(){ int number; cout << "Enter an integer: "; cin >> number;
if (number % 2 == 0 && number % 3 == 0) cout << number << " is divisible by 2 and 3." << endl;
if (number % 2 == 0 || number % 3 == 0) cout << number << " is divisible by 2 or 3." << endl;
if ((number % 2 == 0 || number % 3 == 0) && !(number % 2 == 0 && number % 3 == 0)) cout << number << " divisible by 2 or 3, but not both." << endl;
return(0);}
![Page 20: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/20.jpg)
How would you write this expression in C++
1 <= numberOfDaysInAMonth <= 31
Need to create a compound expression using Boolean operators
![Page 21: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/21.jpg)
Relational Operators
Equality operators== test for equality
!= test for inequality
Examplesint i = 32;int k = 45;
bool q = (i == k); // false
bool r = (i != k); // true
![Page 22: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/22.jpg)
Comments
Allow text commentary to be included in program
Importance
- Programs are read far more often than they are written
- Programs need to be understood so that they can be
maintained
C++ has two conventions for comments
// single line comment (preferred)
/* long comment */ (save for debugging)
Typical use
Describing the working of parts of a program
![Page 23: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/23.jpg)
Enumerated Types
Allows the definition of programmer-defined types
enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY};
Once a type is defined, you can declare a variable of that type:
Day day;
The variable day can hold one of the values defined in the enumerated type. For example, the following statement assigns enumerated value MONDAY to variable day:
day = MONDAY;
![Page 24: Section 2 - More Basics](https://reader036.fdocuments.us/reader036/viewer/2022081520/56816722550346895ddbaaff/html5/thumbnails/24.jpg)
Enumerated Types
As with any other type, you can declare and initialize a variable in one statement:
Day day = MONDAY;
Furthermore, C++ allows you to declare an enumerated type and variable in one statement. For example,
enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY;