Basic Language Constructs Overview for C++03 References Data and Expressions January 21, 2019 Control Structures Brian A. Malloy Functions Namespaces ◭◭ ◮◮ ◭ ◮ Slide 1 of 27 Go Back Full Screen Quit
1. Overview Overview • These slides review basic C ++ language con- References structs up to, but not including, classes . Data and Expressions Control Structures • In the review, we discuss both C++03 and Functions C++11 Namespaces • In some cases, we compare and contrast the ◭◭ ◮◮ two versions ◭ ◮ • The slides are accompanied by videos that further elicidate the concepts found here. Slide 2 of 27 Go Back Full Screen Quit
2. References Overview • Any Intro C ++ text References • http://en.cppreference.com/w/cpp Data and Expressions • The C ++ ISO Standard Control Structures Functions Namespaces ◭◭ ◮◮ ◭ ◮ Slide 3 of 27 Go Back Full Screen Quit
3. Data and Expressions Overview References Data and Expressions Control Structures Functions Namespaces ◭◭ ◮◮ ◭ ◮ Slide 4 of 27 Go Back bool ⇒ true or false Full Screen Quit
3.1. Operators Overview • Expressions are composed of operators, vari- References ables, constants and parentheses Data and Expressions • Logical operators: &&, ||, ! Control Structures • Relational operators: <, >, ==, !=, <=, >= Functions • However, an expression can be considered Namespaces as a Boolean condition where 0 is false and all other values are true: ◭◭ ◮◮ int x = rand(); ◭ ◮ if (x) . . . • Of course, the rules for mixed types still Slide 5 of 27 apply, so 2/4 evaluates to 0 Go Back Full Screen Quit
3.2. Operators Overview • unary, binary, and ternary describe the num- References ber of operands that an operator uses. Data and Expressions • For example, -7 is unary minus; i.e., one Control Structures operand Functions • 3 - 7 is binary minus; i.e., two operands Namespaces • There is only one ternary operator and it’s very useful; for example, the following ◭◭ ◮◮ expression evaluates to the larger of the two ◭ ◮ operands: (a > b) ? a : b Slide 6 of 27 Go Back Full Screen Quit
3.3. Prefix and Postfix Operators Overview • Prefix operators are evaluated in place. References • Postfix operators are evaluated at the end Data and Expressions of the statement Control Structures Functions 1 #include <iostream> Namespaces 2 int main() { 3 int i = 0, j = 0; ◭◭ ◮◮ 4 std::cout << ++i << std::endl; //output is 1 5 std::cout << j++ << std::endl; //output is 0 ◭ ◮ 6 std::cout << i << j << std::endl; //output is 11 7 return 0; Slide 7 of 27 8 } Go Back Full Screen Quit
3.4. Insertion/Extraction Operators Overview • They are binary, left associative operators References that evaluate to the operator Data and Expressions • For example, the stream insertion opera- Control Structures tor, operator ≪ evaluates to operator ≪ , Functions which is why the following expression works: Namespaces The expression: ◭◭ ◮◮ cout << x << y << endl; is actually: ◭ ◮ (((cout << x) << y) << endl); where (cout << x) places the value of x into the Slide 8 of 27 output stream and evaluates to cout << so that the expression becomes: ((cout << y) << endl); Go Back which places y into the output stream and evaluates to (cout << endl); Full Screen Quit
3.5. constants and constant expressions Overview • const : named constants are preferable to References # define , which is a C artifact Data and Expressions – const char STAR = ’*’; Control Structures – const unsigned MAX = 100; Functions • constexpr : value known at compile time Namespaces constexpr int n1 = 10; ◭◭ ◮◮ std::array<int, n1> a1; // fine constexpr int n2 = 10; ◭ ◮ int a2[n2]; // fine Slide 9 of 27 int n3 = 10; int a3[n3]; // warning Go Back int n = 10; std::array<int, n> a2; // error Full Screen Quit
3.6. NULL, 0, and nullptr Overview • NULL and 0 are integers References • nullptr is a pointer of all types Data and Expressions • prefer nullptr Control Structures Functions Namespaces void f(int i) { std::cout << "int" << std::endl; } void f(char* c) { std::cout << "pointer" << std::endl; } ◭◭ ◮◮ int main() { f(NULL); // error ambiguous call ◭ ◮ f(0); // error ambiguous call f(nullptr); // prints pointer Slide 10 of 27 } Go Back Full Screen Quit
3.7. Mixed Type Expressions Overview • Are promoted or truncated: References 1. 5/2 ⇒ 2 Data and Expressions 2. int(2.3) ⇒ 2 Control Structures 3. float(2/4) ⇒ 0.0 Functions 4. 4/8 ⇒ 0 Namespaces 5. float(4)/8 ⇒ 0.5 ◭◭ ◮◮ 6. 2.0/4 ⇒ t 0.5 • Prefer C ++ cast → easier to find in code ◭ ◮ static cast < float > (5/10) evaluates to 0.0 Slide 11 of 27 Go Back Full Screen Quit
3.8. Structured Data Types Overview • Arrays, like C, are passed by reference References Data and Expressions • union s: obviated by inheritance Control Structures • struct s: same as classes except for default Functions protection: Namespaces – Default protection of class is private ◭◭ ◮◮ – Default protection of struct is public ◭ ◮ – structs are useful for storing global data: I prefer Singleton Slide 12 of 27 • Classes are covered in slides about classes Go Back Full Screen Quit
4. Control Structures Overview • selection: if , if/else , switch References Data and Expressions • repetition: for , while , do/while Control Structures • In general, I much prefer clarity and read- Functions ability to obfuscated, hacked, terse code. Namespaces Thus, I prefer the use of brackets because they promote readability. The first exam- ◭◭ ◮◮ ple below is preferable to the second: int sum = 0; ◭ ◮ for (unsigned i = 0; i < MAX; ++i) { sum += i; Slide 13 of 27 } Go Back int sum = 0; for (unsigned i = 0; i < MAX; ++i) sum += i; Full Screen Quit
4.1. switch Overview • If a switch value matches a case value, References then it matches all cases until a break is encountered: Data and Expressions Control Structures int count = 0; Functions int index = 1; Namespaces switch (index) { case 0: ++count; ◭◭ ◮◮ case 1: ++count; case 2: ++count; ◭ ◮ case 3: ++count; case 4: ++count; Slide 14 of 27 case 5: ++count; default: ++count; } Go Back cout << count << endl; // prints 6 Full Screen Quit
4.2. switch/case/break is useful Overview • We may wish to match several values, so References multiple case values w/out a break are like logical or . In the next example, we can Data and Expressions match either upper or lower case letters: Control Structures Functions int count = 0; Namespaces char ch = ’b’; switch (ch) { ◭◭ ◮◮ case ’A’ : case ’a’: ++count; break; case ’B’ : case ’b’: ++count; break; ◭ ◮ case ’C’ : case ’c’: ++count; break; default: cout << "Oops" << endl;; Slide 15 of 27 } Go Back Full Screen Quit
4.3. Short-circuit Evaluation Overview • If evaluation of the first operand obviates References evaluation of the second, then the second Data and Expressions operand is not evaluated. Control Structures • Short-circuit evaluation can be useful. If Functions number happens to be zero, then we won’t Namespaces get a division by zero error in the following example: ◭◭ ◮◮ float sum = 0.0; ◭ ◮ int number = rand(); if ( number != 0 && sum/number > 90.0) { Slide 16 of 27 ... } Go Back Full Screen Quit
4.4. for Overview • The scope of the loop control variable (LCV) References (in this case i ) is the loop body: Data and Expressions for (int i = 0; i < MAX; ++i) { Control Structures cout << i; Functions } Namespaces i is out of scope here ◭◭ ◮◮ • The following hack would be more readable if the programmer used while ( true ) ◭ ◮ // Obfuscated code; great for job security! Slide 17 of 27 i = 0; for ( ; ; ) { Go Back if (i > MAX) break; cout << ++i; Full Screen } Quit
• ranged for loops : We will discuss later Overview w/ vectors References Data and Expressions Control Structures Functions Namespaces ◭◭ ◮◮ ◭ ◮ Slide 18 of 27 Go Back Full Screen Quit
5. Functions Overview • Can be void or return a value. References • Each C ++ program contains a function called Data and Expressions main , which returns an integer. Control Structures • There are two acceptable forms of main : Functions Namespaces ************************************ int main() { ◭◭ ◮◮ return 0; } ◭ ◮ ************************************ int main(int argc, char* argv[]) { Slide 19 of 27 return 0; } Go Back ************************************ and the return statement is optional Full Screen Quit
5.1. Parameter Transmission Modes Overview • The C language has one mode References • C ++ has four modes: Data and Expressions 1. value : default; makes local copy Control Structures 2. reference : use &; pass the address Functions 3. const reference : for large objects Namespaces 4. rvalue reference : later: ref v ptr ◭◭ ◮◮ ◭ ◮ Slide 20 of 27 Go Back Full Screen Quit
Recommend
More recommend