EECS 183 DISCUSSION 8 Hannah Westra Upcoming Due

  • Slides: 24
Download presentation
EECS 183 DISCUSSION 8 Hannah Westra

EECS 183 DISCUSSION 8 Hannah Westra

Upcoming Due Dates ◦ Project 3 Due Tonight (10/28) at 6 pm ◦ Assignment

Upcoming Due Dates ◦ Project 3 Due Tonight (10/28) at 6 pm ◦ Assignment 4 Due Next Friday (11/4) at 6 pm ◦ Final Project Options are Released ◦ Descriptions and Video Demonstrations ◦ Start thinking about groups

File Stream ◦ Up until now you’ve needed to #include <iostream>. Why? ◦ To

File Stream ◦ Up until now you’ve needed to #include <iostream>. Why? ◦ To be able to use the input stream cin and the output stream cout ◦ What is <fstream>, and why do we include it? ◦ If we want to use a file stream and read in data/input from a file, or write data (output) to a file

In pu >_chrysanthemum t u utp ts tre am USER INPUTS “chrysanthemum” am e

In pu >_chrysanthemum t u utp ts tre am USER INPUTS “chrysanthemum” am e r st o PROGRAM INPUTS “chrysanthemum”

Careful with input streams ◦ Do NOT use cin. eof() or my_ifstream. eof() as

Careful with input streams ◦ Do NOT use cin. eof() or my_ifstream. eof() as a condition ◦ Can sometimes cause undefined behavior ◦ INSTEAD: ◦ while (in. File >> x){…} // while reading into the file is still successful ◦ while (!in. File. fail()){…} //while reading into the file does not cause a fail state ◦ You can use cin. clear() or in. File. clear() or out. File. clear() to reset a fail state ◦ Where “out. File” is the variable name of your ostream ◦ A useful function to do this all was provided in lecture

Questions About Filestreams?

Questions About Filestreams?

Bjarne Break!

Bjarne Break!

Definition of a Class ◦ What is a class? ◦ A class is a

Definition of a Class ◦ What is a class? ◦ A class is a container that can hold different types of objects (objects with different data types) ◦ A class is a user-defined data type ◦ Just like int and double and string are data types, so is the class you define ◦ A class is a way to group together related information

Thinking about Classes ◦ A class is like an object that holds multiple pieces

Thinking about Classes ◦ A class is like an object that holds multiple pieces of information ◦ You pass around a box that holds some more information inside it ◦ If you have access to the box, you also have access to what’s inside it

Let’s Create a Student Class ◦ What are some defining characteristics of a student?

Let’s Create a Student Class ◦ What are some defining characteristics of a student? ◦ uniqname ◦ umid ◦ year ◦ A student has all of these attributes, so we could make a Student class, where these attributes are member variables ◦ This allows us to declare a Student (our custom data type) that holds attributes of the student as well

Student Class

Student Class

Public vs Private ◦ A private member variable or member function means that only

Public vs Private ◦ A private member variable or member function means that only members of the class can access it ◦ Member variables are usually private, and we use setters and getters to access them ◦ A public member variable or function means that any part of the code can access that function or variable. Setters and getters are always public.

Is it a Constructor? ◦ There are 3 ways to tell ◦ 1) The

Is it a Constructor? ◦ There are 3 ways to tell ◦ 1) The function name is exactly the same as the class name ◦ 2) The function has no return type ◦ 3) The function starts with a capital letter (not definitive) ◦ How do we know which type of constructor it is? ◦ If it has no parameters (eg. Student()) then it is a default constructor ◦ If is has parameters then it is a non-default constructor

Declaring an object of type Student How would we declare and initialize this Student

Declaring an object of type Student How would we declare and initialize this Student object? DEFAULT CONSTRUCTOR RUNS NONDEFAULT CONSTRUCTOR RUNS

Header Files vs Source Files ◦ Header files have the. h extension ◦ Header

Header Files vs Source Files ◦ Header files have the. h extension ◦ Header files are where class definitions and function declarations go ◦ Contains: ◦ Some member functions ◦ Including a print function ◦ Public and private members ◦ A default constructor ◦ A non-default constructor ◦ Getters ◦ Source files have the. cpp extension ◦ Source files are where function implementations go ◦ Has all the implementation of the declaration in the header file ◦ Be careful with syntax here!!! ◦ Scope Resolution Operator!

Example. h File

Example. h File

Example. cpp File

Example. cpp File

In main. cpp we want to… ◦ Make 3 instances of the class ◦

In main. cpp we want to… ◦ Make 3 instances of the class ◦ Call one with the default constructor ◦ Call the others with the non-default constructors ◦ Access elements and change them, probably using getters and setters ◦ Do something fun…

Upcoming Due Dates ◦ Project 3 Due Tonight (10/28) at 6 pm ◦ Assignment

Upcoming Due Dates ◦ Project 3 Due Tonight (10/28) at 6 pm ◦ Assignment 4 Due Next Friday (11/4) at 6 pm ◦ Final Project Options are Released ◦ Descriptions and Video Demonstrations ◦ Start thinking about groups

Questions?

Questions?