CIS 190: C/C++ Programming: Vectors, Enumeration, Overloading, and More!
CIS 190: C/C++ Programming: Vectors, Enumeration, Overloading, and More!
Lecture 9
Vectors, Enumeration,
Overloading, and More!
1
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
2
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
7
Vectors
similar to arrays, but much more flexible
C++ will handle most of the annoying bits
Declaring a Vector
vector <int> intA;
empty integer vector, called intA
intA
Declaring a Vector
vector <int> intB (10);
integer vector with 10 integers,
initialized (by default) to zero
intB
10
Declaring a Vector
vector <int> intC (10, -1);
integer vector with 10 integers,
initialized to -1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
intC
11
Vector Assignment
unlike arrays, can assign one vector to another
even if theyre different sizes
as long as theyre the same type
intA = intB;
12
Vector Assignment
unlike arrays, can assign one vector to another
even if theyre different sizes
as long as theyre the same type
intA = intB;
size 0
size 10
13
Vector Assignment
unlike arrays, can assign one vector to another
even if theyre different sizes
as long as theyre the same type
intA = intB;
size 0
size 10
intA
14
Vector Assignment
unlike arrays, can assign one vector to another
even if theyre different sizes
as long as theyre the same type
intA = intB;
size 0
size 10
intA = charA;
15
Vector Assignment
unlike arrays, can assign one vector to another
even if theyre different sizes
as long as theyre the same type
intA = intB;
size 0
size 10
intA = charA;
NOT okay!
16
Copying Vectors
can create a copy of an existing
vector when declaring a new vector
vector <int> intD (intC);
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
intC
17
Copying Vectors
can create a copy of an existing
vector when declaring a new vector
vector <int> intD (intC);
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
intC
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
intD
18
.at() operation:
intB.at(2) = 7;
19
20
intB
21
intB
24
ref);
ModifyV
ModifyV
ModifyV
ModifyV
ModifyV
28
Multi-Dimensional Vectors
2-dimensional vectors are essentially
a vector of vectors
vector < vector <char> > charVec;
29
Multi-Dimensional Vectors
2-dimensional vectors are essentially
a vector of vectors
vector < vector <char> > charVec;
30
.at() operator:
intB.at(2).at(3) = 7;
31
.at() operator:
intB.at(2).at(3) = 7;
32
resize()
void resize (n, val);
33
resize()
void resize (n, val);
n is the new size of the vector
if larger than current
vector size is expanded
if smaller than current
vector is reduced to first n elements
34
resize()
void resize (n, val);
val is an optional value
used to initialize any new elements
35
Using resize()
if we declare an empty vector, one way we can
change it to the size we want is resize()
vector < string > stringVec;
stringVec.resize(9);
36
Using resize()
if we declare an empty vector, one way we can
change it to the size we want is resize()
vector < string > stringVec;
stringVec.resize(9);
or, if we want to initialize the new elements:
stringVec.resize(9, hello!);
37
push_back()
add a new element at the end of a vector
void push_back (val);
38
push_back()
add a new element at the end of a vector
void push_back (val);
resize() vs push_back()
resize() is best used when you know the
exact size a vector needs to be
40
resize() vs push_back()
resize() is best used when you know the
exact size a vector needs to be
like when you have the exact number of
songs a singer has in their repertoire
41
resize() vs push_back()
resize() is best used when you know the
exact size a vector needs to be
like when you have the exact number of
songs a singer has in their repertoire
size()
unlike arrays, vectors in C++ know their size
due to C++ managing vectors for you
43
Using size()
int cSize;
// this will not work
cSize = charVec.size();
44
Using size()
int cSize;
// this will not work
cSize = charVec.size();
//you must cast the return type
cSize = (int) charVec.size();
45
Livecoding
lets apply what weve learned about vectors
LIVECODING
46
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
47
Enumeration
enumerations are a type of variable used to
set up collections of named integer constants
useful for lists of values that are tedious to
implement using #define or const
#define
#define
#define
#define
WINTER
SPRING
SUMMER
FALL
0
1
2
3
48
Enumeration Types
two types of enum declarations:
named type
enum seasons {WINTER, SPRING,
SUMMER, FALL};
unnamed type
enum {WINTER, SPRING,
SUMMER, FALL};
49
Named Enumerations
named types allow you to create variables of
that type, use it in function arguments, etc.
// declare a variable of
//
the enumeration type seasons
//
called currentSemester
enum seasons currentSemester;
currentSemester = FALL;
50
Unnamed Enumerations
unnamed types are useful for naming
constants that wont be used as variables
51
Unnamed Enumerations
unnamed types are useful for naming
constants that wont be used as variables
int userChoice;
cout << Please enter season: ;
cin >> userChoice;
switch(userChoice) {
case WINTER:
cout << brr!; /* etc */
}
52
Benefits of Enumeration
named enumeration types allow you to
restrict assignments to only valid values
a seasons variable cannot have a value other
than those in the enum declaration
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
54
Function Overloading
last class, covered overloading constructors:
55
Operators
given variable types have predefined behavior
for operators like +, -, ==, and more
for example:
stringP =
if (charX
intA =
intD +=
}
stringQ;
== charY) {
intB + intC;
intE;
56
Operators
would be nice to have these operators also
work for user-defined variables, like classes
we could even have them as member
functions!
allows access to member variables and functions
that are set to private
Overloading Restrictions
cannot overload ::,
.,
*, or ? :
58
Why Overload?
lets say we have a Money class:
class Money {
public: /* etc */
private:
int m_dollars;
int m_cents;
} ;
59
Why Overload?
and we have two Money objects:
60
Why Overload?
and we have two Money objects:
// we have $700.65 in cash, and
// need to pay $99.85 for bills
Money cash(700, 65);
Money bills(99, 85);
61
Why Overload?
and we have two Money objects:
// we have $700.65 in cash, and
// need to pay $99.85 for bills
Money cash(700, 65);
Money bills(99, 85);
what happens if we do the following?
cash = cash - bills;
62
Why Overload?
and we have two Money objects:
// we have $700.65 in cash, and
// need to pay $99.85 for bills
Money cash(700, 65); cash is now 601
Money bills(99, 85); dollars and -20
cents, or $601.-20
Why Overload?
that doesnt make any sense!
whats going on?
64
Why Overload?
the default subtraction operator provided by
the compiler only works on a nave level
subtracts bills.m_dollars from
cash.m_dollars
and subtracts bills.m_cents from
cash.m_cents
65
Why Overload?
the default subtraction operator provided by
the compiler only works on a nave level
subtracts bills.m_dollars from
cash.m_dollars
and subtracts bills.m_cents from
cash.m_cents
67
were returning
an object of
the class type
68
were returning
an object of
the class type
69
were returning
an object of
the class type
70
were returning
an object of
the class type
were passing in a
Money object
71
were returning
an object of
the class type
were passing in a
Money object as a
const
72
were returning
an object of
the class type
were passing in a
Money object as a
const and by
reference
73
were passing in a
Money object as a
const and by
reference
why would we
want to do that?
were returning
an object of
the class type
74
76
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
78
80
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
82
Refresher on Constructors
special member functions used to create
(or construct) new objects
automatically called when an object is created
implicit:
explicit:
Money cash;
Money bills (89, 40);
Destructors
destructors are the opposite of constructors
they are used when delete() is called on an
instance of a user-created class
compiler automatically provides one for you
but it does not take into account dynamic memory
84
Destructor Example
lets say we have a new member variable of our
Date class called m_next_holiday
pointer to a string with the name of the next holiday
class Date {
private:
int
m_month;
int
m_day;
int
m_year;
string *m_next_holiday ;
};
85
Destructor Example
we will need to update the constructor
Date::Date (int m, int d, int y,
string next_holiday) {
SetMonth(m);
SetDay(d);
SetYear(y);
m_next_holiday = new string;
*m_next_holiday = next_holiday;
}
86
Destructor Example
we will need to update the constructor
what
other
Date::Date (int m,
int
d, changes
int y, do
string
we *next_holiday)
need to make to a {
SetMonth(m); class when adding a
new member variable?
SetDay(d);
SetYear(y);
m_next_holiday = new string;
*m_next_holiday = next_holiday;
}
87
Destructor Example
we also now need to create a destructor of
our own:
~Date();
// our destructor
90
91
Outline
Access Restriction
Vectors
Enumeration
Operator Overloading
New/Delete
Destructors
Homework & Project
92
Homework 6
Classy Trains
last homework!!!
Project
final project will be due December 2nd
two presentation days:
December 2nd, 6-7:30 PM, Towne 100 (Tue)
December 3rd, 1:30-3 PM, Towne 319 (Wed)
Project
project must be completed in groups (of two)
groups will be due October 29th on Piazza
if you dont have a group, youll be assigned one