Section 2 - More Basics
The char Data Type Data type of a single character Example char letter; letter = 'C';
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.
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
Example - Read Characters To read a character from the keyboard, use char ch; cout << "Enter a character: "; cin >> ch;
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
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 " " Hello\0
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
To access a library use a preprocessor directive to add its definitions to your program file #include string s = "Sharp"; string t = “Dull";
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
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 – find() computes the position of a subsequence int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?
Class string Auxiliary operators – + string concatenation – join strings together string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; – += compound concatenation assignment string ThePlace = "Blagoevgrad"; ThePlace += ", 2700";
Example string fname = “Ivan"; string lname = “Ivanov"; string name = fname + lname; cout << name << endl; name = fname + " " + lname; cout << name << endl; The output will be IvanIvanov
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
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
Type bool has two symbolic constants true false Logical (aka Boolean) operators The And operator is && The Or operator is || The Not operator is !
Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = (P && Q); bool T = ((!Q) || R); bool U = !(R && (!Q));
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)
#include 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); }
How would you write this expression in C++ 1 <= numberOfDaysInAMonth <= 31 Need to create a compound expression using Boolean operators
Relational Operators Equality operators == test for equality !=test for inequality Examples int i = 32; int k = 45; bool q = (i == k); // false bool r = (i != k); // true
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
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;
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;