Keywords for Midterm 2

This is not a complete review of everything you guys have seen since the first midterm. Be sure to go through all of Professor Radke's lecture slides for more details and examples. This review is only meant to jog your memory on key things we've covered in discussion.

Pass-by-value

A copy of the input is made and the copy is passed into the function, meaning any changes made to the input variable will not be reflected in the original variables. For example,

double mean(double x, double y) {
return (x+y)/2.0;
}

Pass-by-reference

No copy of the input is made so the actual variable itself is passed into the function, meaning any changes made to the input variable WILL change the original variable. For example,

void swap(double& x, double& y) {
double temp = x;
x = y;
y = temp;
return;
}

Pass-by-constant reference

No copy of the input is made so the actual variable itself is passed into the function, but the const keyword guarantees no changes can be made. For example,

void print(const vector<int>& v) {
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
return;
}

There are examples in Professor Radke's lecture slides that force us to really understand the implications of pass-by-value vs pass-by-reference.

Function overloading

Using the same function name with different function definitions. This is allowed only if these functions can be differentiated by the input parameter types! Note that we cannot distinguish between pass-by-value, pass-by-reference, pass-by-const-reference. For example,

void print(const vector<int>& v);
void print(const vector<double>& v);
void print(const vector<string>& v);

Operator overloading

This is the act of redefining operators so that they are compatible with our own user-defined classes. Operators include <, <=, >, >=, ==, +, -, *, / etc.

bool operator < (Class_name other) const;
bool operator <= (Class_name other) const;

Random numbers

We need to include two libraries to generate random numbers:

#include <cstdlib> // for srand(int) and rand()
#include <ctime> // for time(0)
using namespace std;
int main() {
srand((int)time(0)); // THE ONLY PLACE THIS LINE GOES
// Rest of your code here
return 0;
}

The line srand((int)time(0)) or srand(time(0)) is for seeding the random number generator, and it ONLY GOES AT THE VERY TOP OF int main()!!!

Vectors vs. arrays

Look at the last slide of Lecture 18.

User-defined classes

Know all the pieces of defining a class:

Coding problems

Here's a pretty in-depth coding practice problem where you need to define the member functions for the Rectangle class and a non-member function Rectangle max(const vector<Rectangle>& vec); so that the int main() code included will compile and run correctly.
Practice