CS 112 - Week 5 Lecture 1 - 2022-09-20 TODAY WE WILL * announcements * start intro to writing a C++ class * prep for next class * CS Club - Coding Cafe - the FIRST and THIRD Tuesdays of each month, starting TODAY! * 4:30 - 6:00 pm, meet in BSS 302 * Coding Cafe - a place to meet with other students, collaborate and commiserate, and work on homework and coding projects! * be sure to submit any remaining improved-pieces from HWs 1-3 by 11:59 pm TONIGHT, because hopefully you will be able to reach example solutions for HWs 1-3 at 12:01 am Wednesday * these will hopefully be useful in studying for FRIDAY's Exam 1 (9:00 am in BSS 317, Friday, Sept 23) ===== INTRO to writing your OWN C++ classes (to define your own data types...) ===== * concept of an Abstract Data Type (ADT) ...the idea that, in programming, you may want CUSTOM data types to ABSTRACT important concepts or things you want to represent in a program; abstract data type: includes the data desired (the characteristics desired for an instance of that type) PLUS the operations available for that type (for instances of that type) in C++, you can define a class to define an abstract data type -- and a class lets you specify the data and operations for instances of that class... * another important concept in ADTs is the concept of information hiding -- the user should not really care HOW the ADT is implemented "under the hood" (and they should not be able to BREAK an instance of the ADT by mucking with what is going on "under the hood" * the C++ class syntax DOES support information hiding -- the programmer CAN hide such details from the user and limit the user's access to those details; * so -- in Design Recipe terms -- we're saying that, as part of thinking about the data involved in a problem, you might choose to decide to implement a CLASS to represent that data in writing program(s) using that data ==== * object-oriented languages can take different approaches -- here is C++'s approach You can define a CLASS * in that class, you declare data fields (Savitch calls them member variables) representing the DATA for an instance of that class * and you also declare methods (Savitch calls them member functions) representing the available OPERATIONS on instances of that class * what, then, is an OBJECT in all this? In C++, and OBJECT is an instance of a class * and C++ has other possibities here, too; ...like INHERITANCE! you can define a class to be a SUBCLASS of another class, and INHERIT a bunch of its code, and then just CUSTOMIZE the particular features of instances of that subclass; ...ways to specify the "visibility" of various parts of a class; ...and ways to hopefully make your data more robust, less prone to at least some errors; * and in C++, you can talk about separate compilation of classes very similar to how we've been doing for functions; * some common categories of methods expected of a class: * ways to create new instances (new objects) of that class: constructor methods (constructors for short) * we are going to follow the style rule that says data fields in general should be PRIVATE -- not directly accessible to the user -- and their access is controlled by the provided methods accessor methods - accessors for short (sometimes called getters) - to ACCESS the values in appropriate data fields mutator methods - mutators for short (sometimes called setters) - to MUTATE or MODIFY the values in appropriate data fields and you can also have other methods for other actions you decide you want! * in a separate compilation approach to a C++ class, you have a header file, .h file, with the class DEFINITION (including what are its data fields, what are headers of its methods, etc.) and you have an implementation file, .cpp file, with the implementations of its methods * CS 112 class style: we will name our new classes using CamelCase starting with an uppercase letter and then using an uppercase letter to separate "words" in the name and our methods camelCase starting with a lowercase letter BUT with ONE IMPORTANT EXCEPTION: constructor methods MUST be EXACTLY the name of the class!! * CONTINUING how-to-define-a-class in example-in-progress class PlayerChar