===== CS 111 - Week 10 Lecture 1 - 2024-10-29 ===== ===== TODAY WE WILL: ===== * announcements * concept of a class, and calling a class method * intro our first C++ branching statement: if statement! * prep for next class ===== * Should be working on Homework 8! * have you double-checked your proposed Spring 2025 course schedule with your advisor yet? * if not -- it is a GOOD idea to do so before your Spring 2025 Registration Appointment comes up (between Nov 11 and Nov 22) ===== Review: several of the types of compound expression syntax in C++: ===== infix operator: expr infix_op expr 45 / 5 parentheses: (expr) ((true)) (5 + 4) / 7 prefix operator: prefix_op expr ! true not false function calls: funct_name(arg_expr, arg_expr, ...) max(3, (2 + 1.1)) ===== another option: a C++ method call ===== ...but we need some background for that: ===== C++ is designed to allow object-oriented programming! ===== * in one branch of object-oriented programming, you have the concept of a CLASS * my favorite definition of a class: a way to create your own custom data types! * you can decide you want a specialized data type with certain characteristics for each element of that type * a class provides a way to say, here are the characteristics (data fields) of an instance of this class here are ways of creating a new instance of this class (constructor) here are ways of operating and doing things with this class (METHODS) * hey, C++'s modern string type is a class! so it has methods! * an instance of a class is called an OBJECT, and so when you program with instances of classes you can call that object-oriented PROGRAMMING * an object of a class HAS the data fields that class defines for it, AND that object can call the methods for that class ===== syntax for calling a method in C++ ===== * SO: string is a class that C++'s string library provides! * SO: how do you call a string object's methods? desired_object.desired_method(des_arg, des_arg, ...) ...think: I am calling desired_method on desired_object * say I have: const string FIRST_COURSE = "CS 111"; we can say: FIRST_COURSE is an instance of the string class, FIRST_COURSE is a string object. * hey, the string class has a length method! it expects NOTHING (!) and returns the length, the number of characters, in the calling string object FIRST_COURSE.length() // should have the value 6, // the length of the calling object // FIRST_COURSE * the above is calling the length method of string object FIRST_COURSE ===== SO: before we get to if: another type of C++ statement: the block ===== * typical C++ statement: return expr; cout << expr; ...a single action ending with a semicolon * another kind of C++ statement is a block: { statement; statement; ... } * this block is considered a *single* C++ statement by the C++ compiler * THUS: anywhere a C++ statement is allowed, you can put a block * and by the way: a function's body is always a block... * and remember the CS 111 class coding standards for a block: * { and } are each on their own line * indented even with the preceding line * statement(s) within indented by at least 3 spaces ===== C++ if statement ===== * in Racket, we had: (cond [bool-expr1 result1] [bool-expr2 result2] ... [else result-else] ) * you can do this same logic in C++ using a set of chained instances of if statements: if (bool_expr1) { return result1; } else if (bool_expr2) { return result2; } ... else { return result_else; } * the above behaves like the Racket cond expression -- will take exactly ONE of these branches; * BUT the C++ version above is chained instances of a smaller statement, of a smaller statement, the if statement: simplest C++ if statement: if (bool_expr1) statement1; * if bool_expr1 is true, perform statement1 otherwise just go on * those parentheses around the bool_expr1 are required * you can only have 1 statement after the if -- BUT... that 1 statement can be a block! if (bool_expr1) { statement1; ... statementN; } * (if you do have a non-block single statement for the if, put in on the next line, indented by at least 3 spaces * you can have an optional else: if (bool_expr1) statement1; else statement2; * here, if bool_expr1 is true, you do statement1, OTHERWISE you do statement2 ...so you can NEVER do BOTH statement1 and statement2 !!!