CMPT 117: Tutorial 1 Craig Thompson 12 January 2009
Administrivia Coding habits OOP Header Files Function Overloading
Class info Tutorials Review of course material additional examples Q&A Labs Work on assignments Get help with specific problems Textbook Website http://www.cs.colorado.edu/~main/dsoc.html Lecture slides, sample code, example exam questions, etc.
Contact info Email: cdt830@mail.usask.ca web: http://www.cs.usask.ca/~cdt830/cmpt117 discussion forum: ihelp.usask.ca
How to approach the assignments the assignment specification will give you a rough idea of what you are supposed to do If you don t understand part of the assignment requirements, ask the professor Write (not code) a detailed description of all of the steps in the assignment Write descriptions of all of the files, classes, functions, etc that you will need
Preconditions & Postconditions Preconditions: specify what must be true when a function is called Postconditions: specify what will be true when a function returns, if the preconditions were true
test your code make a list of ways you can test your code (test cases) you may be required to hand in a description of how you tested your code
Object Oriented Programming Objects vs Classes Encapsulation Separation of Concerns
Classes like struct, but can include variables AND functions access variables within a class by using the dot operator after defining a class, we can declare variables of the new type
member functions define the behavior of a class accessed using the dot operator ensure that the class is used in a consistent manor
information hiding you may want to ensure that a class is only accessed through its member functions public members can be accessed by anyone also called the interface to the class private members can only be accessed by member functions
information hiding How do you get the value of a private variable? make a public member function that returns the value! also called the interface to the class private members can only be accessed by member functions
Constructors one or more functions used to initialize the variables of an object has the same name as the class can take parameters does not have a return type constructors are called when the object is created
function headers and definitions help to improve readability of your code don t need to know about how the function is implemented remove multi-line function definitions from the class definition place long definitions after the class, prepend classname:: to functions
Accessors and Mutators Mutator methods change values within the class Accessor methods don t change values within the class adding the keyword const ensures that the function cannot change the values
parameter passing pass by value creates a copy of the value and sends the copy to the function protects original variable from being changed slow pass by reference sends the location of the variable to the function the original variable can be changed fast
Protecting pass-by-reference parameters from change declare them const only accessor methods can be called on const parameters
Header Files Why: separate what the functions do from how they do it header files (.h) include: class definitions function prototypes global variables header file (.h) contains what the class does, the implementation file (.cpp) contains how the class works #include filename.h
Macro Guards prevent a file from being included multiple times page 53 in the textbook
default parameters default parameters can be used to reduce the number of functions in your code page 65 in the textbook default parameters go in the function prototype (.h file
function overloading we want one function that can operate on multiple datatypes (int, float, double, etc.) convert inputs: write one function, and convert the inputs to match the parameters of the function (slow, may generate warnings for type conversions) write multiple functions: no type conversions, but the code gets messy, and there are multiple functions that are nearly identical overloading: write multiple functions with the same name, but different parameters and return types (for ints, floats, doubles, etc.)