0% found this document useful (0 votes)
16 views3 pages

Pract 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Pract 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

file io

file i/o works like cout and cin


general flow of file operations:
create handle -> try to open -> complete actions -> close
notes for both
failure checking: obj.fail() or !obj.is_open() or obj.good()
close: obj.close()
remember that the actions are almost always stateful:
getline doesn't start from the beginning of the file every time, it
reads line after line
insertion operator inserts after what text has already been added.
ofstream may clear the file beforehand, but insertion will insert
after changes you've made in your current run of the program
input specifics
handle is a ifstream object (input file stream)
reading from a file: >> or getline
>> only gets up to next space OR next newline
getline gets up to newline , including spaces
can use in a while loop, just like how we could take repeated user
input
stop when getline() returns false (no more lines to get), or
obj.eof()
output specifics
handle is a ofstream object (output file stream)
outputting to file: << : output is formatted just like in cout
ofstream options: ios::app : open in append mode

classes and objects


classes are a type of object
they bundle data together and control what aspects of this bundle are
visible (encapsulation)
header specifies data and functions
we say "specify" because multiple instances of the same class type
can exist at the same time
the class only specifies what's in the bundle and what operations we
can complete on this bundle, not how many can exist at the same time
data
public vs. private: allows us to control what data is directly exposed
for security and reduction of complexity
allows for safeguards on how data is modified, and which data is
visible to other parts of your program.
class functions
constructors: structures the data when the instance is created: like
initializing a variable in your main, but operating on mutiple things at
once
has to have exact same name as the class
default vs. parameterized: former allows us to create a "blank"
version, parameterized allows us to set data in the same go
similar to the different ways of declaring a vector
mutators / getters / setters: name for functions that directly help
expose specific data of the bundle
class functions can access all data of our bundle, but allow us to
control how they are presented (when combined with public / private)
ex: instead of having to validate all input to change a certain
variable, we can offload that functionality onto the setter

vectors
an example of a class: extends the idea of an array
declaration format:
vector<type> name;
vector<type> name[size];
vector<type> name[size] = {a,b,c};
default size 0, size doubles if insufficient
allows insertion / deletion, keeps track of size, and provides us with some
additional features
vec[0] is vec.at(0) is arr[0]
instead of having to worry about segfaults when accessing out of
bounds, the function will give an explicit out of bounds error:
easier to debug
instead of having to remember the position of the last element, we can
just vec.push_back(element)
instead of having to separately record the size, vec.size()
instead of having to write deletion/insertion algorithms, we use
vec.erase(vec.begin() + pos) and vec.insert(vec.begin() + pos,
el)
however, this also changes the size, which may cause problems
with iteration
the vec.begin() is required because of how vectors work
internally, it's analagous to position 0
passing vectors
unlike arrays, vectors are passed by value: if you want a function to
modify a vector and get the modified vector back, you have to either
return the vector or pass it by reference.

You might also like